VMware Cloud Community
jzim
Contributor
Contributor

how does the searchroot option work with get-view

I want to get all of the objects for the VMs that are in a cluster, is there an easy way to do it with the get-view -searchroot option? I tried the following but it does seam to work for me:

$clustObj = get-view -filter @{"name"="cluster"} -viewtype "clusterComputeResource"

$vmObjs = get-view -viewtype "VirtualMachine" -searchroot $clustObj.MoRef

When I run this $vmObjs comes back empty. I could get the names of all the VMs and put that into a loop but that seams like that would take a long time.

Thanks,

Zim

0 Kudos
5 Replies
admin
Immortal
Immortal

Why not use something like:

Get-Cluster cluster | Get-VM | foreach { $_ | Get-View }

It has one advantage of being much easier to read.

0 Kudos
LucD
Leadership
Leadership

I fully agree with Carter's suggestion, it's easier to read and returns the same info.

But out of curiosity does this return anything ?

Get-View -viewtype "VirtualMachine" -searchroot $null


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

0 Kudos
StueyC
Contributor
Contributor

Hi

What I saw looking at this was the following......I connected to a cluster which has a number of guests at the cluster level of the heirarchy and some other guests one level deeper in the heirarchy (these for information where in a resource pool)

$clusterObj = get-view -filter @{"name"="cluster"} -viewtype "clusterComputeResource"

$vmObjs = get-view -viewtype "VirtualMachine" -searchroot $clusterObj.moRef

the list of vm's returned where only those deeper in the heirarchy - ie those in the resource pool - the vm's located at the cluster level were ignored

as the cluster in the VC heirarchy is in a folder I tried connecting to the folder - so the heirarchy looks like

folder-cluster-resource group

I then connected as follows

$folderObj = get-view -filter @{"name"="folder"} -viewtype "folder"

$vmObjs = get-view -viewtype "VirtualMachine" -searchroot $folderObj.moRef

the results are identical to the first scenario.....the get-view is a quicker lookup method, that coupled with the filter options to for example extract only windows guests into the object array is cool and takes some coding effor out - which is why I'd prefer to find a solution on this.

any ideas on how to ensure the -sourceroot is respected and includes vm objects in the sourceroot itself as well as the downstream heirarchy?

Stu.

0 Kudos
LucD
Leadership
Leadership

The ClusterComputeResource is not handled correctly by the Get-View cmdlet when passed as a -SearchRoot parameter.

A bypass I used, give the MoRef to the hidden resource pool called "Resources".

$rp = Get-View (Get-Cluster <Clustername> | Get-View).ResourcePool
$vms = Get-View -ViewType VirtualMachine -SearchRoot $rp.MoRef

That returns all the VMs under the cluster.


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

0 Kudos
StueyC
Contributor
Contributor

Perfect! - works great - thanks for the tip

Stu.

0 Kudos