Reply to Message

View discussion in a popup

Replying to:
MattHumphreys
Contributor
Contributor

Get VM List based on Multiple Tags

I am trying to get a script to extract a list of virtual machines that have a particular tag combination from available tags and its proving to be quite difficult.

I have been creating a selection of tags based on using Get-Tags and out-gridview to allow the person executing it to decide on what tags they want to filter against.

My problem comes when I am trying to translate that selection into a filter to get a list of VM's with those tags, it could be a varying number of tags in the selection which I wont always know.

I also have the problem of trying to make a list of the vms that match all the tags together.

I have tried putting them into an array and then calling the tags from the position in the array:

Get-VM | Where-Object{(Get-TagAssignment -Entity $_ ).Tag.Name -Match $tag_checks[0].tag_name -and $tag_checks[1].tag_name}

that works but because the array can be any number of tags long that is my issue I dont know how to "create" enough entries for all the items in the array.

I have also looked at defining individual variables for each tag:

$t1 = Get-Tag -Name Name1
$t2 = Get-Tag -Name Name2

$tag1 = Get-VM -Tag $t1
$tag2 = Get-VM -Tag $t2

$results = $tag1 | ?{$tag2 -contains $_}

While both get me to the end result I am struggling to make it efficient and repeatable for a large list of VM's

I suspect I have hit a limitation in my programming knowledge, any advice welcomed

Reply
0 Kudos