VMware Cloud Community
MR_HPE
Contributor
Contributor

Match wildcard in -filter

Hello all,

I realize this is likely a very simple question, but I can't come up with an answer. I need to pull a list of VM's using Get-View with the '-filter' option, but having trouble with the correct Regex expression.

I.e, get-view –viewtype VirtualMachine -filter @{'Name' = '^((*$Cluster*).)*$'} 

Where $Cluster would be the cluster name, providing only the VM's for that specific cluster. The expression above returns no VMs.

 

Thanks.

 

0 Kudos
2 Replies
LucD
Leadership
Leadership

The Filter is based on a property in the object(s) you want to retrieve.
Since the VirtualMachine object does not have a property indicating the Cluster, that is hard/impossible to do.

A better option is to use the SearchRoot parameter.

$cl = Get-Cluster -Name $cluster

Get-View -ViewType VirtualMachine -SearchRoot $cl.Id


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

MR_HPE
Contributor
Contributor

Thank you Luc, that worked perfectly. Nice to see you're still involved in helping people on the PowerCLI side.

0 Kudos