-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathKeyVaultEnumerator.ps1
74 lines (66 loc) · 2.29 KB
/
KeyVaultEnumerator.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
# Azure Key Vault Enumerator
# (C) 2018-2019 Matt Burrough
# v1.2
# Requires the Azure PowerShell cmdlets be installed.
# See https://github.com/Azure/azure-powershell/ for details.
# Before running the script:
# * Run: Import-Module Azure
# * Authenticate to Azure in PowerShell
# * You may also need to run: Set-ExecutionPolicy -Scope Process Unrestricted
$keyvaults = Get-AzureRmKeyVault
foreach($keyvault in $keyvaults)
{
$vaultName = $keyvault.VaultName
Write-Output "----- Vault: $vaultName -----"
Try
{
$secrets = Get-AzureKeyVaultSecret -VaultName $vaultName -ErrorAction Stop
if($secrets -ne $null)
{
Write-Output "SecretName: SecretValueText"
foreach ($secret in $secrets)
{
$value = Get-AzureKeyVaultSecret -VaultName $vaultName -Name $secret.Name
Write-Output "$($secret.Name): $($value.SecretValueText)"
}
}
$keys = Get-AzureKeyVaultKey -VaultName $vaultName -ErrorAction Stop
if($keys -ne $null)
{
Write-Output ""
Write-Output "KeyName: KeyValue"
foreach ($key in $keys)
{
$value = Get-AzureKeyVaultKey -VaultName $vaultName -KeyName $key.Name
Write-Output "$($key.Name): $($value.Key.ToString())"
}
}
$certs = Get-AzureKeyVaultCertificate -VaultName $vaultName
if($certs -ne $null)
{
foreach ($cert in $certs)
{
$cn = $cert.Name
$c = Get-AzureKeyVaultCertificate -VaultName $vaultName -Name $cn
$x509 = $c.Certificate
Write-Output $c
$privkey = (Get-AzureKeyVaultSecret -VaultName $vaultName -Name $cn).SecretValueText
Write-Output "Private Key:"
Write-Output $privkey
Write-Output ""
Write-Output "Exporting Public Key to $cn.cer..."
Export-Certificate -Type CERT -Cert $x509 -FilePath "$cn.cer"
Write-Output "Exporting Private Key to $cn.pfx..."
$privbytes = [Convert]::FromBase64String($privkey)
[IO.File]::WriteAllBytes("$pwd\$cn.pfx", $privbytes)
Write-Output "----------------------------------------------"
}
}
}
Catch
{
Write-Output "Could not read from vault."
}
Write-Output ""
}
Write-Output ""