VMware Cloud Community
tdubb123
Expert
Expert

get-view filter by vcenter and datacenter

how do I combine get-view by both datacenter and virtualmachine?

get-view -viewtype datacenter -filter {Name="DC1"}

Get-View -ViewType VirtualMachine -filter {Name="test"}

0 Kudos
1 Reply
xitanul
Contributor
Contributor

Try this:

$Datacenter = Get-View -ViewType Datacenter -Filter @{Name="DC1"} -Property Name | Select-Object -ExpandProperty MoRef

Get-View -ViewType VirtualMachine -Filter @{Name="test"} -Property Name -SearchRoot $Datacenter

Or one-liner:

Get-View -ViewType VirtualMachine -Filter @{Name="test"} -Property Name -SearchRoot $(Get-View -ViewType Datacenter -Filter @{Name="DC1"} -Property Name).MoRef

...Just make sure to add properties or remove the properties parameter altogether from the VirtualMachine Get-View to return all or specific property values.

Specifying properties will greatly improve the query speed.

$Datacenter = (Get-View -ViewType Datacenter -Filter @{Name="DC1"} -Property Name).MoRef

Get-View -ViewType HostSystem -Filter @{Name="test"} -Property Name -SearchRoot $Datacenter
0 Kudos