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
|
|
|
|
Defaults to "". Specify a string, which separates two CredentialStoreItems for the
|
|
|
|
same hostname.
|
|
|
|
|
|
|
|
.PARAMETER Type
|
2018-03-09 14:02:51 +01:00
|
|
|
Specify the host type of the target. Currently implemented targets are: CiscoUcs, FTP, NetAppFAS, VMware,
|
|
|
|
CisServer, ExchangeHTTP, ExchangeHTTPS, SCP.
|
|
|
|
|
2017-09-21 13:32:15 +02:00
|
|
|
.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
|
|
|
|
|
2017-10-23 10:53:52 +02:00
|
|
|
.EXAMPLE
|
|
|
|
Disconnect-From -RemoteHost "vcenter.myside.local" -Type CisServer
|
|
|
|
|
2017-12-22 08:10:37 +01:00
|
|
|
.EXAMPLE
|
|
|
|
Disconnect-From -RemoteHost "exchange01.myside.local" -Type ExchangeHTTP
|
|
|
|
|
|
|
|
.EXAMPLE
|
|
|
|
Disconnect-From -RemoteHost "exchange01.myside.local" -Type ExchangeHTTPS
|
|
|
|
|
2017-09-21 13:32:15 +02:00
|
|
|
.NOTES
|
2019-04-29 16:05:43 +02:00
|
|
|
- File Name : Disconnect-From.ps1
|
|
|
|
- Author : Marco Blessing - marco.blessing@googlemail.com
|
|
|
|
- Requires :
|
2017-09-21 13:32:15 +02:00
|
|
|
|
|
|
|
.LINK
|
|
|
|
https://github.com/OCram85/PSCredentialStore
|
|
|
|
#>
|
|
|
|
|
|
|
|
[CmdletBinding()]
|
|
|
|
param(
|
|
|
|
[Parameter(Mandatory = $true)]
|
|
|
|
[string]$RemoteHost,
|
|
|
|
|
|
|
|
[Parameter(Mandatory = $true)]
|
2018-03-09 14:02:51 +01:00
|
|
|
[ValidateSet(
|
|
|
|
'CiscoUcs',
|
|
|
|
'FTP',
|
|
|
|
'NetAppFAS',
|
|
|
|
'VMware',
|
|
|
|
'CisServer',
|
|
|
|
'ExchangeHTTP',
|
|
|
|
'ExchangeHTTPS',
|
|
|
|
'SCP'
|
|
|
|
)]
|
2017-09-21 13:32:15 +02:00
|
|
|
[string]$Type,
|
|
|
|
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
|
|
[switch]$Force
|
|
|
|
)
|
|
|
|
|
2017-12-22 08:10:37 +01:00
|
|
|
switch -Regex ($Type) {
|
2017-09-21 13:32:15 +02:00
|
|
|
"VMware" {
|
|
|
|
try {
|
|
|
|
if ($Force) {
|
|
|
|
Disconnect-VIServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop -Force:$true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Disconnect-VIServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
catch {
|
|
|
|
# Write a error message to the log.
|
|
|
|
$MessageParams = @{
|
2019-04-29 16:05:43 +02:00
|
|
|
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type
|
2017-09-21 13:32:15 +02:00
|
|
|
ErrorAction = "Stop"
|
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
|
|
|
}
|
2017-10-23 10:53:52 +02:00
|
|
|
}
|
|
|
|
"CisServer" {
|
|
|
|
try {
|
|
|
|
if ($Force) {
|
|
|
|
Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop -Force:$true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop
|
|
|
|
}
|
|
|
|
}
|
2017-09-21 13:32:15 +02:00
|
|
|
|
2017-10-23 10:53:52 +02:00
|
|
|
catch {
|
|
|
|
# Write a error message to the log.
|
|
|
|
$MessageParams = @{
|
2019-04-29 16:05:43 +02:00
|
|
|
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type
|
2017-10-23 10:53:52 +02:00
|
|
|
ErrorAction = "Stop"
|
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
|
|
|
}
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|
|
|
|
# Check for an existing WinSCP Session var
|
|
|
|
"FTP" {
|
|
|
|
if ($Global:WinSCPSession.Opened) {
|
|
|
|
Remove-WinSCPSession -WinSCPSession $Global:WinSCPSession
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$MessageParams = @{
|
2019-04-29 16:05:43 +02:00
|
|
|
Message = "There is no open WinSCP Session"
|
2017-09-21 13:32:15 +02:00
|
|
|
ErrorAction = "Stop"
|
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# DataONTAP doesn't have a CmdLet `Disconnect-NcController`.
|
|
|
|
# So we go ahead and clear the CurrentNcController variable.
|
|
|
|
"NetAppFAS" {
|
|
|
|
try {
|
|
|
|
$MessageParams = @{
|
2019-04-29 16:05:43 +02:00
|
|
|
Message = "Setting {0} to `$null, which will disconnect NetAppFAS" -f $Global:CurrentNcController
|
2017-09-21 13:32:15 +02:00
|
|
|
ErrorAction = "Continue"
|
|
|
|
}
|
|
|
|
Write-Verbose @MessageParams
|
|
|
|
$Global:CurrentNcController = $null
|
|
|
|
}
|
2017-12-22 08:10:37 +01:00
|
|
|
|
2017-09-21 13:32:15 +02:00
|
|
|
catch {
|
|
|
|
# Write a error message to the log.
|
|
|
|
$MessageParams = @{
|
2019-04-29 16:05:43 +02:00
|
|
|
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type
|
2017-09-21 13:32:15 +02:00
|
|
|
ErrorAction = "Stop"
|
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
"CiscoUcs" {
|
|
|
|
try {
|
|
|
|
Disconnect-Ucs -Ucs $RemoteHost
|
|
|
|
}
|
|
|
|
|
|
|
|
catch {
|
|
|
|
# Write a error message to the log.
|
|
|
|
$MessageParams = @{
|
2019-04-29 16:05:43 +02:00
|
|
|
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type
|
2017-09-21 13:32:15 +02:00
|
|
|
ErrorAction = "Stop"
|
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
|
|
|
}
|
|
|
|
}
|
2017-12-22 08:10:37 +01:00
|
|
|
"ExchangeHTTP*" {
|
|
|
|
try {
|
|
|
|
Get-Variable -Name 'PSExchangeRemote' -Scope Global -ErrorAction Stop
|
|
|
|
Remove-PSSession -Session $Global:PSExchangeRemote -ErrorAction Stop
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
$MessageParams = @{
|
2019-04-29 16:05:43 +02:00
|
|
|
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type
|
2017-12-22 08:10:37 +01:00
|
|
|
ErrorAction = "Stop"
|
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
|
|
|
}
|
|
|
|
}
|
2018-03-09 14:02:51 +01:00
|
|
|
"SCP" {
|
|
|
|
if ($Global:WinSCPSession.Opened) {
|
|
|
|
Remove-WinSCPSession -WinSCPSession $Global:WinSCPSession
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$MessageParams = @{
|
2019-04-29 16:05:43 +02:00
|
|
|
Message = "There is no open WinSCP Session"
|
2018-03-09 14:02:51 +01:00
|
|
|
ErrorAction = "Stop"
|
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
|
|
|
}
|
|
|
|
}
|
2017-09-21 13:32:15 +02:00
|
|
|
default {
|
|
|
|
# Write a error message to the log.
|
|
|
|
$MessageParams = @{
|
2019-04-29 16:05:43 +02:00
|
|
|
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type
|
2017-09-21 13:32:15 +02:00
|
|
|
ErrorAction = "Stop"
|
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|