From 79a1a214c25e7b9eae744bd541cd3afbed3cc897 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 13:08:21 +0100 Subject: [PATCH 01/21] Replace double-quotes with single-quotes --- src/Connection/Connect-To.ps1 | 84 +++++++++++++++++------------------ 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/src/Connection/Connect-To.ps1 b/src/Connection/Connect-To.ps1 index cd074bd..6ce3e3e 100644 --- a/src/Connection/Connect-To.ps1 +++ b/src/Connection/Connect-To.ps1 @@ -10,7 +10,7 @@ function Connect-To { Specify the host, for which you would like to change the credentials. .PARAMETER Identifier - Defaults to "". Specify a string, which separates two CredentialStoreItems for the + Defaults to ''. Specify a string, which separates two CredentialStoreItems for the same hostname. .PARAMETER Type @@ -48,28 +48,28 @@ function Connect-To { [None] .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' + Connect-To -RemoteHost 'ucscentral.myside.local' -Type 'CiscoUcsCentral' .EXAMPLE - Connect-To -RemoteHost "ftp.myside.local" -Type FTP + Connect-To -RemoteHost 'ftp.myside.local' -Type FTP .EXAMPLE - Connect-To -RemoteHost "fas.myside.local" -Type NetAppFAS + Connect-To -RemoteHost 'fas.myside.local' -Type NetAppFAS .EXAMPLE - Connect-To -RemoteHost "esx01.myside.local" -Type VMware + Connect-To -RemoteHost 'esx01.myside.local' -Type VMware .EXAMPLE - Connect-To -RemoteHost "vCenter.myside.local" -Type CisServer + Connect-To -RemoteHost 'vCenter.myside.local' -Type CisServer .EXAMPLE - Connect-To -RemoteHost "exchange01.myside.local" -Type ExchangeHTTP + Connect-To -RemoteHost 'exchange01.myside.local' -Type ExchangeHTTP .EXAMPLE - Connect-To -RemoteHost "exchange01.myside.local" -Type ExchangeHTTPS + Connect-To -RemoteHost 'exchange01.myside.local' -Type ExchangeHTTPS #> [CmdletBinding(DefaultParameterSetName = 'Private')] @@ -120,7 +120,7 @@ function Connect-To { begin { # 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') { $Path = Get-DefaultCredentialStorePath } @@ -131,7 +131,7 @@ function Connect-To { } switch ($Type) { - "VMware" { + 'VMware' { # Disable the yellow certificate warning, since we haven't replaced the SSL certs for vCenter/ESXi $null = Set-PowerCLIConfiguration -Scope Session -InvalidCertificateAction Ignore -Confirm:$false @@ -149,8 +149,8 @@ function Connect-To { # Check if $Identifier has been defined, in which case we need to use different name for # the lookup of the CredentialStoreItem. try { - if ($Identifier -ne "") { - $RemoteHostIdentifier = "{0}/{1}" -f $Identifier, $RemoteHost + if ($Identifier -ne '') { + $RemoteHostIdentifier = '{0}/{1}' -f $Identifier, $RemoteHost $creds = Get-CredentialStoreItem -Shared -RemoteHost $RemoteHostIdentifier -Path $Path } else { @@ -161,8 +161,8 @@ function Connect-To { catch { $MessageParams = @{ Message = ( - "Unable to look up credential store item for RemoteHost " + - ("{0}/Identifier {1}!" -f $RemoteHost, $Identifier) + 'Unable to look up credential store item for RemoteHost ' + + ('{0}/Identifier {1}!' -f $RemoteHost, $Identifier) ) ErrorAction = 'Stop' } @@ -173,16 +173,16 @@ 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 + Message = 'Please provide valid credentials for RemoteHost {0}!' -f $RemoteHost ErrorAction = 'Stop' } Write-Error @MessageParams } else { switch ($Type) { - "CiscoUcs" { + 'CiscoUcs' { try { $handle = Connect-Ucs -Name $RemoteHost -Credential $creds -ErrorAction 'Stop' -NotDefault $ExecutionContext.SessionState.PSVariable.Set('DefaultUcs', $handle) @@ -190,13 +190,13 @@ function Connect-To { catch { $MessageParams = @{ - Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type + Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type ErrorAction = 'Stop' } Write-Error @MessageParams } } - "CiscoUcsCentral" { + 'CiscoUcsCentral' { try { $handle = Connect-UcsCentral -Name $RemoteHost -Credential $creds -NotDefault $ExecutionContext.SessionState.PSVariable.Set('DefaultUcsCentral', $handle) @@ -210,7 +210,7 @@ function Connect-To { Write-Error @MessageParams } } - "FTP" { + 'FTP' { # First establish the FTP session $WinSCPConParams = @{ Credential = $creds @@ -223,22 +223,22 @@ function Connect-To { $Global:WinSCPSession = New-WinSCPSession -SessionOption $FTPSessionOption } catch { - throw "Could not connect to {0} using {1} protocol!" -f $RemoteHost, $Type + throw 'Could not connect to {0} using {1} protocol!' -f $RemoteHost, $Type } # Check the Connection State if (!($WinSCPSession.Opened)) { # Check the connection state and find out if the session is still open. $MessageParams = @{ Message = ( - ("Connection to {0} using Type {1} " -f $RemoteHost, $Type) + - "was established. But now it seems to be lost!" + ('Connection to {0} using Type {1} ' -f $RemoteHost, $Type) + + 'was established. But now it seems to be lost!' ) ErrorAction = 'Stop' } Write-Error @MessageParams } } - "NetAppFAS" { + 'NetAppFAS' { try { $null = Connect-NcController -Name $RemoteHost -Credential $creds -ErrorAction Stop -HTTPS } @@ -246,13 +246,13 @@ function Connect-To { catch { # Write a error message to the log. $MessageParams = @{ - Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type + Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type ErrorAction = 'Stop' } Write-Error @MessageParams } } - "VMware" { + 'VMware' { try { Connect-VIServer -Server $RemoteHost -Credential $creds -ErrorAction Stop | Out-Null } @@ -260,13 +260,13 @@ function Connect-To { catch { # Write a error message to the log. $MessageParams = @{ - Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type + Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type ErrorAction = 'Stop' } Write-Error @MessageParams } } - "CisServer" { + 'CisServer' { try { if ($PassThru.IsPresent) { Connect-CisServer -Server $RemoteHost -Credential $creds -ErrorAction Stop @@ -280,16 +280,16 @@ function Connect-To { catch { # Write a error message to the log. $MessageParams = @{ - Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type + Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type ErrorAction = 'Stop' } Write-Error @MessageParams } } - "ExchangeHTTP" { + 'ExchangeHTTP' { try { $ConnectionParams = @{ - ConnectionURI = "http://{0}/powershell" -f $RemoteHost + ConnectionURI = 'http://{0}/powershell' -f $RemoteHost ConfigurationName = 'Microsoft.Exchange' Credential = $creds ErrorAction = 'Stop' @@ -301,16 +301,16 @@ function Connect-To { catch { # Write a error message to the log. $MessageParams = @{ - Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type + Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type ErrorAction = 'Stop' } Write-Error @MessageParams } } - "ExchangeHTTPS" { + 'ExchangeHTTPS' { try { $ConnectionParams = @{ - ConnectionURI = "https://{0}/powershell" -f $RemoteHost + ConnectionURI = 'https://{0}/powershell' -f $RemoteHost ConfigurationName = 'Microsoft.Exchange' Credential = $creds ErrorAction = 'Stop' @@ -320,13 +320,13 @@ function Connect-To { catch { # Write a error message to the log. $MessageParams = @{ - Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type + Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type ErrorAction = 'Stop' } Write-Error @MessageParams } } - "SCP" { + 'SCP' { $WinSCPSessionParams = @{ Credential = $creds Hostname = $RemoteHost @@ -337,13 +337,13 @@ function Connect-To { $SessionOption = New-WinSCPSessionOption @WinSCPSessionParams $Global:WinSCPSession = New-WinSCPSession -SessionOption $SessionOption Write-Verbose -Message ( - "SCP Connection established with {0}" -f $Global:WinSCPSession.Hostname + 'SCP Connection established with {0}' -f $Global:WinSCPSession.Hostname ) } catch { # Write a error message to the log. $MessageParams = @{ - Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type + Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type ErrorAction = 'Stop' } Write-Error @MessageParams @@ -353,8 +353,8 @@ 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. " -f $RemoteHost, $Type) + - "But now it seems to be lost!" + ('Connection to {0} using Type {1} was established. ' -f $RemoteHost, $Type) + + 'But now it seems to be lost!' ) ErrorAction = 'Stop' } @@ -364,7 +364,7 @@ function Connect-To { default { # Write a error message to the log. $MessageParams = @{ - Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type + Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type ErrorAction = 'Stop' } Write-Error @MessageParams -- 2.45.2 From 56727afa3e40032e9fda1ddca8cc30b8e8bc3770 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 13:09:15 +0100 Subject: [PATCH 02/21] prettify the parameters --- src/Connection/Connect-To.ps1 | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Connection/Connect-To.ps1 b/src/Connection/Connect-To.ps1 index 6ce3e3e..c1109d2 100644 --- a/src/Connection/Connect-To.ps1 +++ b/src/Connection/Connect-To.ps1 @@ -73,19 +73,21 @@ function Connect-To { #> [CmdletBinding(DefaultParameterSetName = 'Private')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( 'PSAvoidGlobalVars', '', Justification = 'Wrapping existing var from WinSCP module.' )] + param ( [Parameter(Mandatory = $true, ParameterSetName = 'Shared')] [Parameter(Mandatory = $true, ParameterSetName = 'Private')] - [string]$RemoteHost, + [string] $RemoteHost, [Parameter(Mandatory = $false, ParameterSetName = 'Shared')] [Parameter(Mandatory = $false, ParameterSetName = 'Private')] - [string]$Identifier, + [string] $Identifier, [Parameter(Mandatory = $true, ParameterSetName = 'Shared')] [Parameter(Mandatory = $true, ParameterSetName = 'Private')] @@ -100,22 +102,22 @@ function Connect-To { 'ExchangeHTTPS', 'SCP' )] - [string]$Type, + [string] $Type, [Parameter(Mandatory = $False, ParameterSetName = 'Shared')] [Parameter(Mandatory = $False, ParameterSetName = 'Private')] - [PSCredential]$Credentials, + [PSCredential] $Credentials, [Parameter(Mandatory = $true, ParameterSetNAme = 'Shared')] - [switch]$Shared, + [switch] $Shared, [Parameter(Mandatory = $False, ParameterSetName = 'Shared')] [ValidateNotNullOrEmpty()] - [string]$Path, + [string] $Path, [Parameter(Mandatory = $False, ParameterSetName = 'Private')] [Parameter(Mandatory = $False, ParameterSetName = 'Shared')] - [switch]$PassThru + [switch] $PassThru ) begin { -- 2.45.2 From f27f1faad7df5dfed9a9a97df5fcf7cc8daf8bbc Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 13:10:54 +0100 Subject: [PATCH 03/21] Sort the connection list --- src/Connection/Connect-To.ps1 | 94 +++++++++++++++++------------------ 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/src/Connection/Connect-To.ps1 b/src/Connection/Connect-To.ps1 index c1109d2..9d0c45a 100644 --- a/src/Connection/Connect-To.ps1 +++ b/src/Connection/Connect-To.ps1 @@ -18,13 +18,13 @@ function Connect-To { - CiscoUcs - CiscoUcsCentral - - CisServer - ExchangeHTTP - ExchangeHTTPS - FTP - NetAppFAS - SCP - VMware + - VMwareCisServer .PARAMETER Credentials Use this parameter to bypass the stored credentials. Without this parameter Connect-To tries to read the @@ -94,13 +94,13 @@ function Connect-To { [ValidateSet( 'CiscoUcs', 'CiscoUcsCentral', - 'FTP', - 'NetAppFAS', - 'VMware', - 'CisServer', 'ExchangeHTTP', 'ExchangeHTTPS', - 'SCP' + 'FTP', + 'NetAppFAS', + 'SCP', + 'VMware', + 'VMwareCisServer' )] [string] $Type, @@ -212,6 +212,46 @@ function Connect-To { Write-Error @MessageParams } } + 'ExchangeHTTP' { + try { + $ConnectionParams = @{ + ConnectionURI = 'http://{0}/powershell' -f $RemoteHost + ConfigurationName = 'Microsoft.Exchange' + Credential = $creds + ErrorAction = 'Stop' + } + $Global:PSExchangeRemote = New-PSSession @ConnectionParams + # ScriptAnalyzer issue (unused var) workaround. + $null = $Global:PSExchangeRemote + } + catch { + # Write a error message to the log. + $MessageParams = @{ + Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type + ErrorAction = 'Stop' + } + Write-Error @MessageParams + } + } + 'ExchangeHTTPS' { + try { + $ConnectionParams = @{ + ConnectionURI = 'https://{0}/powershell' -f $RemoteHost + ConfigurationName = 'Microsoft.Exchange' + Credential = $creds + ErrorAction = 'Stop' + } + $Global:PSExchangeRemote = New-PSSession @ConnectionParams + } + catch { + # Write a error message to the log. + $MessageParams = @{ + Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type + ErrorAction = 'Stop' + } + Write-Error @MessageParams + } + } 'FTP' { # First establish the FTP session $WinSCPConParams = @{ @@ -268,7 +308,7 @@ function Connect-To { Write-Error @MessageParams } } - 'CisServer' { + 'VMwareCisServer' { try { if ($PassThru.IsPresent) { Connect-CisServer -Server $RemoteHost -Credential $creds -ErrorAction Stop @@ -288,46 +328,6 @@ function Connect-To { Write-Error @MessageParams } } - 'ExchangeHTTP' { - try { - $ConnectionParams = @{ - ConnectionURI = 'http://{0}/powershell' -f $RemoteHost - ConfigurationName = 'Microsoft.Exchange' - Credential = $creds - ErrorAction = 'Stop' - } - $Global:PSExchangeRemote = New-PSSession @ConnectionParams - # ScriptAnalyzer issue (unused var) workaround. - $null = $Global:PSExchangeRemote - } - catch { - # Write a error message to the log. - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams - } - } - 'ExchangeHTTPS' { - try { - $ConnectionParams = @{ - ConnectionURI = 'https://{0}/powershell' -f $RemoteHost - ConfigurationName = 'Microsoft.Exchange' - Credential = $creds - ErrorAction = 'Stop' - } - $Global:PSExchangeRemote = New-PSSession @ConnectionParams - } - catch { - # Write a error message to the log. - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams - } - } 'SCP' { $WinSCPSessionParams = @{ Credential = $creds -- 2.45.2 From 9de752691b7dd72903ceaa04ff2e6d4cabe78939 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 13:11:28 +0100 Subject: [PATCH 04/21] Fix the variable names --- src/Connection/Connect-To.ps1 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Connection/Connect-To.ps1 b/src/Connection/Connect-To.ps1 index 9d0c45a..a8e5d20 100644 --- a/src/Connection/Connect-To.ps1 +++ b/src/Connection/Connect-To.ps1 @@ -220,9 +220,9 @@ function Connect-To { Credential = $creds ErrorAction = 'Stop' } - $Global:PSExchangeRemote = New-PSSession @ConnectionParams + $global:PSExchangeRemote = New-PSSession @ConnectionParams # ScriptAnalyzer issue (unused var) workaround. - $null = $Global:PSExchangeRemote + $null = $global:PSExchangeRemote } catch { # Write a error message to the log. @@ -241,7 +241,7 @@ function Connect-To { Credential = $creds ErrorAction = 'Stop' } - $Global:PSExchangeRemote = New-PSSession @ConnectionParams + $global:PSExchangeRemote = New-PSSession @ConnectionParams } catch { # Write a error message to the log. @@ -262,7 +262,7 @@ function Connect-To { } try { $FTPSessionOption = New-WinSCPSessionOption @WinSCPConParams - $Global:WinSCPSession = New-WinSCPSession -SessionOption $FTPSessionOption + $global:WinSCPSession = New-WinSCPSession -SessionOption $FTPSessionOption } catch { throw 'Could not connect to {0} using {1} protocol!' -f $RemoteHost, $Type @@ -337,9 +337,9 @@ function Connect-To { } try { $SessionOption = New-WinSCPSessionOption @WinSCPSessionParams - $Global:WinSCPSession = New-WinSCPSession -SessionOption $SessionOption + $global:WinSCPSession = New-WinSCPSession -SessionOption $SessionOption Write-Verbose -Message ( - 'SCP Connection established with {0}' -f $Global:WinSCPSession.Hostname + 'SCP Connection established with {0}' -f $global:WinSCPSession.Hostname ) } catch { -- 2.45.2 From b7ad5663dc54eb010bd3388738c442a5a574d7e8 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 13:31:50 +0100 Subject: [PATCH 05/21] Define some defaults for the preferences variables --- src/Connection/Connect-To.ps1 | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Connection/Connect-To.ps1 b/src/Connection/Connect-To.ps1 index a8e5d20..8b2a88e 100644 --- a/src/Connection/Connect-To.ps1 +++ b/src/Connection/Connect-To.ps1 @@ -141,6 +141,11 @@ function Connect-To { $null = Set-PowerCLIConfiguration -Scope Session -ProxyPolicy NoProxy -Confirm:$false } } + + # Set sane defaults for Progress, ErrorAction and InformationPreference + $ProgressPreference = 'SilentlyContinue' + $ErrorActionPreference = 'Stop' + $InformationPreference = 'Continue' } process { -- 2.45.2 From c272b30cb470f7ff1d080e91588eca725b6ba964 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 13:32:12 +0100 Subject: [PATCH 06/21] remove the splattings around Write-Error --- src/Connection/Connect-To.ps1 | 86 +++++++---------------------------- 1 file changed, 17 insertions(+), 69 deletions(-) diff --git a/src/Connection/Connect-To.ps1 b/src/Connection/Connect-To.ps1 index 8b2a88e..af35c63 100644 --- a/src/Connection/Connect-To.ps1 +++ b/src/Connection/Connect-To.ps1 @@ -191,16 +191,12 @@ function Connect-To { switch ($Type) { 'CiscoUcs' { try { - $handle = Connect-Ucs -Name $RemoteHost -Credential $creds -ErrorAction 'Stop' -NotDefault + $handle = Connect-Ucs -Name $RemoteHost -Credential $creds -NotDefault $ExecutionContext.SessionState.PSVariable.Set('DefaultUcs', $handle) } catch { - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } 'CiscoUcsCentral' { @@ -210,11 +206,7 @@ function Connect-To { } catch { - $MessageParams = @{ - Message = ('Unable to connect to {0} using {1}' -f $RemoteHost, $Type) - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using {1}' -f $RemoteHost, $Type) } } 'ExchangeHTTP' { @@ -223,7 +215,6 @@ function Connect-To { ConnectionURI = 'http://{0}/powershell' -f $RemoteHost ConfigurationName = 'Microsoft.Exchange' Credential = $creds - ErrorAction = 'Stop' } $global:PSExchangeRemote = New-PSSession @ConnectionParams # ScriptAnalyzer issue (unused var) workaround. @@ -231,11 +222,7 @@ function Connect-To { } catch { # Write a error message to the log. - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } 'ExchangeHTTPS' { @@ -244,17 +231,12 @@ function Connect-To { ConnectionURI = 'https://{0}/powershell' -f $RemoteHost ConfigurationName = 'Microsoft.Exchange' Credential = $creds - ErrorAction = 'Stop' } $global:PSExchangeRemote = New-PSSession @ConnectionParams } catch { # Write a error message to the log. - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } 'FTP' { @@ -270,19 +252,13 @@ function Connect-To { $global:WinSCPSession = New-WinSCPSession -SessionOption $FTPSessionOption } catch { - throw 'Could not connect to {0} using {1} protocol!' -f $RemoteHost, $Type + Write-Error -Message ('Could not connect to {0} using {1} protocol!' -f $RemoteHost, $Type) } + # Check the Connection State - if (!($WinSCPSession.Opened)) { + if (-not $WinSCPSession.Opened) { # Check the connection state and find out if the session is still open. - $MessageParams = @{ - Message = ( - ('Connection to {0} using Type {1} ' -f $RemoteHost, $Type) + - 'was established. But now it seems to be lost!' - ) - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Connection to {0} using Type {1} was established. But now it seems to be lost!' -f $RemoteHost, $Type) } } 'NetAppFAS' { @@ -292,11 +268,7 @@ function Connect-To { catch { # Write a error message to the log. - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } 'VMware' { @@ -306,31 +278,22 @@ function Connect-To { catch { # Write a error message to the log. - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } 'VMwareCisServer' { try { if ($PassThru.IsPresent) { - Connect-CisServer -Server $RemoteHost -Credential $creds -ErrorAction Stop + Connect-CisServer -Server $RemoteHost -Credential $creds } else { - Connect-CisServer -Server $RemoteHost -Credential $creds -ErrorAction Stop | Out-Null + $null = Connect-CisServer -Server $RemoteHost -Credential $creds } - } catch { # Write a error message to the log. - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } 'SCP' { @@ -349,32 +312,17 @@ function Connect-To { } catch { # Write a error message to the log. - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } # Check the Connection State if (!($WinSCPSession.Opened)) { # Check the connection state and find out if the session is still open. - $MessageParams = @{ - Message = ( - ('Connection to {0} using Type {1} was established. ' -f $RemoteHost, $Type) + - 'But now it seems to be lost!' - ) - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Connection to {0} using Type {1} was established. But now it seems to be lost!' -f $RemoteHost, $Type) } } default { # Write a error message to the log. - $MessageParams = @{ - Message = 'Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } } -- 2.45.2 From a5b430e4ae4bea4fe9c87241c0c8dfb564a2c4b3 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 13:39:08 +0100 Subject: [PATCH 07/21] Revert this to be in sync with max line length PSScriptAnalyzer rule --- src/Connection/Connect-To.ps1 | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Connection/Connect-To.ps1 b/src/Connection/Connect-To.ps1 index af35c63..a15ab77 100644 --- a/src/Connection/Connect-To.ps1 +++ b/src/Connection/Connect-To.ps1 @@ -258,7 +258,9 @@ function Connect-To { # Check the Connection State if (-not $WinSCPSession.Opened) { # Check the connection state and find out if the session is still open. - Write-Error -Message ('Connection to {0} using Type {1} was established. But now it seems to be lost!' -f $RemoteHost, $Type) + $m = 'Connection to {0} using Type {1} was established. ' -f $RemoteHost, $Type + $m += 'But now it seems to be lost!' + Write-Error -Message $m } } 'NetAppFAS' { @@ -317,7 +319,9 @@ function Connect-To { # Check the Connection State if (!($WinSCPSession.Opened)) { # Check the connection state and find out if the session is still open. - Write-Error -Message ('Connection to {0} using Type {1} was established. But now it seems to be lost!' -f $RemoteHost, $Type) + $m = 'Connection to {0} using Type {1} was established. ' -f $RemoteHost, $Type + $m += 'But now it seems to be lost!' + Write-Error -Message $m } } default { -- 2.45.2 From 22631f28362865a3777717d4dd13fb52a72b94f1 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 20:16:31 +0100 Subject: [PATCH 08/21] More reordering/cleanup --- src/Connection/Connect-To.ps1 | 65 ++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/src/Connection/Connect-To.ps1 b/src/Connection/Connect-To.ps1 index a15ab77..95cb8c5 100644 --- a/src/Connection/Connect-To.ps1 +++ b/src/Connection/Connect-To.ps1 @@ -264,38 +264,30 @@ function Connect-To { } } 'NetAppFAS' { + # Construct the splatting for Connect-NcController + $params = @{ + Name = $RemoteHost + Credential = $creds + HTTPS = $true + } + + # Check if -PassThru was passed. If so, a) do not set the default variable and b) return the + # session object to the caller. + if ($PSBoundParameters.ContainsKey('PassThru')) { + $params.Add('Transient', $true) + } + try { - $null = Connect-NcController -Name $RemoteHost -Credential $creds -ErrorAction Stop -HTTPS + $handle = Connect-NcController @params } catch { # Write a error message to the log. Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } - } - 'VMware' { - try { - Connect-VIServer -Server $RemoteHost -Credential $creds -ErrorAction Stop | Out-Null - } - catch { - # Write a error message to the log. - Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) - } - } - 'VMwareCisServer' { - try { - if ($PassThru.IsPresent) { - Connect-CisServer -Server $RemoteHost -Credential $creds - } - else { - $null = Connect-CisServer -Server $RemoteHost -Credential $creds - } - } - - catch { - # Write a error message to the log. - Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) + if ($PSBoundParameters.ContainsKey('PassThru')) { + return $handle } } 'SCP' { @@ -324,6 +316,31 @@ function Connect-To { Write-Error -Message $m } } + 'VMware' { + try { + Connect-VIServer -Server $RemoteHost -Credential $creds -ErrorAction Stop | Out-Null + } + + catch { + # Write a error message to the log. + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) + } + } + 'VMwareCisServer' { + try { + if ($PassThru.IsPresent) { + Connect-CisServer -Server $RemoteHost -Credential $creds + } + else { + $null = Connect-CisServer -Server $RemoteHost -Credential $creds + } + } + + catch { + # Write a error message to the log. + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) + } + } default { # Write a error message to the log. Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) -- 2.45.2 From 4c9c03d7da4fa42aed05df056971bae32f9a603d Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 20:16:48 +0100 Subject: [PATCH 09/21] Implement SGWS --- src/Connection/Connect-To.ps1 | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Connection/Connect-To.ps1 b/src/Connection/Connect-To.ps1 index 95cb8c5..e709d7c 100644 --- a/src/Connection/Connect-To.ps1 +++ b/src/Connection/Connect-To.ps1 @@ -290,6 +290,33 @@ function Connect-To { return $handle } } + 'NetAppSGWS' { + # Construct the splatting for Connect-SgwServer + $params = @{ + Name = $RemoteHost + Credential = $creds + SkipCertificateCheck = $true + } + + # Check if -PassThru was passed. If so, a) do not set the default variable and b) return the + # session object to the caller. + if ($PSBoundParameters.ContainsKey('PassThru')) { + $params.Add('Transient', $true) + } + + try { + $SgwSession = Connect-SgwServer @params + } + + catch { + # Write a error message to the log. + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) + } + + if ($PSBoundParameters.ContainsKey('PassThru')) { + return $SgwSession + } + } 'SCP' { $WinSCPSessionParams = @{ Credential = $creds -- 2.45.2 From 9ed86fd126a13a7578a2e07c384b64fab315abf9 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 20:18:57 +0100 Subject: [PATCH 10/21] Reordering --- src/Connection/Disconnect-From.ps1 | 230 +++++++++++++++-------------- 1 file changed, 118 insertions(+), 112 deletions(-) diff --git a/src/Connection/Disconnect-From.ps1 b/src/Connection/Disconnect-From.ps1 index ef8b157..94ad23b 100644 --- a/src/Connection/Disconnect-From.ps1 +++ b/src/Connection/Disconnect-From.ps1 @@ -10,7 +10,7 @@ function Disconnect-From { Specify the remote endpoint, whose session you would like to terminate. .PARAMETER Identifier - Defaults to "". Specify a string, which separates two CredentialStoreItems for the + Defaults to ''. Specify a string, which separates two CredentialStoreItems for the same hostname. .PARAMETER Type @@ -18,13 +18,13 @@ function Disconnect-From { - CiscoUcs - CiscoUcsCentral - - FTP - - NetAppFAS - - VMware - - CisServer - ExchangeHTTP - ExchangeHTTPS + - FTP + - NetAppFAS - SCP + - VMware + - VMwareCisServer .PARAMETER Force Force the disconnect, even if the disconnect would fail. @@ -36,28 +36,28 @@ function Disconnect-From { [None] .EXAMPLE - Disconnect-From -RemoteHost "ucs.myside.local" -Type CiscoUcs + Disconnect-From -RemoteHost 'ucs.myside.local' -Type CiscoUcs .EXAMPLE - Disconnect-From -RemoteHost "ftp.myside.local" -Type FTP + Disconnect-From -RemoteHost 'ftp.myside.local' -Type FTP .EXAMPLE - Disconnect-From -RemoteHost "fas.myside.local" -Type NetAppFAS + Disconnect-From -RemoteHost 'fas.myside.local' -Type NetAppFAS .EXAMPLE - Disconnect-From -RemoteHost "esx01.myside.local" -Type VMware + Disconnect-From -RemoteHost 'esx01.myside.local' -Type VMware .EXAMPLE - Disconnect-From -RemoteHost "esx01.myside.local" -Type VMware -Force:$True + Disconnect-From -RemoteHost 'esx01.myside.local' -Type VMware -Force:$True .EXAMPLE - Disconnect-From -RemoteHost "vcenter.myside.local" -Type CisServer + Disconnect-From -RemoteHost 'vcenter.myside.local' -Type CisServer .EXAMPLE - Disconnect-From -RemoteHost "exchange01.myside.local" -Type ExchangeHTTP + Disconnect-From -RemoteHost 'exchange01.myside.local' -Type ExchangeHTTP .EXAMPLE - Disconnect-From -RemoteHost "exchange01.myside.local" -Type ExchangeHTTPS + Disconnect-From -RemoteHost 'exchange01.myside.local' -Type ExchangeHTTPS #> [CmdletBinding()] @@ -74,13 +74,13 @@ function Disconnect-From { [ValidateSet( 'CiscoUcs', 'CiscoUcsCentral', - 'FTP', - 'NetAppFAS', - 'VMware', - 'CisServer', 'ExchangeHTTP', 'ExchangeHTTPS', - 'SCP' + 'FTP', + 'NetAppFAS', + 'SCP', + 'VMware', + 'VMwareCisServer' )] [string]$Type, @@ -92,7 +92,101 @@ function Disconnect-From { process { switch -Regex ($Type) { - "VMware" { + '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 + } + } + + 'CiscoUCSCentral' { + try { + $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' + } + 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 + } + } + + # 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 + } + } + + 'SCP' { + if ($Global:WinSCPSession.Opened) { + Remove-WinSCPSession -WinSCPSession $Global:WinSCPSession + } + else { + $MessageParams = @{ + Message = 'There is no open WinSCP Session' + ErrorAction = 'Stop' + } + Write-Error @MessageParams + } + } + + 'VMware' { try { if ($Force) { Disconnect-VIServer -Server $RemoteHost -Confirm:$false -ErrorAction 'Stop' -Force:$true @@ -105,13 +199,14 @@ function Disconnect-From { catch { # Write a error message to the log. $MessageParams = @{ - 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 } } - "CisServer" { + + 'VMwareCisServer' { try { if ($Force) { Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction 'Stop' -Force:$true @@ -124,106 +219,17 @@ 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 - } - } - # 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 - } - } - "CiscoUCSCentral" { - try { - $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 + 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 + Message = 'Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type ErrorAction = 'Stop' } Write-Error @MessageParams -- 2.45.2 From 3105a3230a6776a652289b3d0a82f3f927adc1d4 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 20:25:26 +0100 Subject: [PATCH 11/21] Rework some logic --- src/Connection/Disconnect-From.ps1 | 111 +++++++++++------------------ 1 file changed, 40 insertions(+), 71 deletions(-) diff --git a/src/Connection/Disconnect-From.ps1 b/src/Connection/Disconnect-From.ps1 index 94ad23b..7844792 100644 --- a/src/Connection/Disconnect-From.ps1 +++ b/src/Connection/Disconnect-From.ps1 @@ -88,64 +88,53 @@ function Disconnect-From { [switch]$Force ) - begin {} + begin { + # Set sane defaults for Progress, ErrorAction and InformationPreference + $ProgressPreference = 'SilentlyContinue' + $ErrorActionPreference = 'Stop' + $InformationPreference = 'Continue' + } process { switch -Regex ($Type) { 'CiscoUcs' { try { - Disconnect-Ucs -Ucs $RemoteHost + $null = 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 + Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type) } } 'CiscoUCSCentral' { try { - $handle = Connect-UcsCentral -Name $RemoteHost -Credential $creds -NotDefault - $ExecutionContext.SessionState.PSVariable.Set('DefaultUcsCentral', $handle) + $null = Disconnect-UcsCentral -Name $RemoteHost + $ExecutionContext.SessionState.PSVariable.Set('DefaultUcsCentral', $null) } catch { - $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) } } '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-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type) } } # Check for an existing WinSCP Session var 'FTP' { - if ($Global:WinSCPSession.Opened) { - Remove-WinSCPSession -WinSCPSession $Global:WinSCPSession + if ($global:WinSCPSession.Opened) { + Remove-WinSCPSession -WinSCPSession $global:WinSCPSession } else { - $MessageParams = @{ - Message = 'There is no open WinSCP Session' - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message 'There is no open WinSCP Session' } } @@ -153,86 +142,66 @@ function Disconnect-From { # 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 + $m = 'Setting {0} to $null, which will disconnect NetAppFAS' -f $global:CurrentNcController + Write-Verbose -Message $m + $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 + Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type) } } 'SCP' { - if ($Global:WinSCPSession.Opened) { - Remove-WinSCPSession -WinSCPSession $Global:WinSCPSession + if ($global:WinSCPSession.Opened) { + Remove-WinSCPSession -WinSCPSession $global:WinSCPSession } else { - $MessageParams = @{ - Message = 'There is no open WinSCP Session' - ErrorAction = 'Stop' - } - Write-Error @MessageParams + Write-Error -Message 'There is no open WinSCP Session' } } 'VMware' { + # Construct the splatting for Disconnect-VIServer + $params = @{ + Server = $RemoteHost + Confirm = $false + } + + if ($PSBoundParameters.ContainsKey('Force')) { + $params.Add('Force', $true) + } + try { - if ($Force) { - Disconnect-VIServer -Server $RemoteHost -Confirm:$false -ErrorAction 'Stop' -Force:$true - } - else { - Disconnect-VIServer -Server $RemoteHost -Confirm:$false -ErrorAction 'Stop' - } + $null = Disconnect-VIServer @params } 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 -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type) } } 'VMwareCisServer' { try { if ($Force) { - Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction 'Stop' -Force:$true + $null = Disconnect-CisServer -Server $RemoteHost -Confirm:$false -Force:$true } else { - Disconnect-CisServer -Server $RemoteHost -Confirm:$false -ErrorAction 'Stop' + $null = 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 + Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type) } } 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) } } } -- 2.45.2 From 13351d7e2e6f95cfbca1bf8aa5c3cd74ea0bfde3 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 20:27:10 +0100 Subject: [PATCH 12/21] Isn't a valid parameter for Disconnect-From --- src/Connection/Disconnect-From.ps1 | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Connection/Disconnect-From.ps1 b/src/Connection/Disconnect-From.ps1 index 7844792..1508b81 100644 --- a/src/Connection/Disconnect-From.ps1 +++ b/src/Connection/Disconnect-From.ps1 @@ -9,10 +9,6 @@ function Disconnect-From { .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 Specify the host type of the target. Currently implemented targets are: -- 2.45.2 From 2a51e76b07594c4900ee4b5ae05f7472aa38703d Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 20:27:23 +0100 Subject: [PATCH 13/21] Fix comment-based-help --- src/Connection/Disconnect-From.ps1 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Connection/Disconnect-From.ps1 b/src/Connection/Disconnect-From.ps1 index 1508b81..b98a892 100644 --- a/src/Connection/Disconnect-From.ps1 +++ b/src/Connection/Disconnect-From.ps1 @@ -1,17 +1,16 @@ function Disconnect-From { <# .SYNOPSIS - Terminates a session established with Connect-To using a CredentialStoreItem. + Terminates a session established with Connect-To. .DESCRIPTION - Terminates a session established with Connect-To using a CredentialStoreItem. + Terminates a session established with Connect-To. .PARAMETER RemoteHost Specify the remote endpoint, whose session you would like to terminate. .PARAMETER Type Specify the host type of the target. Currently implemented targets are: - - CiscoUcs - CiscoUcsCentral - ExchangeHTTP -- 2.45.2 From c125ad0acdb862d9dd27030c8ed86e9c299b1336 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 20:27:38 +0100 Subject: [PATCH 14/21] prettify parameters and stuff --- src/Connection/Disconnect-From.ps1 | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Connection/Disconnect-From.ps1 b/src/Connection/Disconnect-From.ps1 index b98a892..0550534 100644 --- a/src/Connection/Disconnect-From.ps1 +++ b/src/Connection/Disconnect-From.ps1 @@ -56,14 +56,16 @@ function Disconnect-From { #> [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( 'PSAvoidGlobalVars', '', Justification = 'Wrapping existing global vars from external modules' )] + param ( [Parameter(Mandatory = $true)] - [string]$RemoteHost, + [string] $RemoteHost, [Parameter(Mandatory = $true)] [ValidateSet( @@ -77,10 +79,10 @@ function Disconnect-From { 'VMware', 'VMwareCisServer' )] - [string]$Type, + [string] $Type, [Parameter(Mandatory = $false)] - [switch]$Force + [switch] $Force ) begin { -- 2.45.2 From 1747bb24a85b68057924d3a192cfe06afbfa30f1 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 20:33:27 +0100 Subject: [PATCH 15/21] Add a bit of spacing. Remove default as there is nothing to do. --- src/Connection/Connect-To.ps1 | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Connection/Connect-To.ps1 b/src/Connection/Connect-To.ps1 index e709d7c..863d4ee 100644 --- a/src/Connection/Connect-To.ps1 +++ b/src/Connection/Connect-To.ps1 @@ -199,6 +199,7 @@ function Connect-To { Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } + 'CiscoUcsCentral' { try { $handle = Connect-UcsCentral -Name $RemoteHost -Credential $creds -NotDefault @@ -209,6 +210,7 @@ function Connect-To { Write-Error -Message ('Unable to connect to {0} using {1}' -f $RemoteHost, $Type) } } + 'ExchangeHTTP' { try { $ConnectionParams = @{ @@ -225,6 +227,7 @@ function Connect-To { Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } + 'ExchangeHTTPS' { try { $ConnectionParams = @{ @@ -239,6 +242,7 @@ function Connect-To { Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } + 'FTP' { # First establish the FTP session $WinSCPConParams = @{ @@ -247,6 +251,7 @@ function Connect-To { Protocol = 'Ftp' FtpMode = 'Passive' } + try { $FTPSessionOption = New-WinSCPSessionOption @WinSCPConParams $global:WinSCPSession = New-WinSCPSession -SessionOption $FTPSessionOption @@ -263,6 +268,7 @@ function Connect-To { Write-Error -Message $m } } + 'NetAppFAS' { # Construct the splatting for Connect-NcController $params = @{ @@ -290,6 +296,7 @@ function Connect-To { return $handle } } + 'NetAppSGWS' { # Construct the splatting for Connect-SgwServer $params = @{ @@ -317,6 +324,7 @@ function Connect-To { return $SgwSession } } + 'SCP' { $WinSCPSessionParams = @{ Credential = $creds @@ -343,6 +351,7 @@ function Connect-To { Write-Error -Message $m } } + 'VMware' { try { Connect-VIServer -Server $RemoteHost -Credential $creds -ErrorAction Stop | Out-Null @@ -353,6 +362,7 @@ function Connect-To { Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } + 'VMwareCisServer' { try { if ($PassThru.IsPresent) { @@ -368,10 +378,6 @@ function Connect-To { Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } } - default { - # Write a error message to the log. - Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) - } } } } -- 2.45.2 From 2399f227c90ba5aea9403d837e5be5a64a9e674b Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 20:34:05 +0100 Subject: [PATCH 16/21] Implement NetAppSGWS --- src/Connection/Disconnect-From.ps1 | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Connection/Disconnect-From.ps1 b/src/Connection/Disconnect-From.ps1 index 0550534..9545268 100644 --- a/src/Connection/Disconnect-From.ps1 +++ b/src/Connection/Disconnect-From.ps1 @@ -17,6 +17,7 @@ function Disconnect-From { - ExchangeHTTPS - FTP - NetAppFAS + - NetAppSGWS - SCP - VMware - VMwareCisServer @@ -75,6 +76,7 @@ function Disconnect-From { 'ExchangeHTTPS', 'FTP', 'NetAppFAS', + 'NetAppSGWS', 'SCP', 'VMware', 'VMwareCisServer' @@ -150,6 +152,18 @@ function Disconnect-From { } } + 'NetAppSGWS' { + try { + $null = Disconnect-SgwServer -Server $RemoteHost + $null = Remove-Variable -Name CurrentSgwServer -Scope Global -ErrorAction 'SilentlyContinue' + } + + catch { + # Write a error message to the log. + Write-Error -Message ('Unable to disconnect from {0} using Type {1}.' -f $RemoteHost, $Type) + } + } + 'SCP' { if ($global:WinSCPSession.Opened) { Remove-WinSCPSession -WinSCPSession $global:WinSCPSession -- 2.45.2 From e3349cc4dd4fd289a0843253b0f1a3a4afa5f577 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 20:35:57 +0100 Subject: [PATCH 17/21] Add external dependencies for NetAppSGWS --- src/PSCredentialStore.psd1 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/PSCredentialStore.psd1 b/src/PSCredentialStore.psd1 index efe91d9..8ef1489 100644 --- a/src/PSCredentialStore.psd1 +++ b/src/PSCredentialStore.psd1 @@ -167,6 +167,10 @@ @{ ModuleName = 'DataONTAP' ModuleVersion = '9.7.1.1' + }, + @{ + ModuleName = 'StorageGRID-WebScale' + ModuleVersion = '7.2.1' } ) -- 2.45.2 From 471a372bf9f6cbfed00551a3183d2ec5186d98f1 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Mon, 22 May 2023 21:37:39 +0200 Subject: [PATCH 18/21] Refine the implementation --- src/Connection/Connect-To.ps1 | 24 +++++++++++++++--------- src/Connection/Disconnect-From.ps1 | 27 +++++++++++++++++---------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/Connection/Connect-To.ps1 b/src/Connection/Connect-To.ps1 index 863d4ee..92275d8 100644 --- a/src/Connection/Connect-To.ps1 +++ b/src/Connection/Connect-To.ps1 @@ -22,6 +22,7 @@ function Connect-To { - ExchangeHTTPS - FTP - NetAppFAS + - NetAppStorageGRID - SCP - VMware - VMwareCisServer @@ -48,28 +49,32 @@ function Connect-To { [None] .EXAMPLE - Connect-To -RemoteHost 'ucs.myside.local' -Type CiscoUcs + Connect-To -RemoteHost 'vc.domain.local' -Type CisServer .EXAMPLE - Connect-To -RemoteHost 'ucscentral.myside.local' -Type 'CiscoUcsCentral' + Connect-To -RemoteHost 'ucs.domain.local' -Type CiscoUcs .EXAMPLE - Connect-To -RemoteHost 'ftp.myside.local' -Type FTP + Connect-To -RemoteHost 'ucs-central.domain.local' -Type 'CiscoUcsCentral' .EXAMPLE - Connect-To -RemoteHost 'fas.myside.local' -Type NetAppFAS + Connect-To -RemoteHost 'exchange01.domain.local' -Type ExchangeHTTP .EXAMPLE - Connect-To -RemoteHost 'esx01.myside.local' -Type VMware + Connect-To -RemoteHost 'exchange01.domain.local' -Type ExchangeHTTPS .EXAMPLE - Connect-To -RemoteHost 'vCenter.myside.local' -Type CisServer + Connect-To -RemoteHost 'ftp.domain.local' -Type FTP .EXAMPLE - Connect-To -RemoteHost 'exchange01.myside.local' -Type ExchangeHTTP + Connect-To -RemoteHost 'fas.domain.local' -Type NetAppFAS .EXAMPLE - Connect-To -RemoteHost 'exchange01.myside.local' -Type ExchangeHTTPS + Connect-To -RemoteHost 'grid.domain.local' -Type NetAppStorageGRID + + .EXAMPLE + Connect-To -RemoteHost 'esx01.domain.local' -Type VMware + #> [CmdletBinding(DefaultParameterSetName = 'Private')] @@ -98,6 +103,7 @@ function Connect-To { 'ExchangeHTTPS', 'FTP', 'NetAppFAS', + 'NetAppStorageGRID', 'SCP', 'VMware', 'VMwareCisServer' @@ -297,7 +303,7 @@ function Connect-To { } } - 'NetAppSGWS' { + 'NetAppStorageGRID' { # Construct the splatting for Connect-SgwServer $params = @{ Name = $RemoteHost diff --git a/src/Connection/Disconnect-From.ps1 b/src/Connection/Disconnect-From.ps1 index 9545268..d32ea61 100644 --- a/src/Connection/Disconnect-From.ps1 +++ b/src/Connection/Disconnect-From.ps1 @@ -17,7 +17,7 @@ function Disconnect-From { - ExchangeHTTPS - FTP - NetAppFAS - - NetAppSGWS + - NetAppStorageGRID - SCP - VMware - VMwareCisServer @@ -32,28 +32,35 @@ function Disconnect-From { [None] .EXAMPLE - Disconnect-From -RemoteHost 'ucs.myside.local' -Type CiscoUcs + Disconnect-From -RemoteHost 'vc.domain.local' -Type CisServer .EXAMPLE - Disconnect-From -RemoteHost 'ftp.myside.local' -Type FTP + Disconnect-From -RemoteHost 'ucs.domain.local' -Type CiscoUcs .EXAMPLE - Disconnect-From -RemoteHost 'fas.myside.local' -Type NetAppFAS + Disconnect-From -RemoteHost 'ucs-central.domain.local' -Type 'CiscoUcsCentral' .EXAMPLE - Disconnect-From -RemoteHost 'esx01.myside.local' -Type VMware + Disconnect-From -RemoteHost 'exchange01.domain.local' -Type ExchangeHTTP .EXAMPLE - Disconnect-From -RemoteHost 'esx01.myside.local' -Type VMware -Force:$True + Disconnect-From -RemoteHost 'exchange01.domain.local' -Type ExchangeHTTPS .EXAMPLE - Disconnect-From -RemoteHost 'vcenter.myside.local' -Type CisServer + Disconnect-From -RemoteHost 'ftp.domain.local' -Type FTP .EXAMPLE - Disconnect-From -RemoteHost 'exchange01.myside.local' -Type ExchangeHTTP + Disconnect-From -RemoteHost 'fas.domain.local' -Type NetAppFAS .EXAMPLE - Disconnect-From -RemoteHost 'exchange01.myside.local' -Type ExchangeHTTPS + Disconnect-From -RemoteHost 'grid.domain.local' -Type NetAppStorageGRID + + .EXAMPLE + Disconnect-From -RemoteHost 'esx01.domain.local' -Type VMware + + .EXAMPLE + Disconnect-From -RemoteHost 'esx01.domain.local' -Type VMware -Force:$True + #> [CmdletBinding()] @@ -76,7 +83,7 @@ function Disconnect-From { 'ExchangeHTTPS', 'FTP', 'NetAppFAS', - 'NetAppSGWS', + 'NetAppStorageGRID', 'SCP', 'VMware', 'VMwareCisServer' -- 2.45.2 From db0fa1cf4ad658e7185172a394efa31bad918190 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Mon, 22 May 2023 21:44:13 +0200 Subject: [PATCH 19/21] Implement a function to "find" a CredentialStoreItem in CredentialStores --- src/Item/Find-CredentialStoreItem.Tests.ps1 | 150 +++++++++++++++++++ src/Item/Find-CredentialStoreItem.ps1 | 152 ++++++++++++++++++++ 2 files changed, 302 insertions(+) create mode 100644 src/Item/Find-CredentialStoreItem.Tests.ps1 create mode 100644 src/Item/Find-CredentialStoreItem.ps1 diff --git a/src/Item/Find-CredentialStoreItem.Tests.ps1 b/src/Item/Find-CredentialStoreItem.Tests.ps1 new file mode 100644 index 0000000..3dec6ea --- /dev/null +++ b/src/Item/Find-CredentialStoreItem.Tests.ps1 @@ -0,0 +1,150 @@ +[Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSAvoidUsingConvertToSecureStringWithPlainText', + '', + Justification = 'just used in pester tests.' +)] +param () + +BeforeAll { + $Repo = Get-RepoPath + Import-Module $Repo.Src.Manifest.Item.FullName -Force + + # Backup existing CredentialStores + $Paths = @(('{0}\AppData\Roaming' -f $env:USERPROFILE), ('{0}\ProgramData\PSCredentialStore\' -f $env:SystemDrive)) + $Files = @('CredentialStore.json', 'PSCredentialStore.pfx') + + foreach ($Filepath in $Paths) { + foreach ($File in $Files) { + $OrgPath = Join-Path -Path $FilePath -ChildPath $File + $NewPath = $OrgPath + '.orig' + if (Test-Path $OrgPath) { + try { + $null = Remove-Item -Path $NewPath -Force -Confirm:$false + $null = Rename-Item -Path $OrgPath -NewName $NewPath -Confirm:$false + } + catch { + $_.Exception.Message | Write-Warning + Write-Error -Message ('Unable to revert {0} to {1}' -f $OrgPath, $NewPath) + } + } + } + } + + # Construct the necessary CredentialStores for the Unit tests. + New-CredentialStore -Force + New-CredentialStore -Shared -Force + + # Construct the necessary CredentialStoreItems for the Unit tests. + $CredentialUserName = 'MyUser' + $CredentialPassword = 'FooBar' | ConvertTo-SecureString -AsPlainText -Force + $Credential = [PSCredential]::new($CredentialUserName, $CredentialPassword) + + # Create the CredentialStoreItems + New-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' -Credential $Credential + New-CredentialStoreItem -Shared -RemoteHost 'test-case-a.domain.my' -Credential $Credential + + New-CredentialStoreItem -Shared -RemoteHost 'test-case-b.domain.my' -Credential $Credential + New-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' -Credential $Credential + + New-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' -Credential $Credential -Identifier 'Foo' + New-CredentialStoreItem -Shared -RemoteHost 'test-case-a.domain.my' -Credential $Credential -Identifier 'Foo' + + New-CredentialStoreItem -Shared -RemoteHost 'test-case-b.domain.my' -Credential $Credential -Identifier 'Foo' + New-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' -Credential $Credential -Identifier 'Foo' +} + +AfterAll { + # Check if the private CredentialStore exists + $Paths = @(('{0}\AppData\Roaming' -f $env:USERPROFILE), ('{0}\ProgramData\PSCredentialStore\' -f $env:SystemDrive)) + $Files = @('CredentialStore.json.orig', 'PSCredentialStore.pfx.orig') + + foreach ($Filepath in $Paths) { + foreach ($File in $Files) { + $OrgPath = Join-Path -Path $FilePath -ChildPath $File + $NewPath = $OrgPath.Replace('.orig', '') + if (Test-Path $OrgPath) { + try { + $null = Remove-Item -Path $NewPath -Force -Confirm:$false -ErrorAction SilentlyContinue + $null = Rename-Item -Path $OrgPath -NewName $NewPath -Confirm:$false + } + catch { + $_.Exception.Message | Write-Warning + Write-Error -Message ('Unable to revert {0} to {1}' -f $OrgPath, $NewPath) + } + } + } + } +} + +Describe 'Find-CredentialStoreItem' { + Context 'Default tests' -Tag 'Default' { + It 'Test Function' { + { Get-Command -Name 'Find-CredentialStoreItem' -Module $Repo.Artifact } | Should -Not -Throw + } + + It 'Test Help' { + { Get-Help -Name 'Find-CredentialStoreItem' } | Should -Not -Throw + } + + It 'Help Content' { + $foo = Get-Help -Name 'Find-CredentialStoreItem' + $foo.Synopsis.Length | Should -BeGreaterThan 5 + $foo.Description.Count | Should -BeGreaterOrEqual 1 + $foo.Description[0].Text.Length | Should -BeGreaterThan 5 + } + } + + Context 'Coding tests' -Tag 'Coding' { + It 'Calling Find-CredentialStoreItem with wrong Type' { + { Find-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' -Type 'Foo' } | Should -Throw + } + + It 'Calling Find-CredentialStoreItem present in both CredentialStores w/o Identifier' { + { Find-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' } | Should -Not -Throw + + $foo = Find-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' + $foo.UserName | Should -Be 'MyUser' + $foo.GetNetworkCredential().Password | Should -Be 'FooBar' + } + + It 'Calling Find-CredentialStoreItem present only in shared CredentialStore w/o Identifier' { + { Find-CredentialStoreItem -RemoteHost 'test-case-b.domain.my' } | Should -Not -Throw + + $foo = Find-CredentialStoreItem -RemoteHost 'test-case-b.domain.my' + $foo.UserName | Should -Be 'MyUser' + $foo.GetNetworkCredential().Password | Should -Be 'FooBar' + } + + It 'Calling Find-CredentialStoreItem present only in private CredentialStore w/o Identifier' { + { Find-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' } | Should -Not -Throw + + $foo = Find-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' + $foo.UserName | Should -Be 'MyUser' + $foo.GetNetworkCredential().Password | Should -Be 'FooBar' + } + + It 'Calling Find-CredentialStoreItem present in both CredentialStores w Identifier' { + { Find-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' -Identifier 'Foo' } | Should -Not -Throw + + $foo = Find-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' -Identifier 'Foo' + $foo.UserName | Should -Be 'MyUser' + $foo.GetNetworkCredential().Password | Should -Be 'FooBar' + } + + It 'Calling Find-CredentialStoreItem present only in shared CredentialStore w/o Identifier' { + { Find-CredentialStoreItem -RemoteHost 'test-case-b.domain.my' -Identifier 'Foo' } | Should -Not -Throw + + $foo = Find-CredentialStoreItem -RemoteHost 'test-case-b.domain.my' -Identifier 'Foo' + $foo.UserName | Should -Be 'MyUser' + $foo.GetNetworkCredential().Password | Should -Be 'FooBar' + } + + It 'Calling Find-CredentialStoreItem present only in private CredentialStore w/o Identifier' { + { Find-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' -Identifier 'Foo' } | Should -Not -Throw + + $foo = Find-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' -Identifier 'Foo' + $foo.UserName | Should -Be 'MyUser' + $foo.GetNetworkCredential().Password | Should -Be 'FooBar' + } + } +} diff --git a/src/Item/Find-CredentialStoreItem.ps1 b/src/Item/Find-CredentialStoreItem.ps1 new file mode 100644 index 0000000..d4936d8 --- /dev/null +++ b/src/Item/Find-CredentialStoreItem.ps1 @@ -0,0 +1,152 @@ +function Find-CredentialStoreItem { + <# + .SYNOPSIS + Locates a CredentialStoreItem in any CredentialStore from a given remote host item. + + .DESCRIPTION + Find the credential object and return it as PSCredential object. + + .PARAMETER RemoteHost + Specify the host, for which you would like to find the credentials. + + .PARAMETER Identifier + Provide a custom identifier to the given remote host key. This enables you to store multiple credentials + for a single remote host entry. For example ad/sys1, ftp/sys1, sql/sys1 + + .PARAMETER Type + Influence in which types of CredentialStore this function will look for a object. List of possible types: + - All (include private and shared CredentialStore) - this is also the default. + - Private (only look in a private CredentialStore) + - Shared (only look in the shared CredentialStore) + + .INPUTS + [None] + + .OUTPUTS + [System.Management.Automation.PSCredential] + + .EXAMPLE + $Credential = Find-CredentialStoreItem -RemoteHost 'support.komm-one.net' -Type 'All' + + .EXAMPLE + $params = @{ + RemoteHost = 'support.komm-one.net' + Type = 'Private' + Identifier = 'PersonId' + } + $Credential = Find-CredentialStoreItem @params + #> + + [CmdletBinding()] + + param( + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [string] $RemoteHost, + + [Parameter(Mandatory = $false)] + [string] $Identifier, + + [Parameter(Mandatory = $false)] + [ValidateSet('All', 'Private', 'Shared')] + [string] $Type = 'All' + ) + + begin { + # Define some defaults for the PreferenceVariables. + $ErrorActionPreference = 'Stop' + $InformationPreference = 'Continue' + $WarningPreference = 'Continue' + $ProgressPreference = 'SilentlyContinue' + + # Construct the CredentialStore list, based on what $Type says. + switch ($Type) { + 'All' { + $CredentialStoreList = @('Private', 'Shared') + break + } + 'Private' { + $CredentialStoreList = @('Private') + } + 'Shared' { + $CredentialStoreList = @('Shared') + } + } + } + + process { + # Now go and look for the CredentialStoreItem. + foreach ($Store in $CredentialStoreList) { + # First make sure, that the CredentialStore exists. Sadly I don't have a way to solve this any better + # programmatically, as PowerShell behaves oddly, if you try and pass an empty splatting to a function. + Write-Verbose -Message ('Checking if CredentialStore of type {0} exists' -f $Store) + + if ($Store -eq 'Private') { + if (-not (Test-CredentialStore)) { + Write-Warning -Message ('CredentialStore of type {0} not found, skipping ahead' -f $Store) + continue + } + } + elseif ($Store -eq 'Shared') { + if (-not (Test-CredentialStore -Shared)) { + Write-Warning -Message ('CredentialStore of type {0} not found, skipping ahead' -f $Store) + continue + } + } + else { + Write-Error -Message ('Invalid CredentialStore type {0} supplied' -f $Store) + continue + } + + # Now that we're here, means we have tested the CredentialStore for existence. We can check, if it + # contains a CredentialStoreItem that we are looking for. + $params = @{ + RemoteHost = $RemoteHost + } + + # Check if the user passed -Identifier, then we add it to the splatting. + if (-not [string]::IsNullOrWhiteSpace($Identifier)) { + $params.Add('Identifier', $Identifier) + } + + # Check the CredentialStore type we're currently looking at. + if ($Store -eq 'Shared') { + $params.Add('Shared', $true) + } + + # Now check if the CredentialStoreItem exists + $message = 'Checking if CredentialStoreItem {0}/{1} exists in CredentialStore {2}' + $argumentlist = @($RemoteHost, $Identifier, $Store) + Write-Verbose -Message ($message -f $argumentlist) + + if (Test-CredentialStoreItem @params) { + $message = 'Looking up CredentialStoreItem {0}/{1} from CredentialStore {2}' + $argumentlist = @($RemoteHost, $Identifier, $Store) + Write-Verbose -Message ($message -f $argumentlist) + + try { + Write-Information -MessageData ($message -f $argumentlist) + # Read the CredentialStoreItem from the CredentialStore and store it in $CredentialObject + $CredentialObject = Get-CredentialStoreItem @params + + # Now finish the loop, as we've found what we're looking for. + break + } + catch { + $_.Exception.Message | Write-Warning + $message = 'Unable to read CredentialStoreItem {0}/{1} from CredentialStore {2}' + $argumentlist = @($RemoteHost, $Identifier, $Store) + + Write-Warning -Message ($message -f $argumentlist) + } + } + } + } + + end { + # Only if we've found a CredentialStoreItem above, return it back to the caller. + if ($null -ne $CredentialObject) { + $CredentialObject + } + } +} -- 2.45.2 From 76a4dd6e99a682ad3c6e2cca35199049dec619c4 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Mon, 22 May 2023 21:49:39 +0200 Subject: [PATCH 20/21] Fix the indentation --- src/Connection/Connect-To.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Connection/Connect-To.ps1 b/src/Connection/Connect-To.ps1 index 92275d8..6385e39 100644 --- a/src/Connection/Connect-To.ps1 +++ b/src/Connection/Connect-To.ps1 @@ -55,7 +55,7 @@ function Connect-To { Connect-To -RemoteHost 'ucs.domain.local' -Type CiscoUcs .EXAMPLE - Connect-To -RemoteHost 'ucs-central.domain.local' -Type 'CiscoUcsCentral' + Connect-To -RemoteHost 'ucs-central.domain.local' -Type 'CiscoUcsCentral' .EXAMPLE Connect-To -RemoteHost 'exchange01.domain.local' -Type ExchangeHTTP -- 2.45.2 From fd6dc42099c4688fce41f025cd3bd8a4434564a5 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Mon, 22 May 2023 21:51:21 +0200 Subject: [PATCH 21/21] Revert "Implement a function to "find" a CredentialStoreItem in CredentialStores" This reverts commit db0fa1cf4ad658e7185172a394efa31bad918190. --- src/Item/Find-CredentialStoreItem.Tests.ps1 | 150 ------------------- src/Item/Find-CredentialStoreItem.ps1 | 152 -------------------- 2 files changed, 302 deletions(-) delete mode 100644 src/Item/Find-CredentialStoreItem.Tests.ps1 delete mode 100644 src/Item/Find-CredentialStoreItem.ps1 diff --git a/src/Item/Find-CredentialStoreItem.Tests.ps1 b/src/Item/Find-CredentialStoreItem.Tests.ps1 deleted file mode 100644 index 3dec6ea..0000000 --- a/src/Item/Find-CredentialStoreItem.Tests.ps1 +++ /dev/null @@ -1,150 +0,0 @@ -[Diagnostics.CodeAnalysis.SuppressMessageAttribute( - 'PSAvoidUsingConvertToSecureStringWithPlainText', - '', - Justification = 'just used in pester tests.' -)] -param () - -BeforeAll { - $Repo = Get-RepoPath - Import-Module $Repo.Src.Manifest.Item.FullName -Force - - # Backup existing CredentialStores - $Paths = @(('{0}\AppData\Roaming' -f $env:USERPROFILE), ('{0}\ProgramData\PSCredentialStore\' -f $env:SystemDrive)) - $Files = @('CredentialStore.json', 'PSCredentialStore.pfx') - - foreach ($Filepath in $Paths) { - foreach ($File in $Files) { - $OrgPath = Join-Path -Path $FilePath -ChildPath $File - $NewPath = $OrgPath + '.orig' - if (Test-Path $OrgPath) { - try { - $null = Remove-Item -Path $NewPath -Force -Confirm:$false - $null = Rename-Item -Path $OrgPath -NewName $NewPath -Confirm:$false - } - catch { - $_.Exception.Message | Write-Warning - Write-Error -Message ('Unable to revert {0} to {1}' -f $OrgPath, $NewPath) - } - } - } - } - - # Construct the necessary CredentialStores for the Unit tests. - New-CredentialStore -Force - New-CredentialStore -Shared -Force - - # Construct the necessary CredentialStoreItems for the Unit tests. - $CredentialUserName = 'MyUser' - $CredentialPassword = 'FooBar' | ConvertTo-SecureString -AsPlainText -Force - $Credential = [PSCredential]::new($CredentialUserName, $CredentialPassword) - - # Create the CredentialStoreItems - New-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' -Credential $Credential - New-CredentialStoreItem -Shared -RemoteHost 'test-case-a.domain.my' -Credential $Credential - - New-CredentialStoreItem -Shared -RemoteHost 'test-case-b.domain.my' -Credential $Credential - New-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' -Credential $Credential - - New-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' -Credential $Credential -Identifier 'Foo' - New-CredentialStoreItem -Shared -RemoteHost 'test-case-a.domain.my' -Credential $Credential -Identifier 'Foo' - - New-CredentialStoreItem -Shared -RemoteHost 'test-case-b.domain.my' -Credential $Credential -Identifier 'Foo' - New-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' -Credential $Credential -Identifier 'Foo' -} - -AfterAll { - # Check if the private CredentialStore exists - $Paths = @(('{0}\AppData\Roaming' -f $env:USERPROFILE), ('{0}\ProgramData\PSCredentialStore\' -f $env:SystemDrive)) - $Files = @('CredentialStore.json.orig', 'PSCredentialStore.pfx.orig') - - foreach ($Filepath in $Paths) { - foreach ($File in $Files) { - $OrgPath = Join-Path -Path $FilePath -ChildPath $File - $NewPath = $OrgPath.Replace('.orig', '') - if (Test-Path $OrgPath) { - try { - $null = Remove-Item -Path $NewPath -Force -Confirm:$false -ErrorAction SilentlyContinue - $null = Rename-Item -Path $OrgPath -NewName $NewPath -Confirm:$false - } - catch { - $_.Exception.Message | Write-Warning - Write-Error -Message ('Unable to revert {0} to {1}' -f $OrgPath, $NewPath) - } - } - } - } -} - -Describe 'Find-CredentialStoreItem' { - Context 'Default tests' -Tag 'Default' { - It 'Test Function' { - { Get-Command -Name 'Find-CredentialStoreItem' -Module $Repo.Artifact } | Should -Not -Throw - } - - It 'Test Help' { - { Get-Help -Name 'Find-CredentialStoreItem' } | Should -Not -Throw - } - - It 'Help Content' { - $foo = Get-Help -Name 'Find-CredentialStoreItem' - $foo.Synopsis.Length | Should -BeGreaterThan 5 - $foo.Description.Count | Should -BeGreaterOrEqual 1 - $foo.Description[0].Text.Length | Should -BeGreaterThan 5 - } - } - - Context 'Coding tests' -Tag 'Coding' { - It 'Calling Find-CredentialStoreItem with wrong Type' { - { Find-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' -Type 'Foo' } | Should -Throw - } - - It 'Calling Find-CredentialStoreItem present in both CredentialStores w/o Identifier' { - { Find-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' } | Should -Not -Throw - - $foo = Find-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' - $foo.UserName | Should -Be 'MyUser' - $foo.GetNetworkCredential().Password | Should -Be 'FooBar' - } - - It 'Calling Find-CredentialStoreItem present only in shared CredentialStore w/o Identifier' { - { Find-CredentialStoreItem -RemoteHost 'test-case-b.domain.my' } | Should -Not -Throw - - $foo = Find-CredentialStoreItem -RemoteHost 'test-case-b.domain.my' - $foo.UserName | Should -Be 'MyUser' - $foo.GetNetworkCredential().Password | Should -Be 'FooBar' - } - - It 'Calling Find-CredentialStoreItem present only in private CredentialStore w/o Identifier' { - { Find-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' } | Should -Not -Throw - - $foo = Find-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' - $foo.UserName | Should -Be 'MyUser' - $foo.GetNetworkCredential().Password | Should -Be 'FooBar' - } - - It 'Calling Find-CredentialStoreItem present in both CredentialStores w Identifier' { - { Find-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' -Identifier 'Foo' } | Should -Not -Throw - - $foo = Find-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' -Identifier 'Foo' - $foo.UserName | Should -Be 'MyUser' - $foo.GetNetworkCredential().Password | Should -Be 'FooBar' - } - - It 'Calling Find-CredentialStoreItem present only in shared CredentialStore w/o Identifier' { - { Find-CredentialStoreItem -RemoteHost 'test-case-b.domain.my' -Identifier 'Foo' } | Should -Not -Throw - - $foo = Find-CredentialStoreItem -RemoteHost 'test-case-b.domain.my' -Identifier 'Foo' - $foo.UserName | Should -Be 'MyUser' - $foo.GetNetworkCredential().Password | Should -Be 'FooBar' - } - - It 'Calling Find-CredentialStoreItem present only in private CredentialStore w/o Identifier' { - { Find-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' -Identifier 'Foo' } | Should -Not -Throw - - $foo = Find-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' -Identifier 'Foo' - $foo.UserName | Should -Be 'MyUser' - $foo.GetNetworkCredential().Password | Should -Be 'FooBar' - } - } -} diff --git a/src/Item/Find-CredentialStoreItem.ps1 b/src/Item/Find-CredentialStoreItem.ps1 deleted file mode 100644 index d4936d8..0000000 --- a/src/Item/Find-CredentialStoreItem.ps1 +++ /dev/null @@ -1,152 +0,0 @@ -function Find-CredentialStoreItem { - <# - .SYNOPSIS - Locates a CredentialStoreItem in any CredentialStore from a given remote host item. - - .DESCRIPTION - Find the credential object and return it as PSCredential object. - - .PARAMETER RemoteHost - Specify the host, for which you would like to find the credentials. - - .PARAMETER Identifier - Provide a custom identifier to the given remote host key. This enables you to store multiple credentials - for a single remote host entry. For example ad/sys1, ftp/sys1, sql/sys1 - - .PARAMETER Type - Influence in which types of CredentialStore this function will look for a object. List of possible types: - - All (include private and shared CredentialStore) - this is also the default. - - Private (only look in a private CredentialStore) - - Shared (only look in the shared CredentialStore) - - .INPUTS - [None] - - .OUTPUTS - [System.Management.Automation.PSCredential] - - .EXAMPLE - $Credential = Find-CredentialStoreItem -RemoteHost 'support.komm-one.net' -Type 'All' - - .EXAMPLE - $params = @{ - RemoteHost = 'support.komm-one.net' - Type = 'Private' - Identifier = 'PersonId' - } - $Credential = Find-CredentialStoreItem @params - #> - - [CmdletBinding()] - - param( - [Parameter(Mandatory = $true)] - [ValidateNotNullOrEmpty()] - [string] $RemoteHost, - - [Parameter(Mandatory = $false)] - [string] $Identifier, - - [Parameter(Mandatory = $false)] - [ValidateSet('All', 'Private', 'Shared')] - [string] $Type = 'All' - ) - - begin { - # Define some defaults for the PreferenceVariables. - $ErrorActionPreference = 'Stop' - $InformationPreference = 'Continue' - $WarningPreference = 'Continue' - $ProgressPreference = 'SilentlyContinue' - - # Construct the CredentialStore list, based on what $Type says. - switch ($Type) { - 'All' { - $CredentialStoreList = @('Private', 'Shared') - break - } - 'Private' { - $CredentialStoreList = @('Private') - } - 'Shared' { - $CredentialStoreList = @('Shared') - } - } - } - - process { - # Now go and look for the CredentialStoreItem. - foreach ($Store in $CredentialStoreList) { - # First make sure, that the CredentialStore exists. Sadly I don't have a way to solve this any better - # programmatically, as PowerShell behaves oddly, if you try and pass an empty splatting to a function. - Write-Verbose -Message ('Checking if CredentialStore of type {0} exists' -f $Store) - - if ($Store -eq 'Private') { - if (-not (Test-CredentialStore)) { - Write-Warning -Message ('CredentialStore of type {0} not found, skipping ahead' -f $Store) - continue - } - } - elseif ($Store -eq 'Shared') { - if (-not (Test-CredentialStore -Shared)) { - Write-Warning -Message ('CredentialStore of type {0} not found, skipping ahead' -f $Store) - continue - } - } - else { - Write-Error -Message ('Invalid CredentialStore type {0} supplied' -f $Store) - continue - } - - # Now that we're here, means we have tested the CredentialStore for existence. We can check, if it - # contains a CredentialStoreItem that we are looking for. - $params = @{ - RemoteHost = $RemoteHost - } - - # Check if the user passed -Identifier, then we add it to the splatting. - if (-not [string]::IsNullOrWhiteSpace($Identifier)) { - $params.Add('Identifier', $Identifier) - } - - # Check the CredentialStore type we're currently looking at. - if ($Store -eq 'Shared') { - $params.Add('Shared', $true) - } - - # Now check if the CredentialStoreItem exists - $message = 'Checking if CredentialStoreItem {0}/{1} exists in CredentialStore {2}' - $argumentlist = @($RemoteHost, $Identifier, $Store) - Write-Verbose -Message ($message -f $argumentlist) - - if (Test-CredentialStoreItem @params) { - $message = 'Looking up CredentialStoreItem {0}/{1} from CredentialStore {2}' - $argumentlist = @($RemoteHost, $Identifier, $Store) - Write-Verbose -Message ($message -f $argumentlist) - - try { - Write-Information -MessageData ($message -f $argumentlist) - # Read the CredentialStoreItem from the CredentialStore and store it in $CredentialObject - $CredentialObject = Get-CredentialStoreItem @params - - # Now finish the loop, as we've found what we're looking for. - break - } - catch { - $_.Exception.Message | Write-Warning - $message = 'Unable to read CredentialStoreItem {0}/{1} from CredentialStore {2}' - $argumentlist = @($RemoteHost, $Identifier, $Store) - - Write-Warning -Message ($message -f $argumentlist) - } - } - } - } - - end { - # Only if we've found a CredentialStoreItem above, return it back to the caller. - if ($null -ne $CredentialObject) { - $CredentialObject - } - } -} -- 2.45.2