src/Get-CsPassword.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<#
.SYNOPSIS
    Get CredentialStore Entry Password
.DESCRIPTION
    The Get-CsPassword cmdlet gets the credential password for a CredentialStore entry by name.
.PARAMETER FilePath
    Specifies the path to the CredentialStore file.
.PARAMETER Name
    Specifies the CredentialStore entry name of of the be retrieved. Wildcards are not permitted.
    This cmdlet throws an error if no entry with that name exists.
.PARAMETER Raw
    Return the Password as a standard unsecure string.
.Example
    Get-CsPassword -FilePath CredentialStore.json -Name LocalServer
    This command gets the password of the CredentialStore entry named LocalServer in the CredentialStore.json file.
.LINK
    https://github.com/fodonnel/CredentialStore
#>

function Get-CsPassword {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [string] $Name,

        [Parameter(Position = 1)]
        [switch] $Raw,

        [ValidateScript({
            if (Test-Path $_) { $true }
            else { throw [System.Management.Automation.ValidationMetadataException] "The path '${_}' does not exist." }
        })] 
        [Parameter(Mandatory = $false, Position = 2)]
        [Alias("File")]
        [string] $FilePath = (Get-CsDefaultStore)
    )

    $entry = Get-CsEntry -Name $Name -FilePath $FilePath
    if ($entry) {
        if ($Raw) {
            return $entry.Credential.GetNetworkCredential().Password
        }
        else {
            return $entry.Credential.Password
        }
    }

    throw "Get-CsPassword : Cannot find any entry with entry name '$Name'."
}