src/Get-CsCredential.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
<#
.SYNOPSIS
    Get a CredentialStore Entry Credential
.DESCRIPTION
    The Get-CsCredential cmdlet gets the credential 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.
.Example
    Get-CsCredential -FilePath CredentialStore.json -Name LocalServer
    This command gets the credential of the CredentialStore entry named LocalServer in the CredentialStore.json file.
.LINK
    https://github.com/fodonnel/CredentialStore
#>

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

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

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

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