Implement CiscoUcsCentral provider to Connect-To #54

Closed
pinguinfuss wants to merge 2 commits from pinguinfuss/Connect-To-UcsCentral into master
2 changed files with 96 additions and 64 deletions

View File

@ -14,8 +14,16 @@ function Connect-To {
same hostname. same hostname.
.PARAMETER Type .PARAMETER Type
Specify the host type of the target. Currently implemented targets are: Possible connection values are: Specify the host type of the target. Currently implemented targets are:
CiscoUcs, FTP, NetAppFAS, VMware, CisServer, ExchangeHTTP, ExchangeHTTPS, SCP. - CiscoUcs
- CiscoUcsCentral
- CisServer
- ExchangeHTTP
- ExchangeHTTPS
- FTP
- NetAppFAS
- SCP
- VMware
.PARAMETER Credentials .PARAMETER Credentials
Use this parameter to bypass the stored credentials. Without this parameter Connect-To tries to read the Use this parameter to bypass the stored credentials. Without this parameter Connect-To tries to read the
@ -41,6 +49,9 @@ function Connect-To {
.EXAMPLE .EXAMPLE
Connect-To -RemoteHost "ucs.myside.local" -Type CiscoUcs Connect-To -RemoteHost "ucs.myside.local" -Type CiscoUcs
.EXAMPLE
Connect-To -RemoteHost "ucscentral.myside.local" -Type 'CiscoUcsCentral'
.EXAMPLE .EXAMPLE
Connect-To -RemoteHost "ftp.myside.local" -Type FTP Connect-To -RemoteHost "ftp.myside.local" -Type FTP
@ -82,13 +93,14 @@ function Connect-To {
[Parameter(Mandatory = $true, ParameterSetName = "Private")] [Parameter(Mandatory = $true, ParameterSetName = "Private")]
[ValidateSet( [ValidateSet(
'CiscoUcs', 'CiscoUcs',
'FTP', 'CiscoUcsCentral',
'NetAppFAS',
'VMware',
'CisServer', 'CisServer',
'ExchangeHTTP', 'ExchangeHTTP',
'ExchangeHTTPS', 'ExchangeHTTPS',
'SCP' 'FTP',
'NetAppFAS',
'SCP',
'VMware'
)] )]
[string]$Type, [string]$Type,
@ -109,6 +121,11 @@ function Connect-To {
) )
begin { begin {
# Set defaults for the preference variables.
$InformationPreference = 'Continue'
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
# Set the CredentialStore for private, shared or custom mode. # Set the CredentialStore for private, shared or custom mode.
Write-Debug ("ParameterSetName: {0}" -f $PSCmdlet.ParameterSetName) Write-Debug ("ParameterSetName: {0}" -f $PSCmdlet.ParameterSetName)
if ($PSCmdlet.ParameterSetName -eq "Private") { if ($PSCmdlet.ParameterSetName -eq "Private") {
@ -122,7 +139,7 @@ function Connect-To {
# First check the optional modules # First check the optional modules
if (-not (Resolve-Dependency -Name $Type)) { 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) { switch ($Type) {
"VMware" { "VMware" {
@ -153,11 +170,8 @@ function Connect-To {
} }
catch { catch {
$MessageParams = @{ $m = "Unable to look up credential store item for {0}/Identifier {1}!" -f $RemoteHost, $Identifier
Message = "Unable to look up credential store item for RemoteHost {0}/Identifier {1}!" -f $RemoteHost, $Identifier Write-Error -Message $m
ErrorAction = "Stop"
}
Write-Error @MessageParams
} }
} }
else { else {
@ -165,11 +179,7 @@ function Connect-To {
} }
if ($creds.UserName -eq "" -or $creds.Password.GetType().Name -ne "SecureString") { if ($creds.UserName -eq "" -or $creds.Password.GetType().Name -ne "SecureString") {
$MessageParams = @{ Write-Error -Message ("Please provide valid credentials for RemoteHost {0}!" -f $RemoteHost)
Message = "Please provide valid credentials for RemoteHost {0}!" -f $RemoteHost
ErrorAction = "Stop"
}
Write-Error @MessageParams
} }
else { else {
switch ($Type) { switch ($Type) {
@ -180,11 +190,19 @@ function Connect-To {
} }
catch { catch {
$MessageParams = @{ $_.Exception.Message | Write-Warning
Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type Write-Error -Message ("Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type)
ErrorAction = "Stop" }
} }
Write-Error @MessageParams '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" { "FTP" {

View File

@ -14,8 +14,16 @@ function Disconnect-From {
same hostname. same hostname.
.PARAMETER Type .PARAMETER Type
Specify the host type of the target. Currently implemented targets are: CiscoUcs, FTP, NetAppFAS, VMware, Specify the host type of the target. Currently implemented targets are:
CisServer, ExchangeHTTP, ExchangeHTTPS, SCP. - CiscoUcs
- CiscoUcsCentral
- CisServer
- ExchangeHTTP
- ExchangeHTTPS
- FTP
- NetAppFAS
- SCP
- VMware
.PARAMETER Force .PARAMETER Force
Force the disconnect, even if the disconnect would fail. Force the disconnect, even if the disconnect would fail.
@ -29,6 +37,9 @@ function Disconnect-From {
.EXAMPLE .EXAMPLE
Disconnect-From -RemoteHost "ucs.myside.local" -Type CiscoUcs Disconnect-From -RemoteHost "ucs.myside.local" -Type CiscoUcs
.EXAMPLE
Disconnect-From -RemoteHost "ucscentral.myside.local" -Type 'CiscoUcsCentral'
.EXAMPLE .EXAMPLE
Disconnect-From -RemoteHost "ftp.myside.local" -Type FTP Disconnect-From -RemoteHost "ftp.myside.local" -Type FTP
@ -67,13 +78,14 @@ function Disconnect-From {
[Parameter(Mandatory = $true)] [Parameter(Mandatory = $true)]
[ValidateSet( [ValidateSet(
'CiscoUcs', 'CiscoUcs',
'FTP', 'CiscoUcsCentral',
'NetAppFAS',
'VMware',
'CisServer', 'CisServer',
'ExchangeHTTP', 'ExchangeHTTP',
'ExchangeHTTPS', 'ExchangeHTTPS',
'SCP' 'FTP',
'NetAppFAS',
'SCP',
'VMware'
)] )]
[string]$Type, [string]$Type,
@ -81,43 +93,43 @@ function Disconnect-From {
[switch]$Force [switch]$Force
) )
# Set defaults for the preference variables.
$InformationPreference = 'Continue'
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
switch -Regex ($Type) { switch -Regex ($Type) {
"VMware" { "VMware" {
try { try {
if ($Force) { if ($Force) {
Disconnect-VIServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop -Force:$true Disconnect-VIServer -Server $RemoteHost -Confirm:$false -Force:$true
} }
else { else {
Disconnect-VIServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop Disconnect-VIServer -Server $RemoteHost -Confirm:$false
} }
} }
catch { catch {
# Write a error message to the log. # Write a error message to the log.
$MessageParams = @{ $_.Exception.Message | Write-Warning
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type Write-Error -Message ("Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type)
ErrorAction = "Stop"
}
Write-Error @MessageParams
} }
} }
"CisServer" { "CisServer" {
try { try {
if ($Force) { if ($Force) {
Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop -Force:$true Disconnect-CisServer -Server $RemoteHost -Confirm:$false -Force:$true
} }
else { else {
Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop Disconnect-CisServer -Server $RemoteHost -Confirm:$false
} }
} }
catch { catch {
# Write a error message to the log. # Write a error message to the log.
$MessageParams = @{ $_.Exception.Message | Write-Warning
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type Write-Error -Message ("Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type)
ErrorAction = "Stop"
}
Write-Error @MessageParams
} }
} }
# Check for an existing WinSCP Session var # Check for an existing WinSCP Session var
@ -147,11 +159,8 @@ function Disconnect-From {
catch { catch {
# Write a error message to the log. # Write a error message to the log.
$MessageParams = @{ $_.Exception.Message | Write-Warning
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type Write-Error -Message ("Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type)
ErrorAction = "Stop"
}
Write-Error @MessageParams
} }
} }
@ -162,24 +171,33 @@ function Disconnect-From {
catch { catch {
# Write a error message to the log. # Write a error message to the log.
$MessageParams = @{ $_.Exception.Message | Write-Warning
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type Write-Error -Message ("Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type)
ErrorAction = "Stop"
}
Write-Error @MessageParams
} }
} }
'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*" { "ExchangeHTTP*" {
try { try {
Get-Variable -Name 'PSExchangeRemote' -Scope Global -ErrorAction Stop Get-Variable -Name 'PSExchangeRemote' -Scope Global
Remove-PSSession -Session $Global:PSExchangeRemote -ErrorAction Stop Remove-PSSession -Session $Global:PSExchangeRemote
} }
catch { catch {
$MessageParams = @{ # Write a error message to the log.
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type $_.Exception.Message | Write-Warning
ErrorAction = "Stop" Write-Error -Message ("Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type)
}
Write-Error @MessageParams
} }
} }
"SCP" { "SCP" {
@ -196,11 +214,7 @@ function Disconnect-From {
} }
default { default {
# Write a error message to the log. # Write a error message to the log.
$MessageParams = @{ Write-Error -Message ("Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type)
Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type
ErrorAction = "Stop"
}
Write-Error @MessageParams
} }
} }
} }