VMware Cloud Community
Dan_Kahl
Contributor
Contributor

Trying to filter with multiple tags using "Get-VM -Tag" command

Hi everyone - 

I'm trying to run "get-vm -tag" and filter by using multiple tags.

So for example, let's say I'm connected to 2 vCenters with powercli.  We have 2 categories with different tags.  Let's say 1 category is "application" (tags A1, A2...) and the other is "team ownership" (tags TO1, TO2, ...)

I'm trying to run:

get-vm -tag A1 | get-vm -tag TO1

to identify the application #1 and then filter on the team owner #1.  So instead of getting 30 servers tagged with app #1, I get the 5 servers within the app #1 that team #1 owns.

Obviously I'm doing something wrong! 🙂

Any suggestions on how to do this?

Thanks-

Dan

-Dan
0 Kudos
3 Replies
LucD
Leadership
Leadership

The best I could come up with is something like this

Get-VM -Tag A1 | where{(Get-VM -Tag TO1).Name -Contains $_.Name}


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

0 Kudos
Dan_Kahl
Contributor
Contributor

Thanks Luc!!  That worked for me.  I'm able to filter now.

Best regards-

Dan

-Dan
0 Kudos
sZettermark
Contributor
Contributor

If you have to do this with many tags, this is one way:

$taglist = "TAG-1", "TAG-2", "TAG-3"
Get-VM | ForEach-Object {
    $tags = (Get-TagAssignment -Entity $_).Tag.Name
    if(($taglist | ForEach-Object {$tags -contains $_}) -notcontains $false) {$_}
}

 

0 Kudos