remove the splattings around Write-Error

This commit is contained in:
pinguinfuss 2023-03-09 13:32:12 +01:00
parent b7ad5663dc
commit c272b30cb4

View File

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