如何在特定资源组上添加标签?

发布于 2025-01-21 13:54:01 字数 1726 浏览 3 评论 0原文

我需要仅在KeyVault所在的资源组(我不知道的名称)上添加标签。在几个资源组中,可能有多个密钥vault。 这就是我尝试的方式:

Set-AzContext -Subscription $subscriptionID
#Add Currentdate on resourcegroup of Keyvault

$CurrentDate = ((Get-Date).ToString('dd-MM-yyyy'))

$Tags = @{'Date' = $Currentdate}


$Resources = (Get-AzKeyVault).ResourceGroupName


Foreach ($Resource in $Resources){

$ResourcegroupName = (Get-AzKeyVault).ResourceGroupName
$ResourcegroupId = (Get-AzResourceGroup -Name $ResourcegroupName).ResourceId
    New-AzTag -ResourceId $ResourcegroupId -Tag $Tags
    }

命令单独工作,但是在这种情况下,它会产生多个错误(以下错误3次)。通过我可以看到的主要错误来获得资源限制:


Get-AzResourceGroup : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Name'. Specified method is not supported.
At line:11 char:47
+ ... sourcegroupId = (Get-AzResourceGroup -Name $ResourcegroupName).Resour ...
+                                                ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-AzResourceGroup], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.GetAzureResourceGroupCmdlet

New-AzTag : Cannot validate argument on parameter 'ResourceId'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:12 char:27
+     New-AzTag -ResourceId $ResourcegroupId -Tag $Tags
+                           ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [New-AzTag], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Tags.Tag.NewAzureTagCommand

非常感谢您的帮助!

I need to add tags on only the ResourceGroups (which I don't know the name of) where a Keyvault is in. It is possible that there are more than one Keyvaults in several Resourcegroups.
This is how I tried:

Set-AzContext -Subscription $subscriptionID
#Add Currentdate on resourcegroup of Keyvault

$CurrentDate = ((Get-Date).ToString('dd-MM-yyyy'))

$Tags = @{'Date' = $Currentdate}


$Resources = (Get-AzKeyVault).ResourceGroupName


Foreach ($Resource in $Resources){

$ResourcegroupName = (Get-AzKeyVault).ResourceGroupName
$ResourcegroupId = (Get-AzResourceGroup -Name $ResourcegroupName).ResourceId
    New-AzTag -ResourceId $ResourcegroupId -Tag $Tags
    }

The commands work separately but in this context it gives multiple errors (below error 3 times). Getting the ResourceId gives by what I can see the main error:


Get-AzResourceGroup : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Name'. Specified method is not supported.
At line:11 char:47
+ ... sourcegroupId = (Get-AzResourceGroup -Name $ResourcegroupName).Resour ...
+                                                ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-AzResourceGroup], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.GetAzureResourceGroupCmdlet

New-AzTag : Cannot validate argument on parameter 'ResourceId'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:12 char:27
+     New-AzTag -ResourceId $ResourcegroupId -Tag $Tags
+                           ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [New-AzTag], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Tags.Tag.NewAzureTagCommand

Thanks a lot for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

又爬满兰若 2025-01-28 13:54:01

我知道您需要仅在密钥vault所在的资源组上添加标签。

我修改了您的代码:

Set-AzContext -Subscription $subscriptionID
#Add Currentdate on resourcegroup of Keyvault

$CurrentDate = ((Get-Date).ToString('dd-MM-yyyy'))

$Tags = @{'Date' = $Currentdate}

$Resources = (Get-AzKeyVault).ResourceGroupName

Foreach ($Resource in $Resources){
    $ResourcegroupId = (Get-AzResourceGroup -Name $Resource).ResourceId
    New-AzTag -ResourceId $ResourcegroupId -Tag $Tags
}

I understand that you need to add tags on only the ResourceGroups where a Keyvault is in.

I modified a bit your code:

Set-AzContext -Subscription $subscriptionID
#Add Currentdate on resourcegroup of Keyvault

$CurrentDate = ((Get-Date).ToString('dd-MM-yyyy'))

$Tags = @{'Date' = $Currentdate}

$Resources = (Get-AzKeyVault).ResourceGroupName

Foreach ($Resource in $Resources){
    $ResourcegroupId = (Get-AzResourceGroup -Name $Resource).ResourceId
    New-AzTag -ResourceId $ResourcegroupId -Tag $Tags
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文