Repository: microsoft/CSS-Exchange
File: Shared/ExchangeSessionFunctions.ps1 (PowerShell)

Code before:
. $PSScriptRoot\Invoke-CatchActionError.ps1
function Switch-ExchangeConnectedServer {
    [CmdletBinding()]
    [OutputType([bool])]
    param(
        [Parameter(Mandatory = $true)]
        [string]$ServerFqdn,
        [scriptblock]$CatchActionFunction
    )
    begin {
        Write-Verbose "Calling: $($MyInvocation.MyCommand)"
        if ($null -eq $Script:CachedExchangePsSession) {
            $Script:CachedExchangePsSession = [PSCustomObject]@{
                PrimarySession   = $remoteSession
                ActiveSessionKey = $remoteSession.ComputerName
                Sessions         = @{
                    $remoteSession.ComputerName = $remoteSession
                }
            }
        }
        $result = $false
    }
    process {
        try {
            if (-not ($Script:CachedExchangePsSession.Sessions.ContainsKey($ServerFqdn))) {
                Write-Verbose "Need to use Connect-ExchangeServer to connect to $ServerFqdn"
                Connect-ExchangeServer $ServerFqdn -AllowClobber
                $Script:CachedExchangePsSession.ActiveSessionKey = $ServerFqdn
                $Script:CachedExchangePsSession.Sessions.Add(($remoteSession.ComputerName), $remoteSession)
            } elseif ($Script:CachedExchangePsSession.ActiveSessionKey -ne $ServerFqdn) {
                Write-Verbose "Load the module on the server as we already have it cached."
                $switchToSession = $Script:CachedExchangePsSession.Sessions[$ServerFqdn]
                $serverName = $switchToSession.ComputerName
                $modulePath = "$env:APPDATA\Microsoft\Exchange\RemotePowerShell\$serverName"
                Import-Module -Name $modulePath -ArgumentList $switchToSession -DisableNameChecking
                $Script:CachedExchangePsSession.ActiveSessionKey = $ServerFqdn
            } else {
                Write-Verbose "Already on active session for this server"
            }
            $result = $true
        } catch {
            Write-Verbose "Failed to Connect-ExchangeServer"
            Invoke-CatchActionError $CatchActionFunction
        }
    }
    end {
        return $result
    }
}

# Use this function at the end of the script to revert back to the primary server

Review comment (suggestion):
```suggestion
                Write-Verbose "Calling Connect-ExchangeServer to connect to $ServerFqdn"
```

"Need to use" makes it sounds like you're asking the user to do something.

Code after:
. $PSScriptRoot\Invoke-CatchActionError.ps1
function Switch-ExchangeConnectedServer {
    [CmdletBinding()]
    [OutputType([bool])]
    param(
        [Parameter(Mandatory = $true)]
        [string]$ServerFqdn,
        [scriptblock]$CatchActionFunction
    )
    begin {
        Write-Verbose "Calling: $($MyInvocation.MyCommand)"
        if ($null -eq $Script:CachedExchangePsSession) {
            $Script:CachedExchangePsSession = [PSCustomObject]@{
                PrimarySession   = $remoteSession
                ActiveSessionKey = $remoteSession.ComputerName
                Sessions         = @{
                    $remoteSession.ComputerName = $remoteSession
                }
            }
        }
        $result = $false
    }
    process {
        try {
            if (-not ($Script:CachedExchangePsSession.Sessions.ContainsKey($ServerFqdn))) {
                Write-Verbose "Calling Connect-ExchangeServer to connect to $ServerFqdn"
                Connect-ExchangeServer $ServerFqdn -AllowClobber
                $Script:CachedExchangePsSession.ActiveSessionKey = $ServerFqdn
                $Script:CachedExchangePsSession.Sessions.Add(($remoteSession.ComputerName), $remoteSession)
            } elseif ($Script:CachedExchangePsSession.ActiveSessionKey -ne $ServerFqdn) {
                Write-Verbose "Calling Import-Module as we already have it cached."
                $switchToSession = $Script:CachedExchangePsSession.Sessions[$ServerFqdn]
                $serverName = $switchToSession.ComputerName
                $modulePath = "$env:APPDATA\Microsoft\Exchange\RemotePowerShell\$serverName"
                Import-Module -Name $modulePath -ArgumentList $switchToSession -DisableNameChecking
                $Script:CachedExchangePsSession.ActiveSessionKey = $ServerFqdn
            } else {
                Write-Verbose "Already on active session for this server"
            }
            $result = $true
        } catch {
            Write-Verbose "Failed to Connect-ExchangeServer"
            Invoke-CatchActionError $CatchActionFunction
        }
    }
    end {
        return $result
    }
}

# Use this function at the end of the script to revert back to the primary server