2

I am using Tags to filter VM / identify special configuration.

Get-VM -Tag TEST

Now I want to get all VMs NOT matching a tag .... Is there a way to do that without an extra loop to remove all items matching the Tag ?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
anael
  • 23
  • 1
  • 7

2 Answers2

2

As it seems, no. But using another loop is not a bad solution.

Should be something like:

Get-VM -Name * | ?{$_.Tag -ne "Test"}

Or if VirtualMachine doesn't have a Tag property, you should try with Get-View:

Get-View -Name * | ?{$_ .Tag -ne "Test"} | Get-VM
iTayb
  • 12,373
  • 24
  • 81
  • 135
0

I was looking at this, and I used the -contains/-notcontains comparison operator instead of -eq/-ne, as it always returns the a boolean expression. If you have more than one tag, it might not work properly.

$VMs = get-vm 

foreach ($VM in $VMs){
    If (((Get-Tagassignment $VM).Tag.Name -notcontains "Prod"){
         Whatever you want if it doesn't contain prod.
    }
}
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Ryan
  • 1