Toniccc
Contributor
Contributor

Check the tag on VM according to the tag in the file

 

Hello,

I have a file with correct tags of the environment (prod,test,dev). I need to read the VM name and tag  from the file and if itsn't equal to the tag in vSphere change it. I wrote a script but I couldn't understand how to compare the data in file (string) with tag on VM which is not a string. Could you help me to fix my script?

$vmlist = Import-CSV 'C:\tools\change_tag_to_vm.csv' -Delimiter ";"

foreach ($item in $vmlist)

{
   $vmname = $item.vmname #VMname
   $env = $item.env #Environment vm_landscape tag
   $oldtag=Get-TagAssignment -Category VM_Landscape -Entity $vmname
   if ($oldtag -ne $env)
    {
     Remove-TagAssignment -tag $oldtag confirm:$false
     New-TagAssignment -Tag $env -Entity $vmname

     }

}

Reply
0 Kudos