VMware Cloud Community
Toniccc
Contributor
Contributor
Jump to solution

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
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could test like this

if ($oldtag.Tag.Name -ne $env)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

You could test like this

if ($oldtag.Tag.Name -ne $env)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Toniccc
Contributor
Contributor
Jump to solution

LucD It works, thank you very much for help!

Reply
0 Kudos