Rework some logic

This commit is contained in:
pinguinfuss 2023-03-09 20:25:26 +01:00
parent 9ed86fd126
commit 3105a3230a

View File

@ -88,64 +88,53 @@ function Disconnect-From {
[switch]$Force [switch]$Force
) )
begin {} begin {
# Set sane defaults for Progress, ErrorAction and InformationPreference
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'
$InformationPreference = 'Continue'
}
process { process {
switch -Regex ($Type) { switch -Regex ($Type) {
'CiscoUcs' { 'CiscoUcs' {
try { try {
Disconnect-Ucs -Ucs $RemoteHost $null = Disconnect-Ucs -Ucs $RemoteHost
} }
catch { catch {
# Write a error message to the log. # Write a error message to the log.
$MessageParams = @{ Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
Message = 'Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type
ErrorAction = 'Stop'
}
Write-Error @MessageParams
} }
} }
'CiscoUCSCentral' { 'CiscoUCSCentral' {
try { try {
$handle = Connect-UcsCentral -Name $RemoteHost -Credential $creds -NotDefault $null = Disconnect-UcsCentral -Name $RemoteHost
$ExecutionContext.SessionState.PSVariable.Set('DefaultUcsCentral', $handle) $ExecutionContext.SessionState.PSVariable.Set('DefaultUcsCentral', $null)
} }
catch { catch {
$MessageParams = @{ Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
Message = 'Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type
ErrorAction = 'Stop'
}
Write-Error @MessageParams
} }
} }
'ExchangeHTTP*' { 'ExchangeHTTP*' {
try { try {
Get-Variable -Name 'PSExchangeRemote' -Scope 'Global' -ErrorAction 'Stop' Get-Variable -Name 'PSExchangeRemote' -Scope 'Global'
Remove-PSSession -Session $Global:PSExchangeRemote -ErrorAction 'Stop' Remove-PSSession -Session $global:PSExchangeRemote
} }
catch { catch {
$MessageParams = @{ Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
Message = 'Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type
ErrorAction = 'Stop'
}
Write-Error @MessageParams
} }
} }
# Check for an existing WinSCP Session var # Check for an existing WinSCP Session var
'FTP' { 'FTP' {
if ($Global:WinSCPSession.Opened) { if ($global:WinSCPSession.Opened) {
Remove-WinSCPSession -WinSCPSession $Global:WinSCPSession Remove-WinSCPSession -WinSCPSession $global:WinSCPSession
} }
else { else {
$MessageParams = @{ Write-Error -Message 'There is no open WinSCP Session'
Message = 'There is no open WinSCP Session'
ErrorAction = 'Stop'
}
Write-Error @MessageParams
} }
} }
@ -153,86 +142,66 @@ function Disconnect-From {
# So we go ahead and clear the CurrentNcController variable. # So we go ahead and clear the CurrentNcController variable.
'NetAppFAS' { 'NetAppFAS' {
try { try {
$MessageParams = @{ $m = 'Setting {0} to $null, which will disconnect NetAppFAS' -f $global:CurrentNcController
Message = ( Write-Verbose -Message $m
'Setting {0} to `$null, which will disconnect NetAppFAS' -f $Global:CurrentNcController $global:CurrentNcController = $null
)
ErrorAction = 'Continue'
}
Write-Verbose @MessageParams
$Global:CurrentNcController = $null
} }
catch { catch {
# Write a error message to the log. # Write a error message to the log.
$MessageParams = @{ Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
Message = 'Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type
ErrorAction = 'Stop'
}
Write-Error @MessageParams
} }
} }
'SCP' { 'SCP' {
if ($Global:WinSCPSession.Opened) { if ($global:WinSCPSession.Opened) {
Remove-WinSCPSession -WinSCPSession $Global:WinSCPSession Remove-WinSCPSession -WinSCPSession $global:WinSCPSession
} }
else { else {
$MessageParams = @{ Write-Error -Message 'There is no open WinSCP Session'
Message = 'There is no open WinSCP Session'
ErrorAction = 'Stop'
}
Write-Error @MessageParams
} }
} }
'VMware' { 'VMware' {
# Construct the splatting for Disconnect-VIServer
$params = @{
Server = $RemoteHost
Confirm = $false
}
if ($PSBoundParameters.ContainsKey('Force')) {
$params.Add('Force', $true)
}
try { try {
if ($Force) { $null = Disconnect-VIServer @params
Disconnect-VIServer -Server $RemoteHost -Confirm:$false -ErrorAction 'Stop' -Force:$true
}
else {
Disconnect-VIServer -Server $RemoteHost -Confirm:$false -ErrorAction 'Stop'
}
} }
catch { catch {
# Write a error message to the log. # Write a error message to the log.
$MessageParams = @{ Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
Message = 'Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type
ErrorAction = 'Stop'
}
Write-Error @MessageParams
} }
} }
'VMwareCisServer' { 'VMwareCisServer' {
try { try {
if ($Force) { if ($Force) {
Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction 'Stop' -Force:$true $null = Disconnect-CisServer -Server $RemoteHost -Confirm:$false -Force:$true
} }
else { else {
Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction 'Stop' $null = Disconnect-CisServer -Server $RemoteHost -Confirm:$false
} }
} }
catch { catch {
# Write a error message to the log. # Write a error message to the log.
$MessageParams = @{ Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
Message = 'Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type
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 disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
Message = 'Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type
ErrorAction = 'Stop'
}
Write-Error @MessageParams
} }
} }
} }