I am trying to run a filter when querying VICenter to limit only VirtualMachines for a certain Host.
For example with $vicenter set correctly (with valid credentials):
C:\Temp> $pfsense=Get-View -Server $viserver -ViewType VirtualMachine -Filter @{"Name"="pfsense"} -Property Name, Runtime, Config
C:\Temp> Write-Host $pfsense.Runtime.Host
HostSystem-host-157
So I want to remove the Name filter and filter on Runtime Host:
C:\Temp> $host157=Get-View -Server $viserver -ViewType HostSystem -Property Name, Parent, Summary, Config
C:\Temp> Write-Host $host157.Config.Host
HostSystem-host-157
What Get-View Filter Query do I need to run?
I have tried:
Get-View -Server $viserver -ViewType VirtualMachine -Filter @{"Runtime.Host"="HostSystem-host-157"} -Property Name, Runtime, Config, Guest
However nothing is returned.
Similarly I have tried:
Get-View -Server $viserver -ViewType VirtualMachine -Filter @{"Runtime.Host"="$host157.Config.Host"} -Property Name, Runtime, Config, Guest
Using the Filter with a MoRef is a bit of a special case.
You will need to specify the Value property of the MoRef.
That works because the Filter parameter uses the right side operand as a RegEx.
Simplified
$esx = Get-VMHost -Name MyEsx
Get-View -ViewType VirtualMachine -Filter @{'Runtime.Host' = $esx.ExtensionData.MoRef.Value}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Using the Filter with a MoRef is a bit of a special case.
You will need to specify the Value property of the MoRef.
That works because the Filter parameter uses the right side operand as a RegEx.
Simplified
$esx = Get-VMHost -Name MyEsx
Get-View -ViewType VirtualMachine -Filter @{'Runtime.Host' = $esx.ExtensionData.MoRef.Value}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference