From d41295deefd14dd432eaa05945fa62f5f8168217 Mon Sep 17 00:00:00 2001 From: admwkiv839 Date: Fri, 20 May 2022 20:22:51 +0200 Subject: [PATCH] Fix Test-CredentialStoreItem returning a terminating fault, if CredentialStore doesn't exist. --- src/Item/Test-CredentialStoreItem.ps1 | 55 +++++++++++++++------------ src/Store/Test-CredentialStore.ps1 | 35 ++++++++++------- 2 files changed, 51 insertions(+), 39 deletions(-) diff --git a/src/Item/Test-CredentialStoreItem.ps1 b/src/Item/Test-CredentialStoreItem.ps1 index a248f0e..90e1769 100644 --- a/src/Item/Test-CredentialStoreItem.ps1 +++ b/src/Item/Test-CredentialStoreItem.ps1 @@ -29,11 +29,11 @@ function Test-CredentialStoreItem { [None] .EXAMPLE - If (Test-CredentialStoreItem -RemoteHost "Default") { - Get-CredentialStoreItem -RemoteHost "Default" + If (Test-CredentialStoreItem -RemoteHost 'Default') { + Get-CredentialStoreItem -RemoteHost 'Default' } Else { - Write-Warning ("The given Remote Host {0} does not exist in the credential Store!" -f $RemoteHost) + Write-Warning ('The given Remote Host {0} does not exist in the credential Store!' -f $RemoteHost) } .NOTES @@ -44,11 +44,13 @@ function Test-CredentialStoreItem { .LINK https://github.com/OCram85/PSCredentialStore #> - [CmdletBinding(DefaultParameterSetName = "Private")] + + [CmdletBinding(DefaultParameterSetName = 'Private')] [OutputType([Boolean])] + param( - [Parameter(Mandatory = $false, ParameterSetName = "Shared")] - [string]$Path = "{0}\PSCredentialStore\CredentialStore.json" -f $env:ProgramData, + [Parameter(Mandatory = $false, ParameterSetName = 'Shared')] + [string]$Path = '{0}\PSCredentialStore\CredentialStore.json' -f $env:ProgramData, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] @@ -58,17 +60,17 @@ function Test-CredentialStoreItem { [ValidateNotNullOrEmpty()] [string]$Identifier, - [Parameter(Mandatory = $false, ParameterSetName = "Shared")] + [Parameter(Mandatory = $false, ParameterSetName = 'Shared')] [switch]$Shared ) begin { # Set the CredentialStore for private, shared or custom mode. - Write-Debug ("ParameterSetName: {0}" -f $PSCmdlet.ParameterSetName) - if ($PSCmdlet.ParameterSetName -eq "Private") { + Write-Debug ('ParameterSetName: {0}' -f $PSCmdlet.ParameterSetName) + 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 } @@ -76,17 +78,29 @@ function Test-CredentialStoreItem { } process { - if ($Identifier -ne "") { - $CredentialName = $RemoteHost = "{0}/{1}" -f $Identifier, $RemoteHost + if ($Identifier -ne '') { + $CredentialName = $RemoteHost = '{0}/{1}' -f $Identifier, $RemoteHost } else { $CredentialName = $RemoteHost } - if (Test-CredentialStore -Shared -Path $Path) { - $CS = Get-CredentialStore -Shared -Path $Path + # Construct the splatting for Test-CredentialStore + $params = @{ + Path = $Path + } + + if ($PSBoundParameters.ContainsKey('Shared')) { + $params.Add('Shared', $true) + } + + # Check if the CredentialStore exists, and if it exists try and load the elements from the CredentialStore. + if (Test-CredentialStore @params) { + $CS = Get-CredentialStore @params $CSMembers = Get-Member -InputObject $CS - if (($CSMembers.MemberType -eq "NoteProperty") -and ($CSMembers.Name -contains $CredentialName)) { + + # Now check if the CredentialStore element exists. + if (($CSMembers.MemberType -eq 'NoteProperty') -and ($CSMembers.Name -contains $CredentialName)) { return $true } else { @@ -94,16 +108,7 @@ function Test-CredentialStoreItem { } } else { - $MsgParams = @{ - ErrorAction = "Stop" - Message = "The given credential store ({0}) does not exist!" -f $Path - } - Write-Error @MsgParams + return $false } } - - end { - - } - } diff --git a/src/Store/Test-CredentialStore.ps1 b/src/Store/Test-CredentialStore.ps1 index afbc429..6b7e1b6 100644 --- a/src/Store/Test-CredentialStore.ps1 +++ b/src/Store/Test-CredentialStore.ps1 @@ -25,39 +25,46 @@ function Test-CredentialStore { .LINK https://github.com/OCram85/PSCredentialStore #> - [CmdletBinding(DefaultParameterSetName = "Private")] + [CmdletBinding(DefaultParameterSetName = 'Private')] param( - [Parameter(Mandatory = $false, ParameterSetName = "Shared")] + [Parameter(Mandatory = $false, ParameterSetName = 'Shared')] [string]$Path, - [Parameter(Mandatory = $true, ParameterSetName = "Shared")] + [Parameter(Mandatory = $true, ParameterSetName = 'Shared')] [switch]$Shared ) begin { # Set latest Credential Store version - #Set-Variable -Name "CSVersion" -Value "2.0.0" -Option Constant + #Set-Variable -Name 'CSVersion' -Value '2.0.0' -Option Constant } process { # Set the CredentialStore for private, shared or custom mode. - Write-Debug ("ParameterSetName: {0}" -f $PSCmdlet.ParameterSetName) - if ($PSCmdlet.ParameterSetName -eq "Private") { - $Path = Get-DefaultCredentialStorePath - } - elseif ($PSCmdlet.ParameterSetName -eq "Shared") { - if (!($PSBoundParameters.ContainsKey('Path'))) { - $Path = Get-DefaultCredentialStorePath -Shared + Write-Debug ('ParameterSetName: {0}' -f $PSCmdlet.ParameterSetName) + + # Construct a empty splatting. + $params = @{} + + # Check if the user did not supply a custom path. + if (-not $PSBoundParameters.ContainsKey('Path')) { + # If the user supplied the -Shared parameter, add -Shared to the splatting. + if ($PSCmdlet.ParameterSetName -eq 'Shared') { + $params.Add('Shared', $true) } + + # Get the default CredentialStorePath. + $Path = Get-DefaultCredentialStorePath @params } - Write-Verbose -Message ("Path is: {0}" -f $Path) + + Write-Verbose -Message ('Path is: {0}' -f $Path) if (Test-Path $Path) { - Write-Verbose "CredentialStore in given path found." + Write-Verbose 'CredentialStore in given path found.' return $true } else { - Write-Verbose "The given CredentialStore does not exist!" + Write-Verbose 'The given CredentialStore does not exist!' return $false } } -- 2.45.2