-
Notifications
You must be signed in to change notification settings - Fork 7
/
map-kv-to-env-vars.ps1
41 lines (36 loc) · 1.15 KB
/
map-kv-to-env-vars.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
<#
.SYNOPSIS
Get all secrets from Azure Key Vault and map them to PowerShell environment
variables.
.PARAMETER KeyVault
Name of the Azure Key Vault.
.PARAMETER Subscription
Name of the Azure subscription. If omitted, the default subscription is
selected.
#>
param (
[Parameter(Mandatory, HelpMessage="Name of the Azure Key Vault")]
[string]
$KeyVault,
[Parameter(HelpMessage="Name of the Azure subscription")]
[string]
$Subscription
)
$ErrorActionPreference = "Stop"
Write-Information "Logging into Azure ..."
if ($Subscription) {
Connect-AzAccount -Subscription $Subscription
} else {
Connect-AzAccount
}
Write-Information "Geting secret list from Key Vault: $KeyVault ..."
$secrets = Get-AzKeyVaultSecret -VaultName $KeyVault -Name "*"
$i = 1;
foreach ($secret in $secrets) {
$percentComplete = $i/$secrets.Count*100
$envVarName = $secret.Name.Replace("-", "_")
Write-Progress "Mapping secrets to environment variables ..." -Status $envVarName -PercentComplete $percentComplete
$secretValuePlain = Get-AzKeyVaultSecret -VaultName $KeyVault -Name $secret.Name -AsPlainText
Set-Item -Path env:$envVarName -Value $secretValuePlain
$i++
}