Public/FlyM365GroupApi.ps1

<#
.SYNOPSIS
 
Add mappings into the specified M365Group project.
 
.DESCRIPTION
 
Add mappings into the specified M365Group project.
 
.PARAMETER Project
Specify the name of the project to import mappings.
 
.PARAMETER Path
Specify the csv file path of the project mappings to import.
 
.OUTPUTS
 
None
#>

function Import-FlyM365GroupMappings {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [String]
        ${Project},
        [Parameter(Mandatory = $true)]
        [String]
        ${Path}
    )

    Process {
        'Calling method: Import-FlyM365GroupMappings' | Write-Debug
        $PSBoundParameters | Out-DebugParameter | Write-Debug
        Try {
            $targetProject = Get-FlyProjectByName -ProjectName $Project
            $mappings = New-Object System.Collections.ArrayList;
            $importedMappings = Get-FlyMappingsFromCsv -Path $Path
            #Construct the project mapping list to import
            foreach ($mapping in $importedMappings) {
                $item = [PSCustomObject]@{
                    "sourceName"          = $mapping.'Source Group name'
                    "sourceIdentity"      = $mapping.'Source Group email address'
                    "destinationName"     = $mapping.'Destination Group name'
                    "destinationIdentity" = $mapping.'Destination Group email address'
                }
                [void]$mappings.Add($item);
            }
            #Import project mapping list to the project
            [System.Net.HttpWebRequest]::DefaultMaximumErrorResponseLength = -1
            $result = Add-FlyM365GroupMappings -ProjectId $targetProject.Id -M365GroupMappingCreationModel $mappings.ToArray()
            if ($result) {
                Write-Host 'Successfully added the mappings to the project.' -ForegroundColor Green
            }
        }
        Catch {
            Write-Host 'Failed to add the mappings to the project.' -ForegroundColor Red
            if ($_.ErrorDetails.Message) {
                $errorModel = ConvertFrom-Json $_.ErrorDetails.Message
                if ($errorModel.ErrorCode -eq 'ProjectMappingDuplicated') {
                    Write-Host 'ErrorDetail : ProjectMappingDuplicated' -ForegroundColor Red
                    $errorDetails = ConvertFrom-Json $errorModel.ErrorMessage
                    $errorDetails | Select-Object SourceIdentity, DestinationIdentity, ProjectName | Format-Table
                }
                else {
                    Write-Host $_.ErrorDetails.Message -ForegroundColor Red
                }
            }
            Write-Error $_.Exception
            throw
        }
    }
}

<#
.SYNOPSIS
 
Export the M365Group migration mapping status to a csv file.
 
.DESCRIPTION
 
Export the M365Group migration mapping status to a csv file.
 
.PARAMETER Project
Specify the name of the project to export mappings.
 
.PARAMETER OutFile
Specify the csv file path of the project mappings to export.
 
.PARAMETER Mappings
Specify the csv file to filter specific project mappings to export, export all mappings of the project if not specified.
 
.OUTPUTS
 
None
#>

