PSCredentialStore/src/Connection/Disconnect-From.ps1

240 lines
7.9 KiB
PowerShell
Raw Normal View History

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
same hostname.
.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
- 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()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSAvoidGlobalVars',
'',
Justification = 'Wrapping existing global vars from external modules'
)]
param (
[Parameter(Mandatory = $true)]
[string]$RemoteHost,
[Parameter(Mandatory = $true)]
[ValidateSet(
'CiscoUcs',
'CiscoUcsCentral',
2023-03-09 20:18:57 +01:00
'ExchangeHTTP',
'ExchangeHTTPS',
'FTP',
'NetAppFAS',
2023-03-09 20:18:57 +01:00
'SCP',
'VMware',
2023-03-09 20:18:57 +01:00
'VMwareCisServer'
)]
[string]$Type,
[Parameter(Mandatory = $false)]
[switch]$Force
)
begin {}
process {
switch -Regex ($Type) {
2023-03-09 20:18:57 +01:00
'CiscoUcs' {
try {
2023-03-09 20:18:57 +01:00
Disconnect-Ucs -Ucs $RemoteHost
}
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
ErrorAction = 'Stop'
}
Write-Error @MessageParams
}
}
2023-03-09 20:18:57 +01:00
'CiscoUCSCentral' {
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'
}
2023-03-09 20:18:57 +01:00
Write-Error @MessageParams
}
2023-03-09 20:18:57 +01: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'
}
catch {
$MessageParams = @{
2023-03-09 20:18:57 +01:00
Message = 'Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type
ErrorAction = 'Stop'
}
Write-Error @MessageParams
}
}
2023-03-09 20:18:57 +01:00
# Check for an existing WinSCP Session var
2023-03-09 20:18:57 +01:00
'FTP' {
if ($Global:WinSCPSession.Opened) {
Remove-WinSCPSession -WinSCPSession $Global:WinSCPSession
}
else {
$MessageParams = @{
Message = 'There is no open WinSCP Session'
ErrorAction = 'Stop'
}
Write-Error @MessageParams
}
}
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 {
$MessageParams = @{
Message = (
2023-03-09 20:18:57 +01:00
'Setting {0} to `$null, which will disconnect NetAppFAS' -f $Global:CurrentNcController
)
ErrorAction = 'Continue'
}
Write-Verbose @MessageParams
$Global:CurrentNcController = $null
}
2017-12-22 08:10:37 +01: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
ErrorAction = 'Stop'
}
Write-Error @MessageParams
}
}
2023-03-09 20:18:57 +01:00
'SCP' {
if ($Global:WinSCPSession.Opened) {
Remove-WinSCPSession -WinSCPSession $Global:WinSCPSession
}
else {
$MessageParams = @{
2023-03-09 20:18:57 +01:00
Message = 'There is no open WinSCP Session'
ErrorAction = 'Stop'
}
Write-Error @MessageParams
}
2017-12-22 08:10:37 +01:00
}
2023-03-09 20:18:57 +01:00
'VMware' {
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'
}
}
2023-03-09 20:18:57 +01:00
catch {
2023-03-09 20:18:57 +01:00
# 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
ErrorAction = 'Stop'
}
Write-Error @MessageParams
}
}
2023-03-09 20:18:57 +01:00
'VMwareCisServer' {
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'
}
}
2023-03-09 20:18:57 +01: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
ErrorAction = 'Stop'
}
Write-Error @MessageParams
}
}
2023-03-09 20:18:57 +01:00
default {
# 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
ErrorAction = 'Stop'
}
Write-Error @MessageParams
}
}
}
}