209 lines
6.6 KiB
PowerShell
209 lines
6.6 KiB
PowerShell
function Disconnect-From {
|
|
<#
|
|
.SYNOPSIS
|
|
Terminates a session established with Connect-To using a CredentialStoreItem.
|
|
|
|
.DESCRIPTION
|
|
Terminates a session established with Connect-To using a CredentialStoreItem.
|
|
|
|
.PARAMETER RemoteHost
|
|
Specify the remote endpoint, whose session you would like to terminate.
|
|
|
|
.PARAMETER Identifier
|
|
Defaults to ''. Specify a string, which separates two CredentialStoreItems for the
|
|
same hostname.
|
|
|
|
.PARAMETER Type
|
|
Specify the host type of the target. Currently implemented targets are:
|
|
|
|
- CiscoUcs
|
|
- CiscoUcsCentral
|
|
- ExchangeHTTP
|
|
- ExchangeHTTPS
|
|
- FTP
|
|
- NetAppFAS
|
|
- SCP
|
|
- VMware
|
|
- VMwareCisServer
|
|
|
|
.PARAMETER Force
|
|
Force the disconnect, even if the disconnect would fail.
|
|
|
|
.INPUTS
|
|
[None]
|
|
|
|
.OUTPUTS
|
|
[None]
|
|
|
|
.EXAMPLE
|
|
Disconnect-From -RemoteHost 'ucs.myside.local' -Type CiscoUcs
|
|
|
|
.EXAMPLE
|
|
Disconnect-From -RemoteHost 'ftp.myside.local' -Type FTP
|
|
|
|
.EXAMPLE
|
|
Disconnect-From -RemoteHost 'fas.myside.local' -Type NetAppFAS
|
|
|
|
.EXAMPLE
|
|
Disconnect-From -RemoteHost 'esx01.myside.local' -Type VMware
|
|
|
|
.EXAMPLE
|
|
Disconnect-From -RemoteHost 'esx01.myside.local' -Type VMware -Force:$True
|
|
|
|
.EXAMPLE
|
|
Disconnect-From -RemoteHost 'vcenter.myside.local' -Type CisServer
|
|
|
|
.EXAMPLE
|
|
Disconnect-From -RemoteHost 'exchange01.myside.local' -Type ExchangeHTTP
|
|
|
|
.EXAMPLE
|
|
Disconnect-From -RemoteHost 'exchange01.myside.local' -Type ExchangeHTTPS
|
|
#>
|
|
|
|
[CmdletBinding()]
|
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
|
|
'PSAvoidGlobalVars',
|
|
'',
|
|
Justification = 'Wrapping existing global vars from external modules'
|
|
)]
|
|
param (
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$RemoteHost,
|
|
|
|
[Parameter(Mandatory = $true)]
|
|
[ValidateSet(
|
|
'CiscoUcs',
|
|
'CiscoUcsCentral',
|
|
'ExchangeHTTP',
|
|
'ExchangeHTTPS',
|
|
'FTP',
|
|
'NetAppFAS',
|
|
'SCP',
|
|
'VMware',
|
|
'VMwareCisServer'
|
|
)]
|
|
[string]$Type,
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
[switch]$Force
|
|
)
|
|
|
|
begin {
|
|
# Set sane defaults for Progress, ErrorAction and InformationPreference
|
|
$ProgressPreference = 'SilentlyContinue'
|
|
$ErrorActionPreference = 'Stop'
|
|
$InformationPreference = 'Continue'
|
|
}
|
|
|
|
process {
|
|
switch -Regex ($Type) {
|
|
'CiscoUcs' {
|
|
try {
|
|
$null = Disconnect-Ucs -Ucs $RemoteHost
|
|
}
|
|
|
|
catch {
|
|
# Write a error message to the log.
|
|
Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
|
|
}
|
|
}
|
|
|
|
'CiscoUCSCentral' {
|
|
try {
|
|
$null = Disconnect-UcsCentral -Name $RemoteHost
|
|
$ExecutionContext.SessionState.PSVariable.Set('DefaultUcsCentral', $null)
|
|
}
|
|
catch {
|
|
Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
|
|
}
|
|
}
|
|
|
|
'ExchangeHTTP*' {
|
|
try {
|
|
Get-Variable -Name 'PSExchangeRemote' -Scope 'Global'
|
|
Remove-PSSession -Session $global:PSExchangeRemote
|
|
}
|
|
catch {
|
|
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
|
|
}
|
|
else {
|
|
Write-Error -Message 'There is no open WinSCP Session'
|
|
}
|
|
}
|
|
|
|
# DataONTAP doesn't have a CmdLet `Disconnect-NcController`.
|
|
# So we go ahead and clear the CurrentNcController variable.
|
|
'NetAppFAS' {
|
|
try {
|
|
$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.
|
|
Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
|
|
}
|
|
}
|
|
|
|
'SCP' {
|
|
if ($global:WinSCPSession.Opened) {
|
|
Remove-WinSCPSession -WinSCPSession $global:WinSCPSession
|
|
}
|
|
else {
|
|
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 {
|
|
$null = Disconnect-VIServer @params
|
|
}
|
|
|
|
catch {
|
|
# Write a error message to the log.
|
|
Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
|
|
}
|
|
}
|
|
|
|
'VMwareCisServer' {
|
|
try {
|
|
if ($Force) {
|
|
$null = Disconnect-CisServer -Server $RemoteHost -Confirm:$false -Force:$true
|
|
}
|
|
else {
|
|
$null = Disconnect-CisServer -Server $RemoteHost -Confirm:$false
|
|
}
|
|
}
|
|
|
|
catch {
|
|
# Write a error message to the log.
|
|
Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
|
|
}
|
|
}
|
|
|
|
default {
|
|
# Write a error message to the log.
|
|
Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
|
|
}
|
|
}
|
|
}
|
|
}
|