Question

I'm trying to get a list of all VMs from vcenter, but exclude VM from the list, if its name contains one of the strings I have in another list.

$vm_list = Get-Datacenter | Get-VM 

$vm_list content:

vm1233
vm4566
vm7890
vm3330
vm9990

Now I want to exclude any VM that has one of the strings from the list in its name:

$exluded_vms = @('33', '66')

What's the best way to do that?

Thanks.

Was it helpful?

Solution

I use this:

[regex]$exluded_vms = '33|66'
 $vm_list = Get-Datacenter | Get-VM  | ? { $_.NAME -NOTMATCH $exluded_vms}

a more efficent way is using get-view

   get-view -viewtype virtualmachine -filter @{'name'='^((?!33|66).)*$'} 

last one give more information about VM, you can pipe the result to | select name to limit the ouptup

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top