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

Code before:
        [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
function Invoke-RevertExchangeConnectServerToPrimary {
    [CmdletBinding()]
    param(
        [scriptblock]$CatchActionFunction
    )

Review comment (performance):
```suggestion
                Write-Verbose "Calling Import-Module as we already have it cached."
```

Code after:
        [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
function Invoke-RevertExchangeConnectServerToPrimary {
    [CmdletBinding()]
    param(
        [scriptblock]$CatchActionFunction
    )