Public/Get-BuildSecrets.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
function Get-BuildSecrets {
    <#
.SYNOPSIS
    Gets all secrets in the current environment
.PARAMETER KeyVaultName
    The name of the key vault containing the environment
.PARAMETER SubscriptionID
    Allows the user to specify a subscription id if required. if not specified, the default subscription will be used.
#>


[CmdletBinding()]
param (
    [Parameter(Mandatory=$true,Position=1)]
    [String]$KeyVaultName,
    [Parameter(Mandatory=$false)]
    [String]$SubscriptionID
)

    # Select the appropriate subscription
    if ($SubscriptionID) {
        Select-AzureRmSubscription -SubscriptionId $SubscriptionID 
    }

    # Get all secrets from the specified key vault
    $Secrets = Get-AzureKeyVaultSecret -VaultName $KeyVaultName | Select-Object -ExpandProperty Name   

    foreach ($Secret in $Secrets) { 

        try {
            # Set Environment Variable
            Get-Item -Path Env:$Secret 
        } catch {
            Write-Output "Could not find secret [$Secret] in current environment"
        }
 
    }
   
}