Public/Proxy/Get-AzKeyVaultSecret.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
function Get-AzKeyVaultSecret {
    <#
    .PARAMETER Auto
    Will retrieve the secret and form back info the object that was set.
 
    .PARAMETER Expand
    Will return just the retrieved secret.
 
    .EXAMPLE
    Get-AzKeyVaultSecret -Auto -Name 'Secret'
 
    .NOTES
    General notes
    #>


    <#
    .PARAMETER Auto
    Will retrieve the secret and form back info the object that was set.
 
    .PARAMETER Expand
    Will return just the retrieved secret.
 
    .ForwardHelpTargetName Az.Keyvault\Get-AzKeyVaultSecret
    .ForwardHelpCategory Cmdlet
    #>


    [CmdletBinding(DefaultParameterSetName='ByVaultName')]
    param(
        [Parameter(ParameterSetName='BySecretName', Position=0, HelpMessage='Vault name. Cmdlet constructs the FQDN of a vault based on the name and currently selected environment.')]
        [Parameter(ParameterSetName='ByVaultName', Position=0, HelpMessage='Vault name. Cmdlet constructs the FQDN of a vault based on the name and currently selected environment.')]
        [Parameter(ParameterSetName='BySecretVersions', Position=0, HelpMessage='Vault name. Cmdlet constructs the FQDN of a vault based on the name and currently selected environment.')]
        [ValidateNotNullOrEmpty()]
        [string]
        ${VaultName},

        [Parameter(ParameterSetName='ByInputObjectVaultName', Mandatory=$true, Position=0, ValueFromPipeline=$true, HelpMessage='KeyVault Object.')]
        [Parameter(ParameterSetName='ByInputObjectSecretName', Mandatory=$true, Position=0, ValueFromPipeline=$true, HelpMessage='KeyVault Object.')]
        [Parameter(ParameterSetName='ByInputObjectSecretVersions', Mandatory=$true, Position=0, ValueFromPipeline=$true, HelpMessage='KeyVault Object.')]
        [ValidateNotNullOrEmpty()]
        [Microsoft.Azure.Commands.KeyVault.Models.PSKeyVault]
        ${InputObject},

        [Parameter(ParameterSetName='ByResourceIdVaultName', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true, HelpMessage='KeyVault Resource Id.')]
        [Parameter(ParameterSetName='ByResourceIdSecretName', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true, HelpMessage='KeyVault Resource Id.')]
        [Parameter(ParameterSetName='ByResourceIdSecretVersions', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true, HelpMessage='KeyVault Resource Id.')]
        [ValidateNotNullOrEmpty()]
        [string]
        ${ResourceId},

        [Parameter(ParameterSetName='ByVaultName', Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByInputObjectVaultName', Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByResourceIdVaultName', Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='BySecretName', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByInputObjectSecretName', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByResourceIdSecretName', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='BySecretVersions', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByInputObjectSecretVersions', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByResourceIdSecretVersions', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Alias('SecretName')]
        [ValidateNotNullOrEmpty()]
        [string]
        ${Name},

        [Parameter(ParameterSetName='BySecretName', Mandatory=$true, Position=2, HelpMessage='Secret version. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment, secret name and secret version.')]
        [Parameter(ParameterSetName='ByInputObjectSecretName', Mandatory=$true, Position=2, HelpMessage='Secret version. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment, secret name and secret version.')]
        [Parameter(ParameterSetName='ByResourceIdSecretName', Mandatory=$true, Position=2, HelpMessage='Secret version. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment, secret name and secret version.')]
        [Alias('SecretVersion')]
        [string]
        ${Version},

        [Parameter(ParameterSetName='BySecretVersions', Mandatory=$true, HelpMessage='Specifies whether to include the versions of the secret in the output.')]
        [Parameter(ParameterSetName='ByInputObjectSecretVersions', Mandatory=$true, HelpMessage='Specifies whether to include the versions of the secret in the output.')]
        [Parameter(ParameterSetName='ByResourceIdSecretVersions', Mandatory=$true, HelpMessage='Specifies whether to include the versions of the secret in the output.')]
        [switch]
        ${IncludeVersions},

        [Parameter(ParameterSetName='ByVaultName', HelpMessage='Specifies whether to show the previously deleted secrets in the output.')]
        [Parameter(ParameterSetName='ByInputObjectVaultName', HelpMessage='Specifies whether to show the previously deleted secrets in the output.')]
        [Parameter(ParameterSetName='ByResourceIdVaultName', HelpMessage='Specifies whether to show the previously deleted secrets in the output.')]
        [switch]
        ${InRemovedState},

        [Parameter(HelpMessage='The credentials, account, tenant, and subscription used for communication with Azure.')]
        [Alias('AzContext','AzureRmContext','AzureCredential')]
        [Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core.IAzureContextContainer]
        ${DefaultProfile},

        [Parameter(ParameterSetName='ByVaultName', Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByInputObjectVaultName', Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByResourceIdVaultName', Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='BySecretName', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByInputObjectSecretName', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByResourceIdSecretName', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='BySecretVersions', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByInputObjectSecretVersions', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByResourceIdSecretVersions', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [switch]
        ${Auto},

        [Parameter(ParameterSetName='ByVaultName', Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByInputObjectVaultName', Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByResourceIdVaultName', Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='BySecretName', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByInputObjectSecretName', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByResourceIdSecretName', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='BySecretVersions', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByInputObjectSecretVersions', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [Parameter(ParameterSetName='ByResourceIdSecretVersions', Mandatory=$true, Position=1, HelpMessage='Secret name. Cmdlet constructs the FQDN of a secret from vault name, currently selected environment and secret name.')]
        [switch]
        ${Expand}
    )

    begin
    {
        try {
            $outBuffer = $null
            if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
            {
                $PSBoundParameters['OutBuffer'] = 1
            }
            $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Az.Keyvault\Get-AzKeyVaultSecret', [System.Management.Automation.CommandTypes]::Cmdlet)

            if($PSBoundParameters['Auto']) {
                $null = $PSBoundParameters.Remove('Auto')

                $Expand = $false
                if ($PSBoundParameters['Expand']) {
                    $Expand = $true
                    $null = $PSBoundParameters.Remove('Expand')
                }

                if (!$PSBoundParameters.VaultName) { $PSBoundParameters.VaultName = $Script:KeyVault.VaultName }

                $scriptCmd = {
                    & $wrappedCmd @PSBoundParameters | ForEach-Object {
                        if ($_.ContentType -eq 'PSCredential') {
                            $UserName = $_.Tags['UserName']
                            $_ | Add-Member -NotePropertyName 'Auto' -NotePropertyValue $(New-Object System.Management.Automation.PSCredential ($UserName, $_.SecretValue))
                            if ($Expand) {
                                return New-Object System.Management.Automation.PSCredential ($UserName, $_.SecretValue)
                            }
                        }
                        elseif ($_.ContentType -eq 'JSON') {
                            $SecretValue = $_.SecretValue | Convert-SecureStringToString
                            $SecretObject = $SecretValue | ConvertFrom-Json

                            if ($_.Tags) {
                                foreach($Tag in $_.Tags.GetEnumerator()) {
                                    if ($Tag.Value -eq 'PSCred') {
                                        _ReplaceWithPSCred -SecretName $Tag.Name -SecretObject $SecretObject -VaultName $VaultName
                                    }
                                }
                            }
                            $_ | Add-Member -NotePropertyName 'Auto' -NotePropertyValue $SecretObject
                            if ($Expand) {
                                return $SecretObject
                            }
                        }
                        elseif ($_.ContentType -eq 'String') {
                            $_ | Add-Member -NotePropertyName 'Auto' -NotePropertyValue $_.SecretValue
                            if ($Expand) {
                                return $_.SecretValue
                            }
                        }
                        elseif ($Expand) {
                            return $_.SecretValue
                        }
                        $_
                    }
                }
            } else {
                $scriptCmd = {& $wrappedCmd @PSBoundParameters }
            }
            $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
            $steppablePipeline.Begin($PSCmdlet)

        } catch {
            throw
        }
    }

    process
    {
        try {
            $steppablePipeline.Process($_)
        } catch {
            throw
        }
    }

    end
    {
        try {
            $steppablePipeline.End()
        } catch {
            throw
        }
    }
}