function Export-FlyM365GroupMappingStatus {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [String]
        ${Project},
        [Parameter(Mandatory = $true)]
        [String]
        ${OutFile},
        [Parameter(Mandatory = $false)]
        [String]
        ${Mappings}
    )

    Process {
        'Calling method: Export-FlyM365GroupMappingStatus' | Write-Debug
        $PSBoundParameters | Out-DebugParameter | Write-Debug
        Try {
            $targetProject = Get-FlyProjectByName -ProjectName $Project -PlatformType 'M365Group'
            $result = New-Object System.Collections.ArrayList;
            #Retrieve the project mappings from the specified project
            $allMappings = Get-FlyAllProjectMappings -ProjectId $targetProject.Id
            #Match the project mapping list between csv file and specified project
            if ($Mappings) {
                $targetMappings = Get-FlyMappingsFromCsv -Path $Mappings
                foreach ($target in $targetMappings) {
                    foreach ($mapping in $allMappings) {
                        $sourceIdentity = [System.Web.HttpUtility]::UrlDecode($target.'Source Group email address')
                        $destinationIdentity = [System.Web.HttpUtility]::UrlDecode($target.'Destination Group email address')
                        if ($mapping.SourceIdentity -eq $sourceIdentity -and $mapping.DestinationIdentity -eq $destinationIdentity) {
                            [void]$result.Add($mapping);
                            break;
                        }
                    }
                }
            }
            else {
                $result = @($allMappings)
            }
            if ($result.Count -gt 0) {
                $folderPath = [System.IO.Path]::GetDirectoryName($OutFile)
                If (-not (Test-Path $folderPath)) {
                    # Folder does not exist, create it
                    [void](New-Item -Path $folderPath -ItemType Directory)
                }
                #Output the matched project mappings to specified file path in csv format
                $result | ForEach-Object {
                    Convert-FlyMappingStatus -Mapping $_
                } | Export-Csv -Path $OutFile -NoTypeInformation
                Write-Host 'Successfully retrieved the migration mappings. File path:' $OutFile -ForegroundColor Green
            }
            else {
                throw 'No mapping in the CSV file matches the existing migration mappings in this project.'
            }
        }
        Catch {
            Write-Host 'Failed to retrieve the migration mappings.' -ForegroundColor Red
            ErrorDetail -Error $_
            throw
        }
    }
}

<#
.SYNOPSIS
 
Generate migration report for the specified project mappings.
 
.DESCRIPTION
 
Generate migration report for the specified project mappings.
 
.PARAMETER Project
Specify the name of the project to generate their migration report.
 
.PARAMETER OutFolder
Specify the folder path of migration report file to download.
 
.PARAMETER FileType
Specify the format of the generated report file, CSV or Excel, optional for CSV type.
 
.PARAMETER Mappings
Specify the csv file to filter specific project mappings to generate report, for all mappings of the project if not specified.
 
.PARAMETER TimeZoneOffset
Specify the UTC time offset of current browser. This value will be used to adjust time values when generating the report file, optional for UTC timezone.
 
.PARAMETER Include
Specify a list of objects to be included in the migration report, use Tab for available values, optional if you do not export object details.
 
.OUTPUTS
 
None
#>

