I want to update the tags to existing vms

Sky High 311 Reputation points
2022-05-26T03:52:38.547+00:00

Hi Team,

I want to update tags to all VMS in our tenant.
I only have vm names in the CSV/Excel format as input. However, I know the tags to be updated. Also we have existing tags attached to these VMS. I would want to add few tags to the VMS. How can I achieve this using powershell in one go.

For the command to get executed, it needs resourcegroup name and resourceid. How do we get this.

Ciao

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,101 questions
0 comments No comments
{count} votes

Accepted answer
  1. shiva patpi 13,131 Reputation points Microsoft Employee
    2022-05-26T20:20:39.413+00:00

    @Sky High ,
    Here is the complete script:

    Please Note:
    Script was written based upon the below CSV format, if you format is different please modify it accordingly !

    205927-image.png

    $csv = import-csv ".\vmswithtags.csv" #Read all the VMs and their tags to be applied (Please see the format)
    $listOfVMsAndTags = @{} #Hashtable to store all the existing VMs & Corresponding Tags which are read from .csv file
    foreach($vm in $csv) {

    $listOfVMsAndTags.Add($vm.vmname ,$vm.tag)

    }

    $VMResources = Get-AzVM #Get all the VMs, Resource groupnames
    foreach($VMResource in $VMResources)
    {

    Get existing TAGS

    $existingTags = (Get-AzResource -ResourceGroupName $VMResource.ResourceGroupName -Name $VMResource.Name).Tags

    Search and find the corresponding tags from the hashtable

    $tagsTobeApplied = $listOfVMsAndTags[$VMResource.Name]

    Append to existing tags variable

    $existingTags += @{$tagsTobeApplied.Split('=')[0]=$tagsTobeApplied.Split('=')1}

    Set back the tags

    Set-AzResource -ResourceGroupName $VMResource.ResourceGroupName -name $VMResource.ResourceGroupName -ResourceType "Microsoft.Compute/VirtualMachines" -Tag $existingTags
    }

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. shiva patpi 13,131 Reputation points Microsoft Employee
    2022-05-26T15:48:18.28+00:00

    Hello @Sky High ,
    Thanks for reaching out to Microsoft Q&A.
    Below is part of the script will get all the VMs and their corresponding resource groups - get their existing tags and update with required tag;

    $VMResources = Get-AzVM
    foreach($VMResource in $VMResources)
    {
    Write-Host $VMResource.ResourceGroupName
    $tags = (Get-AzResource -ResourceGroupName $VMResource.ResourceGroupName -Name $VMResource.Name).Tags
    $tags += @{test2="test2"}
    Set-AzResource -ResourceGroupName $VMResource.ResourceGroupName -name $VMResource.ResourceGroupName -ResourceType "Microsoft.Compute/VirtualMachines" -Tag $tags
    }

    Reference Document: https://learn.microsoft.com/en-us/azure/virtual-machines/tag-powershell

    You might want to write remaining part of the script which will loop through csv file - get the corresponding VMs & Tags , compare the VM names and update only those tags accordingly.

    Let us know if you need help in writing the other part of the script.