Created
November 27, 2020 10:15
-
-
Save manualbashing/def75e41112a1115a08cbd5004a04867 to your computer and use it in GitHub Desktop.
Remove secrets from Azure Key Vault and purge them immediately
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Remove-AzKeyVaultSecretAndPurge { | |
[CmdletBinding()] | |
param ( | |
# Name of the key vault | |
[Parameter(Mandatory)] | |
[string] | |
$VaultName, | |
# Name of the secret in removed state | |
[Parameter(Mandatory)] | |
[string] | |
$Name, | |
# Number of attempts before giving up | |
[int] | |
$Try = 5 | |
) | |
Remove-AzKeyVaultSecret -VaultName $VaultName -Name $Name -Force | |
$i = 0 | |
while ($i -lt $Try) { | |
try { | |
$i += 1 | |
Remove-AzKeyVaultSecret -VaultName $VaultName -Name $Name -InRemovedState -Force -ErrorAction Stop | |
break | |
} catch [Microsoft.Azure.KeyVault.Models.KeyVaultErrorException] { | |
if ($_ -notmatch 'Conflict') { | |
throw $_ | |
} | |
Write-Host "[$i] Secret is still being deleted. Will try again in 5 sec." | |
Start-Sleep -Seconds 5 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment