VMware Cloud Community
Sauce18t
Contributor
Contributor
Jump to solution

List all VMs in a DataCenter with a particular tag in a category even if tag is missing.

Disclaimer:  I powershell challenged.

I am looking for a powercli that can list ALL VMs in a particular datacenter that do or do not have the tag category 'ApplicationOwner' assigned to them.

The closest i have seen is the following which will list out all the tag catogories for each vm.

$cat = Get-TagCategory -Name 'ApplicationOwner'

Get-VM | Where{Get-TagAssignment -Entity $_ -Category $cat} | Select Name,@{N="Tag";E={(Get-TagAssignment $_.name).tag}}       (lists VM with it)

Get-VM | Where{!(Get-TagAssignment -Entity $_ -Category $cat)} | Select Name,@{N="Tag";E={(Get-TagAssignment $_.name).tag}}    (lists VMs without it)

I just want to get all vms in a datacenter and have a list with the two columns Name and Tag.  The name would be the VM and the Tag would be something like  ApplicationOwner/HR.  The real trick is it would be nice to list all VMs wheather this particualr tag is assigned or now.  I.E.  It it is not assigned, then just have a blan in the Tag column.

I hope someone can help.

Thank you.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

$dcName = 'MyDC'

$category = 'ApplicationOwner'

$tCat = Get-TagCategory -Name $category

Get-Datacenter -Name $dcName | Get-VM |

Select Name,

    @{N='Category';E={$category}},

    @{N='Tag';E={(Get-TagAssignment -Entity $_ -Category $tCat).Tag}}


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Something like this?

Note that it only handles one tag per VM. If you have more the code needs to be slightly adapted.

Get-Datacenter MyDC | Get-VM |

select Name,@{N='Tag';E={

    $t = Get-TagAssignment -Entity $_

    $c = Get-Tag -Name (Get-TagAssignment -Entity $_).Tag

    "$($c.Category)/$($t.Tag)"}}


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

0 Kudos
Sauce18t
Contributor
Contributor
Jump to solution

Thanks for the quick response.  Unfortunetly our VMs have muliple tags in multiple Categories.  They will however only have one tag per this particular category called "ApplicationOwner".  So the goal woild be to list all VMs in "MyDC" and list next to it what the tag is for the category "ApplicationOwner".  If there is no tag associated with this catergory, then just leave it blank so we know a particular VM does not have one.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

$dcName = 'MyDC'

$category = 'ApplicationOwner'

$tCat = Get-TagCategory -Name $category

Get-Datacenter -Name $dcName | Get-VM |

Select Name,

    @{N='Category';E={$category}},

    @{N='Tag';E={(Get-TagAssignment -Entity $_ -Category $tCat).Tag}}


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

0 Kudos
Sauce18t
Contributor
Contributor
Jump to solution

This is perfect!  I will export to CSV but will take it from here.  Thank you.  I'll mark you answer as correct.

0 Kudos