Fix Test-CredentialStoreItem returning a terminating fault, if Creden… #51
@ -29,11 +29,11 @@ function Test-CredentialStoreItem {
|
|||||||
[None]
|
[None]
|
||||||
|
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
If (Test-CredentialStoreItem -RemoteHost "Default") {
|
If (Test-CredentialStoreItem -RemoteHost 'Default') {
|
||||||
Get-CredentialStoreItem -RemoteHost "Default"
|
Get-CredentialStoreItem -RemoteHost 'Default'
|
||||||
}
|
}
|
||||||
Else {
|
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
|
.NOTES
|
||||||
@ -44,11 +44,13 @@ function Test-CredentialStoreItem {
|
|||||||
.LINK
|
.LINK
|
||||||
https://github.com/OCram85/PSCredentialStore
|
https://github.com/OCram85/PSCredentialStore
|
||||||
#>
|
#>
|
||||||
[CmdletBinding(DefaultParameterSetName = "Private")]
|
|
||||||
|
[CmdletBinding(DefaultParameterSetName = 'Private')]
|
||||||
[OutputType([Boolean])]
|
[OutputType([Boolean])]
|
||||||
|
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory = $false, ParameterSetName = "Shared")]
|
[Parameter(Mandatory = $false, ParameterSetName = 'Shared')]
|
||||||
[string]$Path = "{0}\PSCredentialStore\CredentialStore.json" -f $env:ProgramData,
|
[string]$Path = '{0}\PSCredentialStore\CredentialStore.json' -f $env:ProgramData,
|
||||||
|
|
||||||
[Parameter(Mandatory = $true)]
|
[Parameter(Mandatory = $true)]
|
||||||
[ValidateNotNullOrEmpty()]
|
[ValidateNotNullOrEmpty()]
|
||||||
@ -58,17 +60,17 @@ function Test-CredentialStoreItem {
|
|||||||
[ValidateNotNullOrEmpty()]
|
[ValidateNotNullOrEmpty()]
|
||||||
[string]$Identifier,
|
[string]$Identifier,
|
||||||
|
|
||||||
[Parameter(Mandatory = $false, ParameterSetName = "Shared")]
|
[Parameter(Mandatory = $false, ParameterSetName = 'Shared')]
|
||||||
[switch]$Shared
|
[switch]$Shared
|
||||||
)
|
)
|
||||||
|
|
||||||
begin {
|
begin {
|
||||||
# Set the CredentialStore for private, shared or custom mode.
|
# Set the CredentialStore for private, shared or custom mode.
|
||||||
Write-Debug ("ParameterSetName: {0}" -f $PSCmdlet.ParameterSetName)
|
Write-Debug ('ParameterSetName: {0}' -f $PSCmdlet.ParameterSetName)
|
||||||
if ($PSCmdlet.ParameterSetName -eq "Private") {
|
if ($PSCmdlet.ParameterSetName -eq 'Private') {
|
||||||
$Path = Get-DefaultCredentialStorePath
|
$Path = Get-DefaultCredentialStorePath
|
||||||
}
|
}
|
||||||
elseif ($PSCmdlet.ParameterSetName -eq "Shared") {
|
elseif ($PSCmdlet.ParameterSetName -eq 'Shared') {
|
||||||
if (!($PSBoundParameters.ContainsKey('Path'))) {
|
if (!($PSBoundParameters.ContainsKey('Path'))) {
|
||||||
$Path = Get-DefaultCredentialStorePath -Shared
|
$Path = Get-DefaultCredentialStorePath -Shared
|
||||||
}
|
}
|
||||||
@ -76,17 +78,29 @@ function Test-CredentialStoreItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
process {
|
process {
|
||||||
if ($Identifier -ne "") {
|
if ($Identifier -ne '') {
|
||||||
$CredentialName = $RemoteHost = "{0}/{1}" -f $Identifier, $RemoteHost
|
$CredentialName = $RemoteHost = '{0}/{1}' -f $Identifier, $RemoteHost
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$CredentialName = $RemoteHost
|
$CredentialName = $RemoteHost
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Test-CredentialStore -Shared -Path $Path) {
|
# Construct the splatting for Test-CredentialStore
|
||||||
$CS = Get-CredentialStore -Shared -Path $Path
|
$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
|
$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
|
return $true
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -94,16 +108,7 @@ function Test-CredentialStoreItem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$MsgParams = @{
|
return $false
|
||||||
ErrorAction = "Stop"
|
|
||||||
Message = "The given credential store ({0}) does not exist!" -f $Path
|
|
||||||
}
|
|
||||||
Write-Error @MsgParams
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
end {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -25,39 +25,46 @@ function Test-CredentialStore {
|
|||||||
.LINK
|
.LINK
|
||||||
https://github.com/OCram85/PSCredentialStore
|
https://github.com/OCram85/PSCredentialStore
|
||||||
#>
|
#>
|
||||||
[CmdletBinding(DefaultParameterSetName = "Private")]
|
[CmdletBinding(DefaultParameterSetName = 'Private')]
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory = $false, ParameterSetName = "Shared")]
|
[Parameter(Mandatory = $false, ParameterSetName = 'Shared')]
|
||||||
[string]$Path,
|
[string]$Path,
|
||||||
|
|
||||||
[Parameter(Mandatory = $true, ParameterSetName = "Shared")]
|
[Parameter(Mandatory = $true, ParameterSetName = 'Shared')]
|
||||||
[switch]$Shared
|
[switch]$Shared
|
||||||
)
|
)
|
||||||
|
|
||||||
begin {
|
begin {
|
||||||
# Set latest Credential Store version
|
# 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 {
|
process {
|
||||||
# Set the CredentialStore for private, shared or custom mode.
|
# Set the CredentialStore for private, shared or custom mode.
|
||||||
Write-Debug ("ParameterSetName: {0}" -f $PSCmdlet.ParameterSetName)
|
Write-Debug ('ParameterSetName: {0}' -f $PSCmdlet.ParameterSetName)
|
||||||
if ($PSCmdlet.ParameterSetName -eq "Private") {
|
|
||||||
$Path = Get-DefaultCredentialStorePath
|
# 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)
|
||||||
}
|
}
|
||||||
elseif ($PSCmdlet.ParameterSetName -eq "Shared") {
|
|
||||||
if (!($PSBoundParameters.ContainsKey('Path'))) {
|
# Get the default CredentialStorePath.
|
||||||
$Path = Get-DefaultCredentialStorePath -Shared
|
$Path = Get-DefaultCredentialStorePath @params
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Write-Verbose -Message ("Path is: {0}" -f $Path)
|
Write-Verbose -Message ('Path is: {0}' -f $Path)
|
||||||
|
|
||||||
if (Test-Path $Path) {
|
if (Test-Path $Path) {
|
||||||
Write-Verbose "CredentialStore in given path found."
|
Write-Verbose 'CredentialStore in given path found.'
|
||||||
return $true
|
return $true
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Write-Verbose "The given CredentialStore does not exist!"
|
Write-Verbose 'The given CredentialStore does not exist!'
|
||||||
return $false
|
return $false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user