2017-09-21 13:32:15 +02:00
|
|
|
function Get-CredentialStore {
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Reads the complete content of the credential store and returns it as a new object.
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
The content is in a raw format. It means there is no transformation to the different credential types.
|
|
|
|
You can not use the object properties to connect with remote host. Therefore please use
|
|
|
|
Get-CredentialStoreItem.
|
|
|
|
|
|
|
|
.PARAMETER Path
|
|
|
|
Define a custom path to a shared CredentialStore.
|
|
|
|
|
|
|
|
.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
|
|
|
|
[PSObject] Returns the credential store content as PSObject.
|
|
|
|
|
|
|
|
.EXAMPLE
|
|
|
|
$CSContent = Get-CredentialStore -Path "C:\TMP\mystore.json"
|
|
|
|
#>
|
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
[CmdletBinding(DefaultParameterSetName = 'Private')]
|
|
|
|
[OutputType('PSCredentialStore.Store')]
|
|
|
|
param (
|
|
|
|
[Parameter(Mandatory = $false, ParameterSetName = 'Shared')]
|
2017-09-21 13:32:15 +02:00
|
|
|
|
2019-01-16 12:55:29 +01:00
|
|
|
[string]$Path,
|
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
[Parameter(Mandatory = $true, ParameterSetName = 'Shared')]
|
2017-09-21 13:32:15 +02:00
|
|
|
[switch]$Shared
|
|
|
|
)
|
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
begin {}
|
2017-09-21 13:32:15 +02:00
|
|
|
|
2019-01-16 12:55:29 +01:00
|
|
|
process {
|
|
|
|
# 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
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Test-CredentialStore -Path $Path -Shared) {
|
|
|
|
try {
|
|
|
|
$FileContent = Get-Content -Path $Path -Raw
|
|
|
|
$CS = ConvertFrom-Json $FileContent
|
2022-06-28 08:56:33 +02:00
|
|
|
$CS.PSObject.TypeNames.Insert(0, 'PSCredentialStore.Store')
|
|
|
|
Write-Output $CS
|
2019-01-16 12:55:29 +01:00
|
|
|
}
|
|
|
|
catch [System.Exception] {
|
|
|
|
$MessageParams = @{
|
2022-06-28 08:56:33 +02:00
|
|
|
Message = 'Unknown CredentialStore format. Invalid JSON file.'
|
|
|
|
ErrorAction = 'Stop'
|
2019-01-16 12:55:29 +01:00
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2017-09-21 13:32:15 +02:00
|
|
|
$MessageParams = @{
|
2022-06-28 08:56:33 +02:00
|
|
|
Message = 'Could not find the CredentialStore.'
|
|
|
|
ErrorAction = 'Stop'
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|
|
|
|
Write-Error @MessageParams
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|