-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotateKeyVaultSecretRunbook.ps1
32 lines (27 loc) · 1.17 KB
/
RotateKeyVaultSecretRunbook.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
param (
[Parameter(Mandatory=$True)]
[string]
$keyVaultName,
[Parameter(Mandatory=$True)]
[string]
$secretName
)
# Authenticate to Azure if running from Azure Automation
Write-Output "Logging in...";
$servicePrincipalConnection = Get-AutomationConnection -Name "AzureRunAsConnection"
Login-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint | Write-Verbose
# Retrieve current secret value
Write-Output "Retrieving secret " $secretName " from vault " $keyVaultName
$secret = Get-AzureKeyVaultSecret -vaultName $keyVaultName -name $secretName
Write-Output "Current secret value is " $secret.SecretValueText
# Generate new secret value and update secret
$guid = New-Guid
Write-Output "New secret value is " $guid
# Add code here to actually update the password on the associated user account in AAD
$secretValue = ConvertTo-SecureString $guid -AsPlainText -Force
Write-Output "Updating secret..."
Set-AzureKeyVaultSecret -VaultName $keyVaultName -Name $secretName -SecretValue $secretValue