src/Set-CsKeyVaultEntry.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
49
50
51
52
53
54
55
56
57
58
59
60
61
<#
.SYNOPSIS
    Set CredentialStore Entries in an Azure Key Vault
.DESCRIPTION
    The Set-CsKeyVaultEntry cmdlet adds or updates CredentialStore entries in an Azure KeyVault.
    A user must already be authenticated with Azure to run this command.
.PARAMETER VaultName
    Specifies the name of the keyvault
.PARAMETER Name
    Specifies the name of entry to be added or updated.
.PARAMETER Description
    A description of the CredentialStore entry.
.PARAMETER Credential
    Specifies the PSCredential of the CredentialStore entry.
.Example
    Set-CsKeyVaultEntry -VaultName myVault -Name LocalServer -Credential $cred
    This command sets the CredentialStore entry named LocalServer in the myVault Key Vault.

.LINK
    https://github.com/fodonnel/CredentialStore
#>


function Set-CsKeyVaultEntry {
    [CmdletBinding(SupportsShouldProcess = $true)]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUsePSCredentialType", "")]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [string] $VaultName,

        [ValidateScript( {
            if (Test-CsEntryName $_) { $true }
            else { throw [System.Management.Automation.ValidationMetadataException] "The name '${_}' is invalid." }
        })]
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 1)]
        [string] $Name,

        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 2)]
        [System.Management.Automation.Credential()]
        [PSCredential] $Credential,

        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, Position = 3)]
        [string] $Description
    )

    process {
        if ($pscmdlet.ShouldProcess($Name)) {
            $params = @{
                Name        = $Name
                VaultName   = $VaultName
                SecretValue = $Credential.password
                ContentType = "CredentialStore"
                Tag         = @{
                    Username    = $Credential.Username
                    Description = $Description
                }
            }

            Set-AzureKeyVaultSecret @params
        }
    }
}