diff --git a/src/Connection/Connect-To.ps1 b/src/Connection/Connect-To.ps1 index b7e1abb..815cd08 100644 --- a/src/Connection/Connect-To.ps1 +++ b/src/Connection/Connect-To.ps1 @@ -58,28 +58,20 @@ function Connect-To { .EXAMPLE Connect-To -RemoteHost "exchange01.myside.local" -Type ExchangeHTTPS - - .NOTES - - File Name : Connect-To.ps1 - - Author : Marco Blessing - marco.blessing@googlemail.com - - Requires : - - .LINK - https://github.com/OCram85/PSCredentialStore #> - [CmdletBinding(DefaultParameterSetName = "Private")] - param( - [Parameter(Mandatory = $true, ParameterSetName = "Shared")] - [Parameter(Mandatory = $true, ParameterSetName = "Private")] + [CmdletBinding(DefaultParameterSetName = 'Private')] + param ( + [Parameter(Mandatory = $true, ParameterSetName = 'Shared')] + [Parameter(Mandatory = $true, ParameterSetName = 'Private')] [string]$RemoteHost, - [Parameter(Mandatory = $false, ParameterSetName = "Shared")] - [Parameter(Mandatory = $false, ParameterSetName = "Private")] + [Parameter(Mandatory = $false, ParameterSetName = 'Shared')] + [Parameter(Mandatory = $false, ParameterSetName = 'Private')] [string]$Identifier, - [Parameter(Mandatory = $true, ParameterSetName = "Shared")] - [Parameter(Mandatory = $true, ParameterSetName = "Private")] + [Parameter(Mandatory = $true, ParameterSetName = 'Shared')] + [Parameter(Mandatory = $true, ParameterSetName = 'Private')] [ValidateSet( 'CiscoUcs', 'FTP', @@ -92,29 +84,29 @@ function Connect-To { )] [string]$Type, - [Parameter(Mandatory = $False, ParameterSetName = "Shared")] - [Parameter(Mandatory = $False, ParameterSetName = "Private")] + [Parameter(Mandatory = $False, ParameterSetName = 'Shared')] + [Parameter(Mandatory = $False, ParameterSetName = 'Private')] [PSCredential]$Credentials, - [Parameter(Mandatory = $true, ParameterSetNAme = "Shared")] + [Parameter(Mandatory = $true, ParameterSetNAme = 'Shared')] [switch]$Shared, - [Parameter(Mandatory = $False, ParameterSetName = "Shared")] + [Parameter(Mandatory = $False, ParameterSetName = 'Shared')] [ValidateNotNullOrEmpty()] [string]$Path, - [Parameter(Mandatory = $False, ParameterSetName = "Private")] - [Parameter(Mandatory = $False, ParameterSetName = "Shared")] + [Parameter(Mandatory = $False, ParameterSetName = 'Private')] + [Parameter(Mandatory = $False, ParameterSetName = 'Shared')] [switch]$PassThru ) begin { # Set the CredentialStore for private, shared or custom mode. Write-Debug ("ParameterSetName: {0}" -f $PSCmdlet.ParameterSetName) - if ($PSCmdlet.ParameterSetName -eq "Private") { + if ($PSCmdlet.ParameterSetName -eq 'Private') { $Path = Get-DefaultCredentialStorePath } - elseif ($PSCmdlet.ParameterSetName -eq "Shared") { + elseif ($PSCmdlet.ParameterSetName -eq 'Shared') { if (!($PSBoundParameters.ContainsKey('Path'))) { $Path = Get-DefaultCredentialStorePath -Shared } @@ -155,7 +147,7 @@ function Connect-To { catch { $MessageParams = @{ Message = "Unable to look up credential store item for RemoteHost {0}/Identifier {1}!" -f $RemoteHost, $Identifier - ErrorAction = "Stop" + ErrorAction = 'Stop' } Write-Error @MessageParams } @@ -164,10 +156,10 @@ function Connect-To { $creds = $Credentials } - if ($creds.UserName -eq "" -or $creds.Password.GetType().Name -ne "SecureString") { + if ($creds.UserName -eq "" -or $creds.Password.GetType().Name -ne 'SecureString') { $MessageParams = @{ Message = "Please provide valid credentials for RemoteHost {0}!" -f $RemoteHost - ErrorAction = "Stop" + ErrorAction = 'Stop' } Write-Error @MessageParams } @@ -176,13 +168,13 @@ function Connect-To { "CiscoUcs" { try { $handle = Connect-Ucs -Name $RemoteHost -Credential $creds -ErrorAction Stop - $ExecutionContext.SessionState.PSVariable.Set("DefaultUcs", $handle) + $ExecutionContext.SessionState.PSVariable.Set('DefaultUcs', $handle) } catch { $MessageParams = @{ Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type - ErrorAction = "Stop" + ErrorAction = 'Stop' } Write-Error @MessageParams } @@ -207,7 +199,7 @@ function Connect-To { # Check the connection state and find out if the session is still open. $MessageParams = @{ Message = "Connection to {0} using Type {1} was established. But now it seems to be lost!" -f $RemoteHost, $Type - ErrorAction = "Stop" + ErrorAction = 'Stop' } Write-Error @MessageParams } @@ -221,7 +213,7 @@ function Connect-To { # Write a error message to the log. $MessageParams = @{ Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type - ErrorAction = "Stop" + ErrorAction = 'Stop' } Write-Error @MessageParams } @@ -235,7 +227,7 @@ function Connect-To { # Write a error message to the log. $MessageParams = @{ Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type - ErrorAction = "Stop" + ErrorAction = 'Stop' } Write-Error @MessageParams } @@ -255,7 +247,7 @@ function Connect-To { # Write a error message to the log. $MessageParams = @{ Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type - ErrorAction = "Stop" + ErrorAction = 'Stop' } Write-Error @MessageParams } @@ -274,7 +266,7 @@ function Connect-To { # Write a error message to the log. $MessageParams = @{ Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type - ErrorAction = "Stop" + ErrorAction = 'Stop' } Write-Error @MessageParams } @@ -293,7 +285,7 @@ function Connect-To { # Write a error message to the log. $MessageParams = @{ Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type - ErrorAction = "Stop" + ErrorAction = 'Stop' } Write-Error @MessageParams } @@ -314,7 +306,7 @@ function Connect-To { # Write a error message to the log. $MessageParams = @{ Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type - ErrorAction = "Stop" + ErrorAction = 'Stop' } Write-Error @MessageParams } @@ -323,7 +315,7 @@ function Connect-To { # Check the connection state and find out if the session is still open. $MessageParams = @{ Message = "Connection to {0} using Type {1} was established. But now it seems to be lost!" -f $RemoteHost, $Type - ErrorAction = "Stop" + ErrorAction = 'Stop' } Write-Error @MessageParams } @@ -332,7 +324,7 @@ function Connect-To { # Write a error message to the log. $MessageParams = @{ Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type - ErrorAction = "Stop" + ErrorAction = 'Stop' } Write-Error @MessageParams } diff --git a/src/Connection/Disconnect-From.ps1 b/src/Connection/Disconnect-From.ps1 index 57139b9..509ef4b 100644 --- a/src/Connection/Disconnect-From.ps1 +++ b/src/Connection/Disconnect-From.ps1 @@ -49,18 +49,10 @@ function Disconnect-From { .EXAMPLE Disconnect-From -RemoteHost "exchange01.myside.local" -Type ExchangeHTTPS - - .NOTES - - File Name : Disconnect-From.ps1 - - Author : Marco Blessing - marco.blessing@googlemail.com - - Requires : - - .LINK - https://github.com/OCram85/PSCredentialStore #> [CmdletBinding()] - param( + param ( [Parameter(Mandatory = $true)] [string]$RemoteHost, @@ -81,126 +73,132 @@ function Disconnect-From { [switch]$Force ) - switch -Regex ($Type) { - "VMware" { - try { - if ($Force) { - Disconnect-VIServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop -Force:$true + begin {} + + process { + switch -Regex ($Type) { + "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 = @{ + Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type + ErrorAction = 'Stop' + } + Write-Error @MessageParams + } + } + "CisServer" { + try { + if ($Force) { + Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop -Force:$true + } + else { + Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop + } + } + + 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 + } + } + # Check for an existing WinSCP Session var + "FTP" { + if ($Global:WinSCPSession.Opened) { + Remove-WinSCPSession -WinSCPSession $Global:WinSCPSession } else { - Disconnect-VIServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop + $MessageParams = @{ + Message = 'There is no open WinSCP Session' + 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 = @{ + Message = "Setting {0} to `$null, which will disconnect NetAppFAS" -f $Global:CurrentNcController + ErrorAction = 'Continue' + } + Write-Verbose @MessageParams + $Global:CurrentNcController = $null + } - catch { - # Write a error message to the log. - $MessageParams = @{ - Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type - ErrorAction = "Stop" + 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 } - Write-Error @MessageParams + } - } - "CisServer" { - try { - if ($Force) { - Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop -Force:$true + "CiscoUcs" { + try { + Disconnect-Ucs -Ucs $RemoteHost + } + + 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 + } + } + "ExchangeHTTP*" { + try { + Get-Variable -Name 'PSExchangeRemote' -Scope Global -ErrorAction Stop + Remove-PSSession -Session $Global:PSExchangeRemote -ErrorAction Stop + } + catch { + $MessageParams = @{ + Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type + ErrorAction = 'Stop' + } + Write-Error @MessageParams + } + } + "SCP" { + if ($Global:WinSCPSession.Opened) { + Remove-WinSCPSession -WinSCPSession $Global:WinSCPSession } else { - Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction Stop + $MessageParams = @{ + Message = 'There is no open WinSCP Session' + ErrorAction = 'Stop' + } + Write-Error @MessageParams } } - - catch { + default { # Write a error message to the log. $MessageParams = @{ Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type - ErrorAction = "Stop" + ErrorAction = 'Stop' } Write-Error @MessageParams } } - # Check for an existing WinSCP Session var - "FTP" { - if ($Global:WinSCPSession.Opened) { - Remove-WinSCPSession -WinSCPSession $Global:WinSCPSession - } - else { - $MessageParams = @{ - Message = "There is no open WinSCP Session" - 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 = @{ - Message = "Setting {0} to `$null, which will disconnect NetAppFAS" -f $Global:CurrentNcController - ErrorAction = "Continue" - } - Write-Verbose @MessageParams - $Global:CurrentNcController = $null - } - - 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 - } - - } - "CiscoUcs" { - try { - Disconnect-Ucs -Ucs $RemoteHost - } - - 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 - } - } - "ExchangeHTTP*" { - try { - Get-Variable -Name 'PSExchangeRemote' -Scope Global -ErrorAction Stop - Remove-PSSession -Session $Global:PSExchangeRemote -ErrorAction Stop - } - catch { - $MessageParams = @{ - Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type - ErrorAction = "Stop" - } - Write-Error @MessageParams - } - } - "SCP" { - if ($Global:WinSCPSession.Opened) { - Remove-WinSCPSession -WinSCPSession $Global:WinSCPSession - } - else { - $MessageParams = @{ - Message = "There is no open WinSCP Session" - ErrorAction = "Stop" - } - Write-Error @MessageParams - } - } - 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 - } } + + end {} } diff --git a/src/Connection/Test-CSConnection.ps1 b/src/Connection/Test-CSConnection.ps1 index 08b483e..d37400e 100644 --- a/src/Connection/Test-CSConnection.ps1 +++ b/src/Connection/Test-CSConnection.ps1 @@ -33,7 +33,7 @@ function Test-CSConnection { [CmdletBinding()] [OutputType([bool])] - param( + param ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$RemoteHost, diff --git a/src/Store/Test-CredentialStore.ps1 b/src/Store/Test-CredentialStore.ps1 index fc307d5..bc87442 100644 --- a/src/Store/Test-CredentialStore.ps1 +++ b/src/Store/Test-CredentialStore.ps1 @@ -19,7 +19,7 @@ function Test-CredentialStore { #> [CmdletBinding(DefaultParameterSetName = 'Private')] - [OutputBinding([string])] + [OutputBinding([boolean])] param ( [Parameter(Mandatory = $false, ParameterSetName = 'Shared')] [string]$Path,