PSCredentialStore/src/Connection/Disconnect-From.ps1

220 lines
6.9 KiB
PowerShell
Raw Normal View History

function Disconnect-From {
<#
.SYNOPSIS
2023-03-09 20:27:23 +01:00
Terminates a session established with Connect-To.
.DESCRIPTION
2023-03-09 20:27:23 +01:00
Terminates a session established with Connect-To.
.PARAMETER RemoteHost
Specify the remote endpoint, whose session you would like to terminate.
.PARAMETER Type
Specify the host type of the target. Currently implemented targets are:
- CiscoUcs
- CiscoUcsCentral
- ExchangeHTTP
- ExchangeHTTPS
2023-03-09 20:18:57 +01:00
- FTP
- NetAppFAS
2023-03-09 20:34:05 +01:00
- NetAppSGWS
- SCP
2023-03-09 20:18:57 +01:00
- VMware
- VMwareCisServer
.PARAMETER Force
Force the disconnect, even if the disconnect would fail.
.INPUTS
[None]
.OUTPUTS
[None]
.EXAMPLE
2023-03-09 20:18:57 +01:00
Disconnect-From -RemoteHost 'ucs.myside.local' -Type CiscoUcs
.EXAMPLE
2023-03-09 20:18:57 +01:00
Disconnect-From -RemoteHost 'ftp.myside.local' -Type FTP
.EXAMPLE
2023-03-09 20:18:57 +01:00
Disconnect-From -RemoteHost 'fas.myside.local' -Type NetAppFAS
.EXAMPLE
2023-03-09 20:18:57 +01:00
Disconnect-From -RemoteHost 'esx01.myside.local' -Type VMware
.EXAMPLE
2023-03-09 20:18:57 +01:00
Disconnect-From -RemoteHost 'esx01.myside.local' -Type VMware -Force:$True
.EXAMPLE
2023-03-09 20:18:57 +01:00
Disconnect-From -RemoteHost 'vcenter.myside.local' -Type CisServer
2017-12-22 08:10:37 +01:00
.EXAMPLE
2023-03-09 20:18:57 +01:00
Disconnect-From -RemoteHost 'exchange01.myside.local' -Type ExchangeHTTP
2017-12-22 08:10:37 +01:00
.EXAMPLE
2023-03-09 20:18:57 +01:00
Disconnect-From -RemoteHost 'exchange01.myside.local' -Type ExchangeHTTPS
#>
[CmdletBinding()]
2023-03-09 20:27:38 +01:00
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSAvoidGlobalVars',
'',
Justification = 'Wrapping existing global vars from external modules'
)]
2023-03-09 20:27:38 +01:00
param (
[Parameter(Mandatory = $true)]
2023-03-09 20:27:38 +01:00
[string] $RemoteHost,
[Parameter(Mandatory = $true)]
[ValidateSet(
'CiscoUcs',
'CiscoUcsCentral',
2023-03-09 20:18:57 +01:00
'ExchangeHTTP',
'ExchangeHTTPS',
'FTP',
'NetAppFAS',
2023-03-09 20:34:05 +01:00
'NetAppSGWS',
2023-03-09 20:18:57 +01:00
'SCP',
'VMware',
2023-03-09 20:18:57 +01:00
'VMwareCisServer'
)]
2023-03-09 20:27:38 +01:00
[string] $Type,
[Parameter(Mandatory = $false)]
2023-03-09 20:27:38 +01:00
[switch] $Force
)
2023-03-09 20:25:26 +01:00
begin {
# Set sane defaults for Progress, ErrorAction and InformationPreference
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'
$InformationPreference = 'Continue'
}
process {
switch -Regex ($Type) {
2023-03-09 20:18:57 +01:00
'CiscoUcs' {
try {
2023-03-09 20:25:26 +01:00
$null = Disconnect-Ucs -Ucs $RemoteHost
}
catch {
# Write a error message to the log.
2023-03-09 20:25:26 +01:00
Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
}
}
2023-03-09 20:18:57 +01:00
'CiscoUCSCentral' {
try {
2023-03-09 20:25:26 +01:00
$null = Disconnect-UcsCentral -Name $RemoteHost
$ExecutionContext.SessionState.PSVariable.Set('DefaultUcsCentral', $null)
2023-03-09 20:18:57 +01:00
}
catch {
2023-03-09 20:25:26 +01:00
Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
}
2023-03-09 20:18:57 +01:00
}
2023-03-09 20:18:57 +01:00
'ExchangeHTTP*' {
try {
2023-03-09 20:25:26 +01:00
Get-Variable -Name 'PSExchangeRemote' -Scope 'Global'
Remove-PSSession -Session $global:PSExchangeRemote
2023-03-09 20:18:57 +01:00
}
catch {
2023-03-09 20:25:26 +01:00
Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
}
}
2023-03-09 20:18:57 +01:00
# Check for an existing WinSCP Session var
2023-03-09 20:18:57 +01:00
'FTP' {
2023-03-09 20:25:26 +01:00
if ($global:WinSCPSession.Opened) {
Remove-WinSCPSession -WinSCPSession $global:WinSCPSession
}
else {
2023-03-09 20:25:26 +01:00
Write-Error -Message 'There is no open WinSCP Session'
}
}
2023-03-09 20:18:57 +01:00
# DataONTAP doesn't have a CmdLet `Disconnect-NcController`.
# So we go ahead and clear the CurrentNcController variable.
2023-03-09 20:18:57 +01:00
'NetAppFAS' {
try {
2023-03-09 20:25:26 +01:00
$m = 'Setting {0} to $null, which will disconnect NetAppFAS' -f $global:CurrentNcController
Write-Verbose -Message $m
$global:CurrentNcController = $null
}
2017-12-22 08:10:37 +01:00
catch {
2023-03-09 20:34:05 +01:00
# Write a error message to the log.
Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
}
}
'NetAppSGWS' {
try {
$null = Disconnect-SgwServer -Server $RemoteHost
$null = Remove-Variable -Name CurrentSgwServer -Scope Global -ErrorAction 'SilentlyContinue'
}
catch {
# Write a error message to the log.
2023-03-09 20:25:26 +01:00
Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
}
}
2023-03-09 20:18:57 +01:00
'SCP' {
2023-03-09 20:25:26 +01:00
if ($global:WinSCPSession.Opened) {
Remove-WinSCPSession -WinSCPSession $global:WinSCPSession
2023-03-09 20:18:57 +01:00
}
else {
2023-03-09 20:25:26 +01:00
Write-Error -Message 'There is no open WinSCP Session'
}
2017-12-22 08:10:37 +01:00
}
2023-03-09 20:18:57 +01:00
'VMware' {
2023-03-09 20:25:26 +01:00
# Construct the splatting for Disconnect-VIServer
$params = @{
Server = $RemoteHost
Confirm = $false
}
if ($PSBoundParameters.ContainsKey('Force')) {
$params.Add('Force', $true)
}
try {
2023-03-09 20:25:26 +01:00
$null = Disconnect-VIServer @params
}
2023-03-09 20:18:57 +01:00
catch {
2023-03-09 20:18:57 +01:00
# Write a error message to the log.
2023-03-09 20:25:26 +01:00
Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
}
}
2023-03-09 20:18:57 +01:00
'VMwareCisServer' {
try {
2023-03-09 20:18:57 +01:00
if ($Force) {
2023-03-09 20:25:26 +01:00
$null = Disconnect-CisServer -Server $RemoteHost -Confirm:$false -Force:$true
2023-03-09 20:18:57 +01:00
}
else {
2023-03-09 20:25:26 +01:00
$null = Disconnect-CisServer -Server $RemoteHost -Confirm:$false
}
}
2023-03-09 20:18:57 +01:00
catch {
# Write a error message to the log.
2023-03-09 20:25:26 +01:00
Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
}
}
2023-03-09 20:18:57 +01:00
default {
# Write a error message to the log.
2023-03-09 20:25:26 +01:00
Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
}
}
}
}