Public/Test-ExchangeOnlineLogin.ps1

Function Test-ExchangeOnlineLogin {
    <#
.SYNOPSIS
   Tests connectivity to Exchange Online.
.DESCRIPTION
    Tests connectivity to Exchange Online. If requested, the function will also attempt to import the module and connect to the server.
.NOTES
    File Name : SDT-Commons.ps1
    Author : Jesse Newall (Jesse.Newall@dbschenker.com) / Mark J. Gunnett (mark.gunnett@dbschenker.com)
    Prerequisite : PowerShell V5
    Copyright 2018 - DB Schenker
    Last Edited: 12/12/2018 @ 18:25:59
.LINK
    New-ExoPSSession
.EXAMPLE
    Test-ExchangeOnlineLogin
    Tests whether the exchange online console is connected.
.EXAMPLE
    Test-ExchangeOnlineLogin -Connect
    Tests whether the exchange online console is connected. If it's not connected, it attempts to import the module and connect.
#>

    param(
        [Parameter(Mandatory = $false)]
        [switch]$Connect
    )
    $checkEXOPSSession = Get-PSSession
    # If ExOPssesion is running, ignore module import. If not, import module.
    if ($checkEXOPSSession.state -eq "Opened" -and $checkEXOPSSession.configurationname -eq "Microsoft.Exchange") {
        Write-Verbose "Exchange Online Powershell already running. No need to reconnect."
        return $true
    }
    # If requested to connect, execute module import and connect.
    if ($Connect) {
        Write-Warning "Not connected to Exchange Online Powershell... please sign in to complete authentication and import module."
        $PSExoPowershellModuleRoot = (Get-ChildItem -Path $env:userprofile -Filter CreateExoPSSession.ps1 -Recurse -ErrorAction SilentlyContinue -Force | Select-Object -Last 1).DirectoryName
        $ExoPowershellModule = "Microsoft.Exchange.Management.ExoPowershellModule.dll";
        $ModulePath = Join-Path -Path $PSExoPowershellModuleRoot -ChildPath $ExoPowershellModule
        Write-Debug "Checking Module Path for ExO Module: $ModulePath"
        if (-not (Test-Path $ModulePath)) {
            $Error_Missing_Module = "Exchange Online Powershell Module is missing. Please refer to the Exchange Online Powershell with MFA Documentation. {0}"
            $Error_Module_URL = "https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/mfa-connect-to-exchange-online-powershell?view=exchange-ps"
            Throw ($Error_Missing_Module -f $Error_Module_URL)
        }
        Import-Module $ModulePath
        Write-Verbose "Module imported successfully."
        try {
            $Office365PSSession = New-ExoPSSession -ConnectionUri "https://outlook.office365.com/powershell-liveid/"
            Import-PSSession $Office365PSSession
        }
        catch {
            $Error_Login_Canceled = "Exception of type 'Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException' was thrown."
            if ($_ -like $Error_Login_Canceled) {
                Write-Verbose "User canceled login, returning false for connectivity status."
                return $false
            }
            else {
                Write-Debug "Unknown error thrown. Passing up the chain."
                throw
            }
        }
        return $true
    }
    else {
        return $false
    }
}