首页 > 解决方案 > 在 Azure KeyVault 机密版本上编辑现有标记值

问题描述

我的目标是在我的 KeyVault 中创建特定机密的新版本,并将当前版本的所有标签复制到新版本。但是,在这样做的同时,我想更改其中一个标签的值并保持其余标签不变。

$CurrentSecret = Get-AzKeyVaultSecret -VaultName $VaultName -Name $SecretName -IncludeVersions
$ActiveSecret = $CurrentSecret[0]

$NewSecret = New-AzServiceBusKey -Name $SecretName -ResourceGroup $ResourceGroupName -Namespace $SBNameSpace -RegenerateKey PrimaryKey

$NewSecretValue = $NewSecret.PrimaryKey

$SecureStringNewKey = ConvertTo-SecureString -String $NewSecretValue -AsPlainText -Force
$EndDate = (Get-Date).AddMonths(12)

$SecretObject = Set-AzKeyVaultSecret -VaultName $VaultName -Name $SecretName -SecretValue $SecureStringNewKey -Expires $EndDate -ContentType $ActiveSecret.ContentType -Tag $ActiveSecret.Tags

PS E:\XXX> $SecretObject.Tags

Name                           Value
----                           -----
KeyType                        Primary
SecretType                     ServiceBusKey

我想将“KeyType”的值更改为“Secondary”并保持一切不变。

我尝试了以下方法,但它按预期覆盖了该值:

$Tags = @{ 'KeyType' = 'Secondary'}

$New = Update-AzKeyVaultSecret -VaultName $VaultName -Name $SecretName -Expires $EndDate -ContentType $ActiveSecret.ContentType -Enable $True -Tag $Tags -PassThru

$New.Tags

Name                           Value
----                           -----
KeyType                        Secondary

最终,原始版本中的标签将增加到 10 个左右。我不期待在更新标签时将枚举器中的所有内容指定为代码中的核心值的方法。我尝试查看 $New.Tags.KeyType。(Length,Clone,GetEnumerator,GetHashCode,CompareTo,Contains,CopyTo,EndsWith,Equals) 但找不到仅将 KeyType 标记的值更新为“次要”的方法

标签: azurepowershelltagsazure-keyvault

解决方案


好吧,如果我理解你的意思,你已经创建了新版本的秘密,你想Tag现在更新它,请在之后尝试下面的命令Set-AzKeyVaultSecret

$sec = Get-AzKeyVaultSecret -VaultName $VaultName -Name $SecretName
$sec.Tags.KeyType = "Secondary"
Update-AzKeyVaultSecret -VaultName $VaultName -Name $VaultName -Tag $sec.Tags

在此处输入图像描述

或者你可以直接使用下面的命令,它也可以工作。

$SecretObject = Set-AzKeyVaultSecret -VaultName $VaultName -Name $SecretName -SecretValue $SecureStringNewKey -Expires $EndDate -ContentType $ActiveSecret.ContentType -Tag $ActiveSecret.Tags
$SecretObject.Tags.KeyType = "Secondary"
Update-AzKeyVaultSecret -VaultName $VaultName -Name mySecret123 -Tag $SecretObject.Tags

推荐阅读