Skip to content
This repository was archived by the owner on Jul 6, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 40 additions & 22 deletions src/Connection/Connect-To.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ function Connect-To {
same hostname.

.PARAMETER Type
Specify the host type of the target. Currently implemented targets are: Possible connection values are:
CiscoUcs, FTP, NetAppFAS, VMware, CisServer, ExchangeHTTP, ExchangeHTTPS, SCP.
Specify the host type of the target. Currently implemented targets are:
- CiscoUcs
- CiscoUcsCentral
- CisServer
- ExchangeHTTP
- ExchangeHTTPS
- FTP
- NetAppFAS
- SCP
- VMware

.PARAMETER Credentials
Use this parameter to bypass the stored credentials. Without this parameter Connect-To tries to read the
Expand All @@ -41,6 +49,9 @@ function Connect-To {
.EXAMPLE
Connect-To -RemoteHost "ucs.myside.local" -Type CiscoUcs

.EXAMPLE
Connect-To -RemoteHost "ucscentral.myside.local" -Type 'CiscoUcsCentral'

.EXAMPLE
Connect-To -RemoteHost "ftp.myside.local" -Type FTP

Expand Down Expand Up @@ -82,13 +93,14 @@ function Connect-To {
[Parameter(Mandatory = $true, ParameterSetName = "Private")]
[ValidateSet(
'CiscoUcs',
'FTP',
'NetAppFAS',
'VMware',
'CiscoUcsCentral',
'CisServer',
'ExchangeHTTP',
'ExchangeHTTPS',
'SCP'
'FTP',
'NetAppFAS',
'SCP',
'VMware'
)]
[string]$Type,

Expand All @@ -109,6 +121,11 @@ function Connect-To {
)

begin {
# Set defaults for the preference variables.
$InformationPreference = 'Continue'
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'

# Set the CredentialStore for private, shared or custom mode.
Write-Debug ("ParameterSetName: {0}" -f $PSCmdlet.ParameterSetName)
if ($PSCmdlet.ParameterSetName -eq "Private") {
Expand All @@ -122,7 +139,7 @@ function Connect-To {

# First check the optional modules
if (-not (Resolve-Dependency -Name $Type)) {
Write-Error -Message ("Could not resolve the optional dependencies defined for {0}" -f $Type) -ErrorAction 'Stop'
Write-Error -Message ("Could not resolve the optional dependencies defined for {0}" -f $Type)
}
switch ($Type) {
"VMware" {
Expand Down Expand Up @@ -153,23 +170,16 @@ function Connect-To {
}

catch {
$MessageParams = @{
Message = "Unable to look up credential store item for RemoteHost {0}/Identifier {1}!" -f $RemoteHost, $Identifier
ErrorAction = "Stop"
}
Write-Error @MessageParams
$m = "Unable to look up credential store item for {0}/Identifier {1}!" -f $RemoteHost, $Identifier
Write-Error -Message $m
}
}
else {
$creds = $Credentials
}

if ($creds.UserName -eq "" -or $creds.Password.GetType().Name -ne "SecureString") {
$MessageParams = @{
Message = "Please provide valid credentials for RemoteHost {0}!" -f $RemoteHost
ErrorAction = "Stop"
}
Write-Error @MessageParams
Write-Error -Message ("Please provide valid credentials for RemoteHost {0}!" -f $RemoteHost)
}
else {
switch ($Type) {
Expand All @@ -180,11 +190,19 @@ function Connect-To {
}

catch {
$MessageParams = @{
Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type
ErrorAction = "Stop"
}
Write-Error @MessageParams
$_.Exception.Message | Write-Warning
Write-Error -Message ("Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type)
}
}
'CiscoUcsCentral' {
try {
$handle = Connect-UcsCentral -Name $RemoteHost -Credential $creds -NotDefault
$ExecutionContext.SessionState.PSVariable.Set('DefaultUcsCentral', $handle)
}

catch {
$_.Exception.Message | Write-Warning
Write-Error -Message ('Unable to connect to {0} using {1}' -f $RemoteHost, $Type)
}
}
"FTP" {
Expand Down
98 changes: 56 additions & 42 deletions src/Connection/Disconnect-From.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ function Disconnect-From {
same hostname.

.PARAMETER Type
Specify the host type of the target. Currently implemented targets are: CiscoUcs, FTP, NetAppFAS, VMware,
CisServer, ExchangeHTTP, ExchangeHTTPS, SCP.
Specify the host type of the target. Currently implemented targets are:
- CiscoUcs
- CiscoUcsCentral
- CisServer
- ExchangeHTTP
- ExchangeHTTPS
- FTP
- NetAppFAS
- SCP
- VMware

.PARAMETER Force
Force the disconnect, even if the disconnect would fail.
Expand All @@ -29,6 +37,9 @@ function Disconnect-From {
.EXAMPLE
Disconnect-From -RemoteHost "ucs.myside.local" -Type CiscoUcs

.EXAMPLE
Disconnect-From -RemoteHost "ucscentral.myside.local" -Type 'CiscoUcsCentral'

.EXAMPLE
Disconnect-From -RemoteHost "ftp.myside.local" -Type FTP

Expand Down Expand Up @@ -67,57 +78,58 @@ function Disconnect-From {
[Parameter(Mandatory = $true)]
[ValidateSet(
'CiscoUcs',
'FTP',
'NetAppFAS',
'VMware',
'CiscoUcsCentral',
'CisServer',
'ExchangeHTTP',
'ExchangeHTTPS',
'SCP'
'FTP',
'NetAppFAS',
'SCP',
'VMware'
)]
[string]$Type,

[Parameter(Mandatory = $false)]
[switch]$Force
)

# Set defaults for the preference variables.
$InformationPreference = 'Continue'
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'


switch -Regex ($Type) {
"VMware" {
try {
if ($Force) {
Disconnect-VIServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop -Force:$true
Disconnect-VIServer -Server $RemoteHost -Confirm:$false -Force:$true
}
else {
Disconnect-VIServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop
Disconnect-VIServer -Server $RemoteHost -Confirm:$false
}
}

catch {
# Write a error message to the log.
$MessageParams = @{
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type
ErrorAction = "Stop"
}
Write-Error @MessageParams
$_.Exception.Message | Write-Warning
Write-Error -Message ("Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type)
}
}
"CisServer" {
try {
if ($Force) {
Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop -Force:$true
Disconnect-CisServer -Server $RemoteHost -Confirm:$false -Force:$true
}
else {
Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop
Disconnect-CisServer -Server $RemoteHost -Confirm:$false
}
}

catch {
# Write a error message to the log.
$MessageParams = @{
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type
ErrorAction = "Stop"
}
Write-Error @MessageParams
$_.Exception.Message | Write-Warning
Write-Error -Message ("Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type)
}
}
# Check for an existing WinSCP Session var
Expand Down Expand Up @@ -147,11 +159,8 @@ function Disconnect-From {

catch {
# Write a error message to the log.
$MessageParams = @{
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type
ErrorAction = "Stop"
}
Write-Error @MessageParams
$_.Exception.Message | Write-Warning
Write-Error -Message ("Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type)
}

}
Expand All @@ -162,24 +171,33 @@ function Disconnect-From {

catch {
# Write a error message to the log.
$MessageParams = @{
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type
ErrorAction = "Stop"
}
Write-Error @MessageParams
$_.Exception.Message | Write-Warning
Write-Error -Message ("Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type)
}
}

'CiscoUcsCentral' {
try {
Disconnect-UcsCentral -Ucs $RemoteHost
}

catch {
# Write a error message to the log.
$_.Exception.Message | Write-Warning
Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type)
}
}


"ExchangeHTTP*" {
try {
Get-Variable -Name 'PSExchangeRemote' -Scope Global -ErrorAction Stop
Remove-PSSession -Session $Global:PSExchangeRemote -ErrorAction Stop
Get-Variable -Name 'PSExchangeRemote' -Scope Global
Remove-PSSession -Session $Global:PSExchangeRemote
}
catch {
$MessageParams = @{
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type
ErrorAction = "Stop"
}
Write-Error @MessageParams
# Write a error message to the log.
$_.Exception.Message | Write-Warning
Write-Error -Message ("Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type)
}
}
"SCP" {
Expand All @@ -196,11 +214,7 @@ function Disconnect-From {
}
default {
# Write a error message to the log.
$MessageParams = @{
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type
ErrorAction = "Stop"
}
Write-Error @MessageParams
Write-Error -Message ("Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type)
}
}
}