Pregunta

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.

¿Fue útil?

Solución

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top