2019-01-16 12:55:29 +01:00
|
|
|
function Test-CredentialStoreItem {
|
2017-09-21 13:32:15 +02:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Checks if the given RemoteHost identifier combination exists in the credential store.
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
Use this cmdlet for basic checks with a single item. Check the item first with this function before
|
|
|
|
you try to interact with it.
|
|
|
|
|
|
|
|
.PARAMETER Path
|
|
|
|
Define a custom credential store you try to read from. Without the `-Path` parameter
|
|
|
|
`Test-CredentialStoreItem` tries to read from the default private store.
|
|
|
|
|
|
|
|
.PARAMETER RemoteHost
|
|
|
|
Specify the host, for which you would like to change the credentials.
|
|
|
|
|
|
|
|
.PARAMETER Identifier
|
|
|
|
Adds an optional identifier to the given RemoteHost. Makes it possible to store multiple credentials
|
|
|
|
for a single host.
|
|
|
|
|
|
|
|
.PARAMETER Shared
|
|
|
|
Switch to shared mode with this param. This enforces the command to work with a shared CredentialStore which
|
|
|
|
can be decrypted across systems.
|
|
|
|
|
|
|
|
.INPUTS
|
|
|
|
[None]
|
|
|
|
|
|
|
|
.OUTPUTS
|
|
|
|
[None]
|
|
|
|
|
|
|
|
.EXAMPLE
|
2022-06-28 08:56:33 +02:00
|
|
|
if (Test-CredentialStoreItem -RemoteHost "Default") {
|
2017-09-21 13:32:15 +02:00
|
|
|
Get-CredentialStoreItem -RemoteHost "Default"
|
|
|
|
}
|
2022-06-28 08:56:33 +02:00
|
|
|
else {
|
2017-09-21 13:32:15 +02:00
|
|
|
Write-Warning ("The given Remote Host {0} does not exist in the credential Store!" -f $RemoteHost)
|
|
|
|
}
|
|
|
|
#>
|
2022-06-28 08:56:33 +02:00
|
|
|
|
|
|
|
[CmdletBinding(DefaultParameterSetName = 'Private')]
|
|
|
|
[OutputType([bool])]
|
|
|
|
param (
|
|
|
|
[Parameter(Mandatory = $false, ParameterSetName = 'Shared')]
|
2017-09-21 13:32:15 +02:00
|
|
|
[string]$Path = "{0}\PSCredentialStore\CredentialStore.json" -f $env:ProgramData,
|
|
|
|
|
|
|
|
[Parameter(Mandatory = $true)]
|
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[string]$RemoteHost,
|
|
|
|
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[string]$Identifier,
|
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
[Parameter(Mandatory = $false, ParameterSetName = 'Shared')]
|
2017-09-21 13:32:15 +02:00
|
|
|
[switch]$Shared
|
|
|
|
)
|
|
|
|
|
2019-01-16 12:55:29 +01:00
|
|
|
begin {
|
|
|
|
# Set the CredentialStore for private, shared or custom mode.
|
|
|
|
Write-Debug ("ParameterSetName: {0}" -f $PSCmdlet.ParameterSetName)
|
2022-06-28 08:56:33 +02:00
|
|
|
if ($PSCmdlet.ParameterSetName -eq 'Private') {
|
2019-01-16 12:55:29 +01:00
|
|
|
$Path = Get-DefaultCredentialStorePath
|
|
|
|
}
|
2022-06-28 08:56:33 +02:00
|
|
|
elseif ($PSCmdlet.ParameterSetName -eq 'Shared') {
|
2019-01-16 12:55:29 +01:00
|
|
|
if (!($PSBoundParameters.ContainsKey('Path'))) {
|
|
|
|
$Path = Get-DefaultCredentialStorePath -Shared
|
|
|
|
}
|
|
|
|
}
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|
|
|
|
|
2019-01-16 12:55:29 +01:00
|
|
|
process {
|
|
|
|
if ($Identifier -ne "") {
|
|
|
|
$CredentialName = $RemoteHost = "{0}/{1}" -f $Identifier, $RemoteHost
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$CredentialName = $RemoteHost
|
|
|
|
}
|
2017-09-21 13:32:15 +02:00
|
|
|
|
2019-01-16 12:55:29 +01:00
|
|
|
if (Test-CredentialStore -Shared -Path $Path) {
|
|
|
|
$CS = Get-CredentialStore -Shared -Path $Path
|
|
|
|
$CSMembers = Get-Member -InputObject $CS
|
2022-06-28 08:56:33 +02:00
|
|
|
if (($CSMembers.MemberType -eq 'NoteProperty') -and ($CSMembers.Name -contains $CredentialName)) {
|
2019-01-16 12:55:29 +01:00
|
|
|
return $true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $false
|
|
|
|
}
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-01-16 12:55:29 +01:00
|
|
|
$MsgParams = @{
|
2022-06-28 08:56:33 +02:00
|
|
|
ErrorAction = 'Stop'
|
2019-01-16 12:55:29 +01:00
|
|
|
Message = "The given credential store ({0}) does not exist!" -f $Path
|
|
|
|
}
|
|
|
|
Write-Error @MsgParams
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-16 12:55:29 +01:00
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
end {}
|
2019-01-16 12:55:29 +01:00
|
|
|
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|