Public/Get-MigrationStatus.ps1

Function Get-MigrationStatus {
    <#
.SYNOPSIS
    Get-MigrationStatus displays errors for a failed migration job to the Online Archive.

.DESCRIPTION
    A more detailed description of what the function does.

.PARAMETER UserPrincipalName
    The parameter UserPrincipalName is used to find a user's migration job on Exchange Online.

.PARAMETER IncludeReport
    The switch parameter IncludeReport when used exports a CSV file of a user researched.

.EXAMPLE
    The example below does blah
    PS C:\> <Example>
    
.NOTES
    Author: <Name>
    Last Edit: yyyy-mm-dd
    Version 1.0 - initial release of blah
    Version 1.1 - update for blah

#>

    [CmdletBinding()]
    Param (
        
        [Parameter (Mandatory = $True, ValueFromPipeline = $True)]
        [ValidateNotNullOrEmpty()]
        [string[]]$UserPrincipalName,
        [Parameter (Mandatory = $False)]
        [switch]$IncludeReport,
        [Parameter (Mandatory = $False)]
        [switch]$ExportReport
        
    )

    Begin {
        # Start of the BEGIN block.
        Write-Verbose -Message "Entering the BEGIN block [$($MyInvocation.MyCommand.CommandType): $($MyInvocation.MyCommand.Name)]."
        # Turn the verbose preferences on.
        $VerbosePreference = "Continue"
        # Ensure connection to Exchange Online
        Test-ExchangeOnlineLogin -Connect
        # Array for failed PST information for user.
        $CombinedUserInfo = @()
        #Get the date for exporting the report DD/MM/YYYY
        $TodaysDate = Get-Date -Format dd-MM-yyyy
    }   # End Begin block

    Process {
        # Start of PROCESS block.
        Write-Verbose -Message "Entering the PROCESS block [$($MyInvocation.MyCommand.CommandType): $($MyInvocation.MyCommand.Name)]."
        # For each user in the parameter UserPrincipalName, get user properties.
        Foreach ($User in $UserPrincipalName) {
            Try {
                # Get the user's alias and upn.
                $UserInfo = Get-Mailbox $User | Select-Object Alias, UserPrincipalName
            }
            Catch {
                $_.Exception.Message
            }        
            Try {
                # Check if the user has an import request or not.
                Write-Verbose "Attaining info for $User."
                $CheckIfImport = Get-MailboxImportRequest -Mailbox $User
                # If the user does not have an import job created, then say so in the custom object.
                If (!$CheckIfImport) {
                    $Properties = @{
                        # Userprincipalname
                        UPN     = $UserInfo.UserPrincipalName
                        # Message if no job can be found
                        Message = "Cannot find an import job for the user. Check if RealMigrator installed. If installed, check the RealMigrator client for failure to upload the PST to the blob."
                    }
                    $CombinedUserInfo += New-Object PSObject -Property $Properties                   
                }
                Else {
                    Foreach ($Job in $CheckIfImport) {                      
                        If ($IncludeReport -and $Job) {                       
                            # If the user has an import job and if the include report switch is activated.
                            $Job | Get-MailboxImportRequestStatistics -IncludeReport | Format-List | Foreach-Object {
                                $Properties = @{
                                    # Userprincipalname
                                    UPN                = $UserInfo.UserPrincipalName
                                    # Name of the job that includes the PST name.
                                    'Job Name'         = $_.Name
                                    # Status of the failed job.
                                    Status             = $_.Status
                                    # When the job was created.
                                    StartDate          = $_.StartTimestamp
                                    # Percent Complete
                                    'Percent Complete' = $_.PercentComplete
                                    # The error message received.
                                    Message            = $_.Message
                                    # Report
                                    Report             = $_.Report     
                                }                    
                            }
                        }
                        If ($Job) {
                            # If the user does have a job.
                            $Job | Get-MailboxImportRequestStatistics | Foreach-Object {
                                $Properties = @{
                                    # Userprincipalname
                                    UPN                = $UserInfo.UserPrincipalName
                                    # Name of the job that includes the PST name.
                                    'Job Name'         = $_.Name
                                    # Status of the failed job.
                                    Status             = $_.Status
                                    # When the job was created.
                                    StartDate          = $_.StartTimestamp
                                    # Percent Complete
                                    'Percent Complete' = $_.PercentComplete
                                    # The error message received.
                                    Message            = $_.Message
                                }
                            }
                        }
                        $CombinedUserInfo += New-Object PSObject -Property $Properties                                                    
                    }
                } 
            }
            Catch {
                $_.Exception.Message
            }
        }
                        
    } # End of PROCESS block.

    End {
        # Start of END block.
        Write-Verbose -Message "Entering the END block [$($MyInvocation.MyCommand.CommandType): $($MyInvocation.MyCommand.Name)]."
        # Check if the IncludeReport switch is included.
        If ($ExportReport) {
            # If the IncludeReport switch is included, export the CSV file.
            Write-Verbose "Displaying results and exporting CSV file."
            $CombinedUserInfo | Select-Object UPN, 'Job Name', StartDate, 'Percent Complete', Status, Message, Report | Export-CSV -Path "$env:UserProfile\OneDrive - Schenker AG\RealMigrator_UserErrorReport_$TodaysDate.csv" -NoTypeInformation -ErrorAction Stop
            # Display the results from the custom object.
            $CombinedUserInfo | Select-Object UPN, 'Job Name', StartDate, 'Percent Complete', Status, Message, Report   
            # If the custom object is not included
        }
        Else {
            Write-Verbose "Displaying results..."
            $CombinedUserInfo | Select-Object UPN, 'Job Name', StartDate, 'Percent Complete', Status, Message, Report      
        }       
        # Removing the Exchange Online PowerShell Session
        #Write-Verbose -Message "Removing Exchange Online PowerShell Session"
        # Removing only the session that equals Microsoft Exchange.
        #$RemoveEXOPSSession = Get-PSSession |Where-Object {$_.ConfigurationName -eq "Microsoft.Exchange"} | Remove-PSSession
 
    } # End of the END Block.
} # End Function