VMware Cloud Community
erki72
Enthusiast
Enthusiast

Replace Tag from mutiple VMs (CSV)

I have a CSV with the following content:
vmname,oldtag,newtag

vmname - is the affected VM
oldtag  - tag to be removed
newtag - Tag which should be added (is available in the vCenter)

Import-Csv -path "filepath" | ForEach-Object {
Remove-TagAssignment (Get-VM $($_.vmname) | Get-TagAssignment -Category $($_.oldtag)) -Confirm:$false
New-TagAssignment -Tag "$($_.newtag)" -Entity "$($_.vmname)" -Confirm:$false
}


Message in PowerCLI:
Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.


Without CSV it works for individual vm
Remove-TagAssignment (Get-VM Servername | Get-TagAssignment -Category "Prod") -Confirm:$false
New-TagAssignment -Tag "Test" -Entity Servername -Confirm:$false


Someone an idea?
Thanks

0 Kudos
2 Replies
fabio1975
Commander
Commander

Ciao 

I tried your script and in my environment it running without problem. 

I used this CSV format 

fabio1975_0-1651042915358.png

But you need to use for oldtag not the specific TAG but the category.

fabio1975_1-1651042978084.png

 

If you want to remove a specific TAG (not Category) you need to change this row (replace -Category with -Tah)

Remove-TagAssignment (Get-VM $($_.vmname) | Get-TagAssignment -Tag $($_.oldtag)) -Confirm:$false

 

For your specific error can you post your CSV? Do you have any blank rows?

Fabio

Visit vmvirtual.blog
If you're satisfied give me a kudos

0 Kudos
erki72
Enthusiast
Enthusiast

Hi @fabio1975 
thank you in advance for your help. 
My .csv

erki72_0-1651057932766.png

The hint with the CSV got me further.
Just tested with the "," and it seems to work better

0 Kudos