function Export-FlyM365GroupMigrationReport {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [String]
        ${Project},
        [Parameter(Mandatory = $true)]
        [String]
        ${OutFolder},
        [Parameter(Mandatory = $false)]
        [String]
        ${Mappings},
        [Parameter(Mandatory = $false)]
        [ValidateSet('CSV', 'Excel')]
        [String]
        ${FileType} = [ReportFileType]::CSV,
        [Parameter(Mandatory = $false)]
        [Int32]
        ${TimeZoneOffset},
        [Parameter(Mandatory = $false)]
        [ValidateSet('ErrorObjects', 'WarningObjects', 'SuccessfulObjects', 'SkippedObjects', 'FilterOutObjects', 'NotFoundObjects', 'ErrorIgnoredObjects', 'UnsupportedObjects')]
        [String[]]
        ${Include}
    )

    Process {
        'Calling method: Export-FlyM365GroupMigrationReport' | Write-Debug
        $PSBoundParameters | Out-DebugParameter | Write-Debug
        Try {
            $targetProject = Get-FlyProjectByName -ProjectName $Project -PlatformType 'M365Group'
            $targetMappings = Get-FlyM365GroupMappings -ProjectId $targetProject.Id -Mappings $Mappings
            #Construct the settings of report job
            $reportSetting = [PSCustomObject]@{
                "includeMappingSummary" = $true
                "includeDetails"        = $false
                "reportItemStatus"      = @()
                "reportFileType"        = $FileType
                "isSelectAll"           = $true
                "mappingIds"            = @()
                "timeZone"              = $TimeZoneOffset
            }
            if ($Include -and $Include.Count -gt 0) {
                $reportSetting.includeDetails = $true
                $reportSetting.reportItemStatus = $Include -replace "ErrorObjects", "FailedObjects"
            }
            if ($targetMappings -and @($targetMappings).Count -gt 0) {
                $mappingIds = $targetMappings | Select-Object -Property Id | ForEach-Object { "$($_.Id)" }
                $reportSetting.mappingIds = @($mappingIds)
                $reportSetting.isSelectAll = $false
            }
            #Trigger the migration report job and get the job id
            $jobId = Start-FlyM365GroupReportJob -ProjectId $targetProject.Id -GenerateReportSettingsModel $reportSetting
            #Monitor the job status and download the report file when job is finished
            while ($true) {
                Write-Host 'The report generation job is running.' -ForegroundColor Green
                Start-Sleep -Seconds 60
                $job = Get-FlyReportJobs -RequestBody @($jobId)
                $jobStatus = $job.data[0].Status
                if ($jobStatus -eq [MappingJobStatus]::Finished -or $jobStatus -eq [MappingJobStatus]::FinishedWithException) {
                    Write-Host 'The report generation job is finished.' -ForegroundColor Green
                    $result = Get-FlyReportUrl -JobId $jobId
                    if ($null -ne $result.ReportUrl) {
                        $fileName = Split-Path $([uri]$result.ReportUrl).AbsolutePath -Leaf
                        $filePath = Join-Path -Path $OutFolder -ChildPath $fileName
                        If (-not (Test-Path $OutFolder)) {
                            # Folder does not exist, create it
                            [void](New-Item -Path $OutFolder -ItemType Directory)
                        }
                        Invoke-WebRequest -URI $result.ReportUrl -OutFile $filePath -UseBasicParsing
                        Write-Host 'Successfully downloaded the job report. Report path:' $filePath -ForegroundColor Green
                    }
                    break;
                }
                elseif ($jobStatus -eq [MappingJobStatus]::Failed -or $jobStatus -eq [MappingJobStatus]::Stopped) {
                    throw ('The report generation job is {0} with id {1}' -f [MappingJobStatus].GetEnumName($jobStatus), $job.data[0].JobName)
                }
            }
        }
        Catch {
            Write-Host 'Failed to generate the migration report.' -ForegroundColor Red
            ErrorDetail -Error $_
            throw
        }
    }
}

<#
.SYNOPSIS
 
Start a pre-scan job against the selected project mappings.
 
.DESCRIPTION
 
Start a pre-scan job against the selected project mappings.
 
.PARAMETER Project
Specify the name of the project to run job.
 
.PARAMETER Mappings
Specify the csv file to filter specific project mappings to run job, for all mappings of the project if not specified.
 
.OUTPUTS
 
A Boolean value indicates whether the work has started successfully
#>

function Start-FlyM365GroupPreScan {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [String]
        ${Project},
        [Parameter(Mandatory = $false)]
        [String]
        ${Mappings}
    )

    Process {
        'Calling method: Start-FlyM365GroupPreScan' | Write-Debug
        $PSBoundParameters | Out-DebugParameter | Write-Debug
        Try {
            $targetProject = Get-FlyProjectByName -ProjectName $Project
            $targetMappings = Get-FlyM365GroupMappings -ProjectId $targetProject.Id -Mappings $Mappings
            #Construct the settings of the migration job
            $jobSettings = [PSCustomObject]@{
                "type"        = [MappingJobType]::Assessment
                "isSelectAll" = $true
                "mappingIds"  = @()
            }
            if ($targetMappings -and @($targetMappings).Count -gt 0) {
                $mappingIds = $targetMappings | Select-Object -Property Id | ForEach-Object { "$($_.Id)" }
                $jobSettings.mappingIds = @($mappingIds)
                $jobSettings.isSelectAll = $false
            }
            $result = Start-FlyM365GroupPreScanJob -ProjectId $targetProject.Id -ProjectMappingOperationModel $jobSettings
            if ($result) {
                Write-Host 'The job has started.' -ForegroundColor Green
            }
        }
        Catch {
            Write-Host 'Failed to start the job.' -ForegroundColor Red
            ErrorDetail -Error $_
            throw
        }
    }
}

<#
.SYNOPSIS
 
Start a verify job against the selected project mappings.
 
.DESCRIPTION
 
Start a verify job against the selected project mappings.
 
.PARAMETER Project
Specify the name of the project to run job.
 
