Skip to content
This repository was archived by the owner on Jul 6, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 30 additions & 25 deletions src/Item/Test-CredentialStoreItem.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()]
Expand All @@ -58,52 +60,55 @@ 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
}
}
}

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 {
return $false
}
}
else {
$MsgParams = @{
ErrorAction = "Stop"
Message = "The given credential store ({0}) does not exist!" -f $Path
}
Write-Error @MsgParams
return $false
}
}

end {

}

}
35 changes: 21 additions & 14 deletions src/Store/Test-CredentialStore.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down