<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Get VM List based on Multiple Tags in VMware PowerCLI Discussions</title>
    <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Get-VM-List-based-on-Multiple-Tags/m-p/2893901#M105345</link>
    <description>&lt;P&gt;Thanks for that LucD, that gives me a list of VM's that matches any of the tags in $tagnames, I am looking to get to a list that contains only the VM's that match all of the tags, I have reluctantly come to the conclusion just for expediency that I will have to manually enter the tag information:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Get-VM | Select Name,@{Name="Tags";Expression={(Get-TagAssignment -Entity $_).Tag.Name}} |&lt;BR /&gt;Where {$_.Tags -match "Tag1" -and $_.Tags -match "Tag2" -and $_.Tags -match "Tag3" -and $_.Tags -notmatch "Tag4"} |&lt;BR /&gt;Format-Table -Autosize&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It gives me the response I wanted and allows me to do things like exclude based on a particular tag or with a tag combination.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The reason for the whole thing is we are using veeam and tag combinations to group machines into backups but veeam doesnt easily display a list of VM's that are captured by the tag combination so I am doing this to get me a list to ensure that everything is correct before the backups run.&lt;/P&gt;</description>
    <pubDate>Wed, 16 Feb 2022 14:49:24 GMT</pubDate>
    <dc:creator>MattHumphreys</dc:creator>
    <dc:date>2022-02-16T14:49:24Z</dc:date>
    <item>
      <title>Get VM List based on Multiple Tags</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Get-VM-List-based-on-Multiple-Tags/m-p/2893873#M105342</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;I also have the problem of trying to make a list of the vms that match all the tags together.&lt;/P&gt;&lt;P&gt;I have tried putting them into an array and then calling the tags from the position in the array:&lt;/P&gt;&lt;P&gt;Get-VM | Where-Object{(Get-TagAssignment -Entity $_ ).Tag.Name -Match $tag_checks[0].tag_name -and $tag_checks[1].tag_name}&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;I have also looked at defining individual variables for each tag:&lt;/P&gt;&lt;P&gt;$t1 = Get-Tag -Name Name1&lt;BR /&gt;$t2 = Get-Tag -Name Name2&lt;/P&gt;&lt;P&gt;$tag1 = Get-VM -Tag $t1&lt;BR /&gt;$tag2 = Get-VM -Tag $t2&lt;/P&gt;&lt;P&gt;$results = $tag1 | ?{$tag2 -contains $_}&lt;/P&gt;&lt;P&gt;While both get me to the end result I am struggling to make it efficient and repeatable for a large list of VM's&lt;/P&gt;&lt;P&gt;I suspect I have hit a limitation in my programming knowledge, any advice welcomed&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 11:38:12 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Get-VM-List-based-on-Multiple-Tags/m-p/2893873#M105342</guid>
      <dc:creator>MattHumphreys</dc:creator>
      <dc:date>2022-02-16T11:38:12Z</dc:date>
    </item>
    <item>
      <title>Re: Get VM List based on Multiple Tags</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Get-VM-List-based-on-Multiple-Tags/m-p/2893897#M105344</link>
      <description>&lt;P&gt;Try something like this.&lt;BR /&gt;The variable $tagNames can contain 1 or more Tag names.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;$tagNames = 'Tag1','Tag2','Tag3'

$vms = Get-VM -Tag $tagNames

$tagNames | ForEach-Object -Process {
  $vms = Get-VM -Tag $_ | where{$vms.Name -contains $_.Name}
}
$vms&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;How you populate the variable $tagNames is up to you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 14:30:51 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Get-VM-List-based-on-Multiple-Tags/m-p/2893897#M105344</guid>
      <dc:creator>LucD</dc:creator>
      <dc:date>2022-02-16T14:30:51Z</dc:date>
    </item>
    <item>
      <title>Re: Get VM List based on Multiple Tags</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Get-VM-List-based-on-Multiple-Tags/m-p/2893901#M105345</link>
      <description>&lt;P&gt;Thanks for that LucD, that gives me a list of VM's that matches any of the tags in $tagnames, I am looking to get to a list that contains only the VM's that match all of the tags, I have reluctantly come to the conclusion just for expediency that I will have to manually enter the tag information:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Get-VM | Select Name,@{Name="Tags";Expression={(Get-TagAssignment -Entity $_).Tag.Name}} |&lt;BR /&gt;Where {$_.Tags -match "Tag1" -and $_.Tags -match "Tag2" -and $_.Tags -match "Tag3" -and $_.Tags -notmatch "Tag4"} |&lt;BR /&gt;Format-Table -Autosize&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It gives me the response I wanted and allows me to do things like exclude based on a particular tag or with a tag combination.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The reason for the whole thing is we are using veeam and tag combinations to group machines into backups but veeam doesnt easily display a list of VM's that are captured by the tag combination so I am doing this to get me a list to ensure that everything is correct before the backups run.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 14:49:24 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Get-VM-List-based-on-Multiple-Tags/m-p/2893901#M105345</guid>
      <dc:creator>MattHumphreys</dc:creator>
      <dc:date>2022-02-16T14:49:24Z</dc:date>
    </item>
    <item>
      <title>Re: Get VM List based on Multiple Tags</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Get-VM-List-based-on-Multiple-Tags/m-p/2893903#M105346</link>
      <description>&lt;P&gt;Did you try my latest reply?&lt;BR /&gt;That gives me only the VMs that have all the specified tags&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 14:53:30 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Get-VM-List-based-on-Multiple-Tags/m-p/2893903#M105346</guid>
      <dc:creator>LucD</dc:creator>
      <dc:date>2022-02-16T14:53:30Z</dc:date>
    </item>
    <item>
      <title>Re: Get VM List based on Multiple Tags</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Get-VM-List-based-on-Multiple-Tags/m-p/2893908#M105347</link>
      <description>&lt;P&gt;Yes I have tried it just now and it works as you mentioned, gives me a list of the VM's that match all the specified tags and I have populated it from the list of available tags on the cluster.&lt;/P&gt;&lt;P&gt;I also added a limit on the Get-VM's to put it into a limited location so I dont search an entire vcenter just a specific datacenter&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;$location = Get-Datacenter | Out-GridView -Title "Select single location to run checks on" -OutputMode Single

$vms = Get-VM -Location $location -Tag $tagNames

$tagNames | ForEach-Object -Process {
  $vms = Get-VM -Location $location -Tag $_ | where{$vms.Name -contains $_.Name}
}
$vms.Name | Sort-Object |Format-Table -Autosize&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 16 Feb 2022 15:07:39 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Get-VM-List-based-on-Multiple-Tags/m-p/2893908#M105347</guid>
      <dc:creator>MattHumphreys</dc:creator>
      <dc:date>2022-02-16T15:07:39Z</dc:date>
    </item>
  </channel>
</rss>

