Update-AzureRmVM fails when tags are already added to the VM
I'm currently writing a script that handles some common tasks. One of these is a cleanup script for when a vm isn't needed anymore. This script removes the following objects;
- virtual machine
- nic
- blobs
I use module version 1.2.0 of Azure.Compute (currently available via the Web Platform installer)
When testing this script i ran into the following error thrown by UpdateAzureRmVm
Update-AzureRmVM : Invalid tag format. Expect @{Name = "tagName"} or @{Name = "tagName"; Value =
"tagValue"}
The virtual machine that I try to update has a tag associated with it (to group vms for a certain application) and it seems that update-azurermvm has different criteria then new-azurermvm (thats when we set the tag. I use the following script (cut here and there) to create, remove a nic and update the vm for reference:
$VMName = "VM001"
$ResourceGroupName = "rg-001"
$ApplicationName = "ExampleApplicationName"
$Location = "West Europe"
$VirtualMachine = "<A prepped Virtual machine object>"
#Creating of VM. Tags are being accepted here.
New-AzureRmVM `
-ResourceGroupName $ResourceGroupName `
-Location $Location `
-VM $VirtualMachine `
-Tags @{Name="Application";Value=$ApplicationName}
#Getting the virtual machine object sometime later
$VirtualMachine = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName
#Removing the nic
$Nic = "<Nic ID>"
$VirtualMachine = Remove-AzureRmVMNetworkInterface -NetworkInterfaceIDs $Nic -VM $VirtualMachine
#Updating the VM
$VirtualMachine | Update-AzureRmVM
After running the last line the above error is thrown.
Hi Vincent,
I was having the same problem as you do now and here is how I am updating my Tags:
Single Tag: $SATags = @(@{Name = "Business Unit"; Value = $BusinessUnit})
Multiple Tags: $Tags = @(@{Name = "Business Unit"; Value = $BusinessUnit}, @{Name = "Project Code"; Value = $ProjectCode}, @{Name = "Primary Contact"; Value = $PrimaryContact}, @{Name = "Secondary Contact"; Value = $SecondaryContact}, @{Name = "Request No"; Value = $RequestNo}, @{Name = "Start Date"; Value = $StartDate}, @{Name = "End Date"; Value = $EndDate})
Can you try if this works for you? I have to test if something has changed with version 1.0.2
Let me know how it goes. Good Luck.
I will try that but note that I don't update the tag. The initial set during creating goes fine. Then I retrieve a new instance of the virtual machine object, remove another object (nic), update the VM and then the error it thrown. I never touched the tags in the new object, it still is as Get-AzureRmVM returned the information.
This could be due to an update on the cmdlet that block you from updating the tags in certain format (that was passing before).
Could you please try $vm.Tags = $null and then try update it again? In that way, it would be no-op on the tags field. Thanks.
Same-same, unable to update tags for the giving VM.
Not only a single tag overrides all tags resetting it to one tag only and multiple tags update gives an error but also tags cannot be saved to the VM at all. Simple saying, Update-AzureRMVm -VM $vm -Tags $tag does not save the value.
Add @hyonholee
I have exactly the same issue. Update-AzureRmVM just doesn't work if VM contains any tags.
This tripped me up recently on a large number of resizes running from Azure Automation. Any VM with tags doesn't update if the VM object is piped in to Update-AzureRmVm. Easy to repeat and I should think relatively easy to fix?!
I worked around this by using Get-AzureRmResource in order to have the tags in a hashtable and then removed the tags using Set-AzureRmResource -Tag @{}.
I then made my update and when piping back to Update-AzureRmVm, also specified the -Tags parameter, feeding back in the hashtable.
$Data = @"
[
{
"ResourceGroup": "RESOURCEGROUP",
"Name": "VM1",
"Type": "Standard_DS1_v2"
}
]
"@
$VMs = ConvertFrom-Json -InputObject $Data
Foreach ($VM in $VMs) {
# Get and store tags from the resource (as a hashtable)
Try { $CurrentResource = Get-AzureRmResource -ResourceName $VM.Name -ResourceGroupName $VM.ResourceGroup }
Catch { Write-Output "$($VM.Name) in $($VM.ResourceGroup) doesn't seem to exist!"; Continue }
# Remove the tags from the resource because a compute resource with tags can't be updated!!
Set-AzureRmResource -Tag @{} -ResourceId $CurrentResource.ResourceId -Confirm:$false -Force
# Get the VM object
$Current = Get-AzureRmVM -ResourceGroupName $VM.ResourceGroup -Name $VM.Name
# Tell stdout what's happening
Write-Output "Resizing $($Current.Name) from $($Current.HardwareProfile.vmSize)..."
# Update the VM Type
$Current.HardwareProfile.vmSize = $VM.Type
$Current | Update-AzureRmVM -Tags $CurrentResource.Tags -OutVariable ResizeResult | Out-Null
If ($ResizeResult.StatusCode -eq 'OK') {
Write-Output "...now resized to $($Current.HardwareProfile.vmSize)."
}
Else {
Write-Output "...[[[ ERROR ]]]`n$ResizeResult"
}
}