pamiller21
Enthusiast
Enthusiast

Copy VM Tags From VMs

Is there a way to take the tags from <VM NAME> and copy them to <VM NAME>_replica?

0 Kudos
7 Replies
LucD
Leadership
Leadership

Yes


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

lol any advice on how to conquer this? I am at a loss on how to do this today.

0 Kudos
LucD
Leadership
Leadership

Use the TagAssignment cmdlets.
Something like this for example.

$vmName = 'MyVM'
$copyVm = "$($vmName)_replica"

Get-VM -Name $vmName | Get-TagAssignment |
ForEach-Object -Process {
  New-TagAssignment -Entity $copyVm -Tag $_.Tag
}


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

That is simple, can I point this at all my VMs and somehow array through all them?

0 Kudos
LucD
Leadership
Leadership

Sure, just leave out the Name parameter on the Get-VM


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

I am running this:

 

$vmName = Get-VM

$copyVm = "$($vmName)_replica"

Get-VM -Name $vmName | Get-TagAssignment | ForEach-Object -Process { New-TagAssignment -Entity $copyVm -Tag $_.Tag }

 

And when I get to the $copyVm = "$($vmName)_replica" part it combines all the VMs together in one line.  The next part then looks for a VM named everything.

0 Kudos
LucD
Leadership
Leadership

I meant like this

Get-VM | Get-TagAssignment |
ForEach-Object -Process {
  New-TagAssignment -Entity "$($_.Entity.Name)_replica" -Tag $_.Tag
}


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

0 Kudos