.PARAMETER Mappings
Specify the csv file to filter specific project mappings to run job, for all mappings of the project if not specified.
 
.OUTPUTS
 
A Boolean value indicates whether the job has started successfully
#>

function Start-FlyM365GroupVerification {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [String]
        ${Project},
        [Parameter(Mandatory = $false)]
        [String]
        ${Mappings}
    )

    Process {
        'Calling method: Start-FlyM365GroupVerification' | Write-Debug
        $PSBoundParameters | Out-DebugParameter | Write-Debug
        Try {
            $targetProject = Get-FlyProjectByName -ProjectName $Project
            $targetMappings = Get-FlyM365GroupMappings -ProjectId $targetProject.Id -Mappings $Mappings
            #Construct the settings of the migration job
            $jobSettings = [PSCustomObject]@{
                "type"        = [MappingJobType]::Validation
                "isSelectAll" = $true
                "mappingIds"  = @()
            }
            if ($targetMappings -and @($targetMappings).Count -gt 0) {
                $mappingIds = $targetMappings | Select-Object -Property Id | ForEach-Object { "$($_.Id)" }
                $jobSettings.mappingIds = @($mappingIds)
                $jobSettings.isSelectAll = $false
            }
            $result = Start-FlyM365GroupVerificationJob -ProjectId $targetProject.Id -ProjectMappingOperationModel $jobSettings
            if ($result) {
                Write-Host 'The job has started.' -ForegroundColor Green
            }
        }
        Catch {
            Write-Host 'Failed to start the job.' -ForegroundColor Red
            ErrorDetail -Error $_
            throw
        }
    }
}

<#
.SYNOPSIS
 
Start a migration job against the selected project mappings.
 
.DESCRIPTION
 
Start a migration job against the selected project mappings.
 
.PARAMETER Project
Specify the name of the project to run job.
 
.PARAMETER Mode
Specify the mode of the migration job, use Tab for available values.
 
.PARAMETER Mappings
Specify the csv file to filter specific project mappings to run job, for all mappings of the project if not specified.
 
.PARAMETER ScheduleTime
Specify the time when you want the job to be scheduled. By default the job will be executed as soon as possible, optional for no schedule.
 
.OUTPUTS
 
A Boolean value indicates whether the job has started successfully
#>

function Start-FlyM365GroupMigration {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [String]
        ${Project},
        [Parameter(Mandatory = $true)]
        [ValidateSet('FullMigration', 'IncrementalMigration', 'ErrorOnly', 'MembershipOnly')]
        [String]
        ${Mode},
        [Parameter(Mandatory = $false)]
        [String]
        ${Mappings},
        [Parameter(Mandatory = $false)]
        [Datetime]
        ${ScheduleTime}
    )

    Process {
        'Calling method: Start-FlyM365GroupMigration' | Write-Debug
        $PSBoundParameters | Out-DebugParameter | Write-Debug
        Try {
            $targetProject = Get-FlyProjectByName -ProjectName $Project
            $targetMappings = Get-FlyM365GroupMappings -ProjectId $targetProject.Id -Mappings $Mappings
            #Construct the settings of the migration job
            $jobSettings = [PSCustomObject]@{
                "type"          = $Mode
                "scheduledTime" = 0
                "isSelectAll"   = $true
                "mappingIds"    = @()
            }
            if ($targetMappings -and @($targetMappings).Count -gt 0) {
                $mappingIds = $targetMappings | Select-Object -Property Id | ForEach-Object { "$($_.Id)" }
                $jobSettings.mappingIds = @($mappingIds)
                $jobSettings.isSelectAll = $false
            }
            if ($ScheduleTime) {
                $jobSettings.scheduledTime = $ScheduleTime.Ticks
            }
            $result = Start-FlyM365GroupMigrationJob -ProjectId $targetProject.Id -MigrationJobSettingsModel $jobSettings
            if ($result) {
                Write-Host 'The job has started.' -ForegroundColor Green
            }
        }
        Catch {
            Write-Host 'Failed to start the job.' -ForegroundColor Red
            ErrorDetail -Error $_
            throw
        }
    }
}