我编写了一个 PowerShell 5.1 脚本,用于从启用了软删除的 Azure Key Vault 中删除机密。我了解到,要成功清除此类 Key Vault 中的机密,您必须:
- 删除秘密。
- 再次删除密钥,这次指定 -InRemovedState 参数。
我的脚本是:
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True)]
[string]$keyvaultName
)
#Remove assorted security items from Key Vault
# Shut up blather about deprecated features
Set-Item -Path Env:\SuppressAzurePowerShellBreakingChangeWarnings -Value 'true'
Write-Output "Starting Removal"
### Remove Secrets
$secrets = Get-AzKeyVaultSecret -VaultName $keyvaultName
### Delete the secrets
foreach ($secret in $secrets) {
Write-Output "Removing $($secret.Name)"
Remove-AzKeyVaultSecret -VaultName $keyvaultName -Name $secret.Name -Force | Wait-Process
if($?) {
Write-Host "Removal of $($secret.Name) succeeded."
}
else {
Write-Host "Removal of $($secret.Name) $vaultName failed."
}
}
### Go back around and purge the secrets
foreach ($secret in $secrets) {
Write-Output "Purging $($secret.Name)"
Remove-AzKeyVaultSecret -VaultName $keyvaultName -Name $secret.Name -InRemovedState -Force | Wait-Process
if($?) {
Write-Host "Purge of $($secret.Name) succeeded."
}
else {
Write-Host "Purge of $($secret.Name) $vaultName failed."
}
}
Write-Output "Remove Complete"
但是,当脚本运行时,偶尔会出现“冲突”错误。出现的错误数量各不相同。我还得到了一个突然出现在这个过程中的证书,我怀疑这是由于我的 Key Vault 中的某种损坏。这是输出,脚本test-run.ps1
调用上面显示的清除脚本代码:
> .\test-run
Starting Removal
Removing QA-testing-certificate
Remove-AzKeyVaultSecret : Operation returned an invalid status code 'Forbidden'
At C:\Users\SESA280186\Desktop\OData\DevOps\Security\Key-Vault-material\keyvault-prototyping\remove-items2.ps1:25
char:4
+ Remove-AzKeyVaultSecret -VaultName $keyvaultName -Name $secret.Nam ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Remove-AzKeyVaultSecret], KeyVaultErrorException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.KeyVault.RemoveAzureKeyVaultSecret
Removal of QA-testing-certificate failed.
Removing QA-testing-secret
Removal of QA-testing-secret succeeded.
Removing QA-testing-secrets2
Removal of QA-testing-secrets2 succeeded.
Removing QA-testing-secrets3
Removal of QA-testing-secrets3 succeeded.
Purging QA-testing-certificate
Remove-AzKeyVaultSecret : Operation returned an invalid status code 'Forbidden'
At C:\Users\SESA280186\Desktop\OData\DevOps\Security\Key-Vault-material\keyvault-prototyping\remove-items2.ps1:39
char:4
+ Remove-AzKeyVaultSecret -VaultName $keyvaultName -Name $secret.Nam ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Remove-AzKeyVaultSecret], KeyVaultErrorException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.KeyVault.RemoveAzureKeyVaultSecret
Purge of QA-testing-certificate failed.
Purging QA-testing-secret
Remove-AzKeyVaultSecret : Operation returned an invalid status code 'Conflict'
At C:\Users\SESA280186\Desktop\OData\DevOps\Security\Key-Vault-material\keyvault-prototyping\remove-items2.ps1:39
char:4
+ Remove-AzKeyVaultSecret -VaultName $keyvaultName -Name $secret.Nam ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Remove-AzKeyVaultSecret], KeyVaultErrorException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.KeyVault.RemoveAzureKeyVaultSecret
Purge of QA-testing-secret failed.
Purging QA-testing-secrets2
Remove-AzKeyVaultSecret : Operation returned an invalid status code 'Conflict'
At C:\Users\SESA280186\Desktop\OData\DevOps\Security\Key-Vault-material\keyvault-prototyping\remove-items2.ps1:39
char:4
+ Remove-AzKeyVaultSecret -VaultName $keyvaultName -Name $secret.Nam ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Remove-AzKeyVaultSecret], KeyVaultErrorException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.KeyVault.RemoveAzureKeyVaultSecret
Purge of QA-testing-secrets2 failed.
Purging QA-testing-secrets3
Purge of QA-testing-secrets3 succeeded.
Remove Complete
任何人都可以就发生的事情提供一些建议吗?我忽略了什么?