Skip to content

Instantly share code, notes, and snippets.

@manualbashing
Created November 27, 2020 10:15
Show Gist options
  • Save manualbashing/def75e41112a1115a08cbd5004a04867 to your computer and use it in GitHub Desktop.
Save manualbashing/def75e41112a1115a08cbd5004a04867 to your computer and use it in GitHub Desktop.
Remove secrets from Azure Key Vault and purge them immediately
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