编辑 Azure KeyVault 机密版本上的现有标记值

Editing an existing tag value on a Azure KeyVault secret version

我的目标是在我的 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。 (长度、克隆、GetEnumerator、GetHashCode、CompareTo、包含、CopyTo、EndsWith、等于) 但找不到将 KeyType 标记的值更新为 'Secondary'

的方法

好吧,如果我没有理解你的意思,你已经创建了新版本的秘密,你现在想更新它的 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