diff --git a/src/Connection/Connect-To.ps1 b/src/Connection/Connect-To.ps1 index 8b2a88e..af35c63 100644 --- a/src/Connection/Connect-To.ps1 +++ b/src/Connection/Connect-To.ps1 @@ -191,16 +191,12 @@ function Connect-To { switch ($Type) { 'CiscoUcs' { try { - $handle = Connect-Ucs -Name $RemoteHost -Credential $creds -ErrorAction 'Stop' -NotDefault + $handle = Connect-Ucs -Name $RemoteHost -Credential $creds -NotDefault $ExecutionContext.SessionState.PSVariable.Set('DefaultUcs', $handle) } catch { - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } 'CiscoUcsCentral' { @@ -210,11 +206,7 @@ function Connect-To { } catch { - $MessageParams = @{ - Message = ('Unable to connect to {0} using {1}' -f $RemoteHost, $Type) - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using {1}' -f $RemoteHost, $Type) } } 'ExchangeHTTP' { @@ -223,7 +215,6 @@ function Connect-To { ConnectionURI = 'http://{0}/powershell' -f $RemoteHost ConfigurationName = 'Microsoft.Exchange' Credential = $creds - ErrorAction = 'Stop' } $global:PSExchangeRemote = New-PSSession @ConnectionParams # ScriptAnalyzer issue (unused var) workaround. @@ -231,11 +222,7 @@ function Connect-To { } catch { # Write a error message to the log. - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } 'ExchangeHTTPS' { @@ -244,17 +231,12 @@ function Connect-To { ConnectionURI = 'https://{0}/powershell' -f $RemoteHost ConfigurationName = 'Microsoft.Exchange' Credential = $creds - ErrorAction = 'Stop' } $global:PSExchangeRemote = New-PSSession @ConnectionParams } catch { # Write a error message to the log. - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } 'FTP' { @@ -270,19 +252,13 @@ function Connect-To { $global:WinSCPSession = New-WinSCPSession -SessionOption $FTPSessionOption } catch { - throw 'Could not connect to {0} using {1} protocol!' -f $RemoteHost, $Type + Write-Error -Message ('Could not connect to {0} using {1} protocol!' -f $RemoteHost, $Type) } + # Check the Connection State - if (!($WinSCPSession.Opened)) { + if (-not $WinSCPSession.Opened) { # Check the connection state and find out if the session is still open. - $MessageParams = @{ - Message = ( - ('Connection to {0} using Type {1} ' -f $RemoteHost, $Type) + - 'was established. But now it seems to be lost!' - ) - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Connection to {0} using Type {1} was established. But now it seems to be lost!' -f $RemoteHost, $Type) } } 'NetAppFAS' { @@ -292,11 +268,7 @@ function Connect-To { catch { # Write a error message to the log. - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } 'VMware' { @@ -306,31 +278,22 @@ function Connect-To { catch { # Write a error message to the log. - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } 'VMwareCisServer' { try { if ($PassThru.IsPresent) { - Connect-CisServer -Server $RemoteHost -Credential $creds -ErrorAction Stop + Connect-CisServer -Server $RemoteHost -Credential $creds } else { - Connect-CisServer -Server $RemoteHost -Credential $creds -ErrorAction Stop | Out-Null + $null = Connect-CisServer -Server $RemoteHost -Credential $creds } - } catch { # Write a error message to the log. - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } 'SCP' { @@ -349,32 +312,17 @@ function Connect-To { } catch { # Write a error message to the log. - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } # Check the Connection State if (!($WinSCPSession.Opened)) { # Check the connection state and find out if the session is still open. - $MessageParams = @{ - Message = ( - ('Connection to {0} using Type {1} was established. ' -f $RemoteHost, $Type) + - 'But now it seems to be lost!' - ) - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Connection to {0} using Type {1} was established. But now it seems to be lost!' -f $RemoteHost, $Type) } } default { # Write a error message to the log. - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } }