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

Code before:
            $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
    )
    process {
        Write-Verbose "Calling: $($MyInvocation.MyCommand)"

        try {
            if ($null -ne $Script:CachedExchangePsSession) {
                $primarySession = $Script:CachedExchangePsSession.PrimarySession

                if ($Script:CachedExchangePsSession.ActiveSessionKey -ne $Script:CachedExchangePsSession.PrimarySession.ComputerName) {
                    Write-Verbose "Connect-ExchangeServer to primary session"
                    Connect-ExchangeServer $primarySession.ComputerName -AllowClobber
                    Remove-PSSession $remoteSession
                }

                foreach ($key in $Script:CachedExchangePsSession.Sessions.Keys) {
                    $session = $Script:CachedExchangePsSession.Sessions[$key]

                    if ($session -ne $primarySession) {
                        Write-Verbose "Removing session for $key"
                        Remove-PSSession $session
                    }
                }

                $Script:CachedExchangePsSession = $null
            }
        } catch {
            Invoke-CatchActionError $CatchActionFunction
        }
    }
}


Review comment (suggestion):
```suggestion
                    Write-Verbose "Calling Connect-ExchangeServer to primary session"
```

Code after:
            $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
    )
    process {
        Write-Verbose "Calling: $($MyInvocation.MyCommand)"

        try {
            if ($null -ne $Script:CachedExchangePsSession) {
                $primarySession = $Script:CachedExchangePsSession.PrimarySession

                if ($Script:CachedExchangePsSession.ActiveSessionKey -ne $Script:CachedExchangePsSession.PrimarySession.ComputerName) {
                    Write-Verbose "Calling Connect-ExchangeServer to primary session"
                    Connect-ExchangeServer $primarySession.ComputerName -AllowClobber
                    Remove-PSSession $remoteSession
                }

                foreach ($key in $Script:CachedExchangePsSession.Sessions.Keys) {
                    $session = $Script:CachedExchangePsSession.Sessions[$key]

                    if ($session -ne $primarySession) {
                        Write-Verbose "Removing session for $key"
                        Remove-PSSession $session
                    }
                }

                $Script:CachedExchangePsSession = $null
            }
        } catch {
            Invoke-CatchActionError $CatchActionFunction
        }
    }
}