VMware Cloud Community
pamiller21
Enthusiast
Enthusiast

Assign VM Tag IF

How do I go about doing things like:
1) Assign this tag if....connected to these portgroups
2) Assign this tag if....within this folder

0 Kudos
11 Replies
LucD
Leadership
Leadership

These both follow somewhat the same layout.

1) Something like this.
Note that this is very basic. There is, for example, no test if that Tag was already assigned.

$vmName = 'MyVM'

$tgtPG = 'PG'

$tagCategory = 'cat'

$tagName = 'whatever'


$tag = Get-Tag -Name $tagName -Category $tagCategory


if(Get-VM -Name $vmName -PipelineVariable vm | Get-NetworkAdapter | where{$_.NetworkName -eq $tgtPG}){

    New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false

}

2) Same logic here

$vmName = 'MyVM'

$folderName = 'MyFolder'

$tagCategory = 'cat'

$tagName = 'whatever'


$tag = Get-Tag -Name $tagName -Category $tagCategory


if(Get-VM -Name $vmName -PipelineVariable vm | where{$_.Folder.Name -eq $folderName}){

    New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false

}


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

I am looking at your port group tagging and I would like to have it scan the entire VM collection and anything attached to these portgroups get this tag, so I altered it, but I am missing something on how it gets all the VMs, but needs to individually tag.  Here is what I have:

$tgtPG = 'Corp-61'

$tagCategory = 'IP Use'

$tagName = 'Internal IP'

$tag = Get-Tag -Name $tagName -Category $tagCategory

if(Get-VM | Get-NetworkAdapter | where{$_.NetworkName -eq $tgtPG}){

    New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false

}

I really appreciate this!

0 Kudos
LucD
Leadership
Leadership

Try with a nested Where-clause

$tgtPG = 'Corp-61'

$tagCategory = 'IP Use'

$tagName = 'Internal IP'


$tag = Get-Tag -Name $tagName -Category $tagCategory


Get-VM -PipelineVariable vm | where{Get-NetworkAdapter -VM $_  | where{$_.NetworkName -eq $tgtPG}} |

ForEach-Object -Process {

    New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false

}


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

I have been testing this and it mostly works, but the replicas that I have generated with veeam are attached to the portgroup, but the NICs are disconnected so the script doesn't see them.  Is there a way to allow that?

0 Kudos
LucD
Leadership
Leadership

Is there a way to recognise the replica and find the original VM for which they are a replica?


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

Sure, they are <IDENTICAL_VM NAME>_replica

0 Kudos
LucD
Leadership
Leadership

Try like this

$tgtPG = 'Corp-61'

$tagCategory = 'IP Use'

$tagName = 'Internal IP'

$tag = Get-Tag -Name $tagName -Category $tagCategory

Get-VM -PipelineVariable vm | where{Get-NetworkAdapter -VM $_  | where{$_.NetworkName -eq $tgtPG}} |

ForEach-Object -Process {

    New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false

    Get-VM -Name "$($vm.Name)_replica" -ErrorAction SilentlyContinue |

    New-TagAssignment -Tag $tag -Confirm:$false

}


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

I am trying to apply tags via folders, but they are not searching recursively and not sure how to make that work.

### Mgmt ###

$folderName = 'Mgmt'

$tagCategory = 'Admin'

$tagName = 'Team Tag'

$tag = Get-Tag -Name $tagName -Category $tagCategory

Get-VM -PipelineVariable vm | where{$_.Folder.Name -eq $folderName} |

ForEach-Object -Process {

    New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false

    Get-VM -Name "$($vm.Name)_replica" -ErrorAction SilentlyContinue |

    New-TagAssignment -Tag $tag -Confirm:$false

0 Kudos
LucD
Leadership
Leadership

Do you mean you have other folders below the folder 'Mgmt'?

And VMs in those folders should also be tagged?


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

Correct!

0 Kudos
LucD
Leadership
Leadership

It's not pretty, but try like this

### Mgmt ###

$folderName = 'Mgmt'

$tagCategory = 'Admin'

$tagName = 'Team Tag'

$tag = Get-Tag -Name $tagName -Category $tagCategory

$folder = Get-Folder -Name $folderName

$folders = @($folder)

$folders += Get-Folder -Location $folder


Get-VM -Location $folders -PipelineVariable vm

ForEach-Object -Process {

    New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false

    Get-VM -Name "$($vm.Name)_replica" -ErrorAction SilentlyContinue |

    New-TagAssignment -Tag $tag -Confirm:$false

}


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

0 Kudos