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