modifying/updating Azure VM 标记值的代码在 Azure PowerShell Runbook 中不起作用

Codes for modifying/updating Azure VM Tags value doesn't work in Azure PowerShell Runbook

我正在研究 updating/modifying Azure VM 标签值,当我在我的本地 PowerShell ISE 上测试它并连接到我们的 Azure 订阅时代码成功运行(它成功修改了现有的标签值)但是当我在 Azure 门户中的 Azure PowerShell Runbooks 中对其进行了测试。该代码没有按预期运行,而是删除了所有现有标签。

这是我从中搜索到的示例代码 link:

#Get all tags from Resource (VM)
$GetVM = Get-AzureRMVM -Name 'myVM' -ResourceGroupName 'myrg'
$tags = (Get-AzureRMVM -ResourceName $GetVM.Name -ResourceGroupName $GetVM.ResourceGroupName).Tags

#Modifying the value of the Tag
$tags['tag1'] = "value1"

#Updating the Tags
$UpdateTag = Set-AzureRmResource -Tag $tags -ResourceName $GetVM.Name -ResourceGroupName $GetVM.ResourceGroupName -ResourceType Microsoft.Compute/virtualMachines -Force

我可以重现你的问题,正如@4c74356b41 的评论,你应该更新你的模块。在我直接从共享Resources-Modules更新模块AzureRM.ComputeAzureRM.ProfileAzureRM.Resources后---浏览图库---搜索每个模块如AzureRM.Compute--点击导入。更新这些模块后,此 Runbook 有效。