2017-09-21 13:32:15 +02:00
|
|
|
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
|
2023-03-09 20:18:57 +01:00
|
|
|
Defaults to ''. Specify a string, which separates two CredentialStoreItems for the
|
2017-09-21 13:32:15 +02:00
|
|
|
same hostname.
|
|
|
|
|
|
|
|
.PARAMETER Type
|
2022-07-19 14:29:25 +02:00
|
|
|
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
|
2022-07-19 14:29:25 +02:00
|
|
|
- SCP
|
2023-03-09 20:18:57 +01:00
|
|
|
- VMware
|
|
|
|
- VMwareCisServer
|
2018-03-09 14:02:51 +01:00
|
|
|
|
2017-09-21 13:32:15 +02:00
|
|
|
.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
|
2017-09-21 13:32:15 +02:00
|
|
|
|
|
|
|
.EXAMPLE
|
2023-03-09 20:18:57 +01:00
|
|
|
Disconnect-From -RemoteHost 'ftp.myside.local' -Type FTP
|
2017-09-21 13:32:15 +02:00
|
|
|
|
|
|
|
.EXAMPLE
|
2023-03-09 20:18:57 +01:00
|
|
|
Disconnect-From -RemoteHost 'fas.myside.local' -Type NetAppFAS
|
2017-09-21 13:32:15 +02:00
|
|
|
|
|
|
|
.EXAMPLE
|
2023-03-09 20:18:57 +01:00
|
|
|
Disconnect-From -RemoteHost 'esx01.myside.local' -Type VMware
|
2017-09-21 13:32:15 +02:00
|
|
|
|
|
|
|
.EXAMPLE
|
2023-03-09 20:18:57 +01:00
|
|
|
Disconnect-From -RemoteHost 'esx01.myside.local' -Type VMware -Force:$True
|
2017-09-21 13:32:15 +02:00
|
|
|
|
2017-10-23 10:53:52 +02:00
|
|
|
.EXAMPLE
|
2023-03-09 20:18:57 +01:00
|
|
|
Disconnect-From -RemoteHost 'vcenter.myside.local' -Type CisServer
|
2017-10-23 10:53:52 +02:00
|
|
|
|
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
|
2017-09-21 13:32:15 +02:00
|
|
|
#>
|
|
|
|
|
|
|
|
[CmdletBinding()]
|
2022-07-15 10:59:56 +02:00
|
|
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
|
|
|
|
'PSAvoidGlobalVars',
|
|
|
|
'',
|
|
|
|
Justification = 'Wrapping existing global vars from external modules'
|
|
|
|
)]
|
2022-06-28 08:56:33 +02:00
|
|
|
param (
|
2017-09-21 13:32:15 +02:00
|
|
|
[Parameter(Mandatory = $true)]
|
|
|
|
[string]$RemoteHost,
|
|
|
|
|
|
|
|
[Parameter(Mandatory = $true)]
|
2018-03-09 14:02:51 +01:00
|
|
|
[ValidateSet(
|
|
|
|
'CiscoUcs',
|
2022-07-19 14:29:25 +02:00
|
|
|
'CiscoUcsCentral',
|
2023-03-09 20:18:57 +01:00
|
|
|
'ExchangeHTTP',
|
|
|
|
'ExchangeHTTPS',
|
2018-03-09 14:02:51 +01:00
|
|
|
'FTP',
|
|
|
|
'NetAppFAS',
|
2023-03-09 20:18:57 +01:00
|
|
|
'SCP',
|
2018-03-09 14:02:51 +01:00
|
|
|
'VMware',
|
2023-03-09 20:18:57 +01:00
|
|
|
'VMwareCisServer'
|
2018-03-09 14:02:51 +01:00
|
|
|
)]
|
2017-09-21 13:32:15 +02:00
|
|
|
[string]$Type,
|
|
|
|
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
|
|
[switch]$Force
|
|
|
|
)
|
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
begin {}
|
|
|
|
|
|
|
|
process {
|
|
|
|
switch -Regex ($Type) {
|
2023-03-09 20:18:57 +01:00
|
|
|
'CiscoUcs' {
|
2022-06-28 08:56:33 +02:00
|
|
|
try {
|
2023-03-09 20:18:57 +01:00
|
|
|
Disconnect-Ucs -Ucs $RemoteHost
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
catch {
|
|
|
|
# Write a error message to the log.
|
|
|
|
$MessageParams = @{
|
2023-03-09 20:18:57 +01:00
|
|
|
Message = 'Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type
|
2022-06-28 08:56:33 +02:00
|
|
|
ErrorAction = 'Stop'
|
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|
|
|
|
}
|
2023-03-09 20:18:57 +01:00
|
|
|
|
|
|
|
'CiscoUCSCentral' {
|
2022-06-28 08:56:33 +02:00
|
|
|
try {
|
2023-03-09 20:18:57 +01:00
|
|
|
$handle = Connect-UcsCentral -Name $RemoteHost -Credential $creds -NotDefault
|
|
|
|
$ExecutionContext.SessionState.PSVariable.Set('DefaultUcsCentral', $handle)
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
$MessageParams = @{
|
|
|
|
Message = 'Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type
|
|
|
|
ErrorAction = 'Stop'
|
2022-06-28 08:56:33 +02:00
|
|
|
}
|
2023-03-09 20:18:57 +01:00
|
|
|
Write-Error @MessageParams
|
2017-10-23 10:53:52 +02:00
|
|
|
}
|
2023-03-09 20:18:57 +01:00
|
|
|
}
|
2017-09-21 13:32:15 +02:00
|
|
|
|
2023-03-09 20:18:57 +01:00
|
|
|
'ExchangeHTTP*' {
|
|
|
|
try {
|
|
|
|
Get-Variable -Name 'PSExchangeRemote' -Scope 'Global' -ErrorAction 'Stop'
|
|
|
|
Remove-PSSession -Session $Global:PSExchangeRemote -ErrorAction 'Stop'
|
|
|
|
}
|
2022-06-28 08:56:33 +02:00
|
|
|
catch {
|
|
|
|
$MessageParams = @{
|
2023-03-09 20:18:57 +01:00
|
|
|
Message = 'Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type
|
2022-06-28 08:56:33 +02:00
|
|
|
ErrorAction = 'Stop'
|
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
2017-10-23 10:53:52 +02:00
|
|
|
}
|
|
|
|
}
|
2023-03-09 20:18:57 +01:00
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
# Check for an existing WinSCP Session var
|
2023-03-09 20:18:57 +01:00
|
|
|
'FTP' {
|
2022-06-28 08:56:33 +02:00
|
|
|
if ($Global:WinSCPSession.Opened) {
|
|
|
|
Remove-WinSCPSession -WinSCPSession $Global:WinSCPSession
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|
2022-06-28 08:56:33 +02:00
|
|
|
else {
|
|
|
|
$MessageParams = @{
|
|
|
|
Message = 'There is no open WinSCP Session'
|
|
|
|
ErrorAction = 'Stop'
|
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|
|
|
|
}
|
2023-03-09 20:18:57 +01:00
|
|
|
|
2022-06-28 08:56:33 +02: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' {
|
2022-06-28 08:56:33 +02:00
|
|
|
try {
|
|
|
|
$MessageParams = @{
|
2022-07-15 10:59:56 +02:00
|
|
|
Message = (
|
2023-03-09 20:18:57 +01:00
|
|
|
'Setting {0} to `$null, which will disconnect NetAppFAS' -f $Global:CurrentNcController
|
2022-07-15 10:59:56 +02:00
|
|
|
)
|
2022-06-28 08:56:33 +02:00
|
|
|
ErrorAction = 'Continue'
|
|
|
|
}
|
|
|
|
Write-Verbose @MessageParams
|
|
|
|
$Global:CurrentNcController = $null
|
|
|
|
}
|
2017-12-22 08:10:37 +01:00
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
catch {
|
|
|
|
# Write a error message to the log.
|
|
|
|
$MessageParams = @{
|
2023-03-09 20:18:57 +01:00
|
|
|
Message = 'Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type
|
2022-06-28 08:56:33 +02:00
|
|
|
ErrorAction = 'Stop'
|
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 20:18:57 +01:00
|
|
|
'SCP' {
|
|
|
|
if ($Global:WinSCPSession.Opened) {
|
|
|
|
Remove-WinSCPSession -WinSCPSession $Global:WinSCPSession
|
|
|
|
}
|
|
|
|
else {
|
2022-06-28 08:56:33 +02:00
|
|
|
$MessageParams = @{
|
2023-03-09 20:18:57 +01:00
|
|
|
Message = 'There is no open WinSCP Session'
|
2022-06-28 08:56:33 +02:00
|
|
|
ErrorAction = 'Stop'
|
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|
2017-12-22 08:10:37 +01:00
|
|
|
}
|
2023-03-09 20:18:57 +01:00
|
|
|
|
|
|
|
'VMware' {
|
2022-07-19 14:29:25 +02:00
|
|
|
try {
|
2023-03-09 20:18:57 +01:00
|
|
|
if ($Force) {
|
|
|
|
Disconnect-VIServer -Server $RemoteHost -Confirm:$false -ErrorAction 'Stop' -Force:$true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Disconnect-VIServer -Server $RemoteHost -Confirm:$false -ErrorAction 'Stop'
|
|
|
|
}
|
2022-07-19 14:29:25 +02:00
|
|
|
}
|
2023-03-09 20:18:57 +01:00
|
|
|
|
2022-07-19 14:29:25 +02:00
|
|
|
catch {
|
2023-03-09 20:18:57 +01:00
|
|
|
# Write a error message to the log.
|
2022-07-19 14:29:25 +02:00
|
|
|
$MessageParams = @{
|
2023-03-09 20:18:57 +01:00
|
|
|
Message = 'Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type
|
2022-07-19 14:29:25 +02:00
|
|
|
ErrorAction = 'Stop'
|
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 20:18:57 +01:00
|
|
|
'VMwareCisServer' {
|
2022-06-28 08:56:33 +02:00
|
|
|
try {
|
2023-03-09 20:18:57 +01:00
|
|
|
if ($Force) {
|
|
|
|
Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction 'Stop' -Force:$true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction 'Stop'
|
2022-06-28 08:56:33 +02:00
|
|
|
}
|
|
|
|
}
|
2023-03-09 20:18:57 +01:00
|
|
|
|
|
|
|
catch {
|
|
|
|
# Write a error message to the log.
|
2022-06-28 08:56:33 +02:00
|
|
|
$MessageParams = @{
|
2023-03-09 20:18:57 +01:00
|
|
|
Message = 'Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type
|
2022-06-28 08:56:33 +02:00
|
|
|
ErrorAction = 'Stop'
|
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
|
|
|
}
|
2018-03-09 14:02:51 +01:00
|
|
|
}
|
2023-03-09 20:18:57 +01:00
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
default {
|
|
|
|
# Write a error message to the log.
|
2018-03-09 14:02:51 +01:00
|
|
|
$MessageParams = @{
|
2023-03-09 20:18:57 +01:00
|
|
|
Message = 'Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type
|
2022-06-28 08:56:33 +02:00
|
|
|
ErrorAction = 'Stop'
|
2018-03-09 14:02:51 +01:00
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
|
|
|
}
|
|
|
|
}
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|
|
|
|
}
|