question

SkyHigh-4313 avatar image
0 Votes"
SkyHigh-4313 asked shivapatpi-MSFT answered

I want to update the tags to existing vms

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

shivapatpi-MSFT avatar image
1 Vote"
shivapatpi-MSFT answered

@SkyHigh-4313 ,
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
}



image.png (3.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

shivapatpi-MSFT avatar image
0 Votes"
shivapatpi-MSFT answered shivapatpi-MSFT edited

Hello @SkyHigh-4313 ,
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://docs.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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yes @shivapatpi-MSFT. Please write the script which reads data from the CSV sheet.
Also, when I run the script shared by you it is returning null value for the resourcegroup name value. Please check on that.

Ciao

0 Votes 0 ·

please set the subscription before running the script
az account set --subscription <subid>

Also please post your format of the csv file and sample entries

0 Votes 0 ·