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

标签 azure powershell tags

我只需要在 Keyvault 所在的 ResourceGroup(我不知道其名称)上添加标签。多个 Resourcegroup 中可能存在多个 Keyvault。 这是我尝试的方法:

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 次)。获取 ResourceId 给出了我可以看到的主要错误:


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

非常感谢您的帮助!

最佳答案

我了解您只需要在 Keyvault 所在的资源组上添加标签。

我修改了你的代码:

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
}

关于azure - 如何在特定资源组上添加标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71881193/

相关文章:

azure Entity Framework : what does a connection string usually look like to login?

java - Azure VM 上 Tomcat 7 上的 SSL

azure - 如何下载 Azure BLOB?

wpf - 有没有办法在不同的用户上下文中启动 PowerShell 运行空间?

PHP正则表达式删除所有异常的javascript

javascript - 我正在尝试使用 jQuery/JavaScript 将所有 <table>、<tr> 和 <td> 标签替换为 <div> 标签

java - 增加/减少 Azure 中辅助角色实例的数量

powershell - 在 PowerShell 中将所有写输出通过管道传输到同一个输出文件

具有在单独文件中定义的类的 PowerShell 模块未通过 GitHub Actions 中的 Pester 测试

xslt - 优化<xsl :apply-templates/> for a set of tags