VMware Cloud Community
esxi1979
Expert
Expert
Jump to solution

Get-View -ViewType virtualmachine, is it possible to get the cluster info of the VM ?

Is it possible at 1st place to use get-view in place of

Get-VM | Select-Object -Property Name,VMHost,@{N="Cluster";E={$_.VMHost.Parent}},Folder

?

I wanted to know with

Get-View -ViewType virtualmachine, is it possible to get the cluster info of the VM ?

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

foreach($vm in (Get-View -ViewType VirtualMachine -Property 'Name','Parent','Runtime.Host')){

    $esx = Get-View -Id $vm.Runtime.Host -Property 'Name','Parent'

    $obj = Get-View -Id $esx.Parent -Property 'Name','Parent'

    $folder = Get-View -Id $vm.Parent -Property 'Name'

    while ($obj.Parent -isnot [VMware.Vim.Computeresource] -and !$obj.Parent){

        $obj = Get-View -Id $obj.Parent -property 'Parent'

    }

    $vm | Select Name,@{N='VMHost';E={$esx.Name}},@{N='Cluster';E={$obj.Name}},@{N='Folder';E={$folder.Name}}

}


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

View solution in original post

3 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

foreach($vm in (Get-View -ViewType VirtualMachine -Property 'Name','Parent','Runtime.Host')){

    $esx = Get-View -Id $vm.Runtime.Host -Property 'Name','Parent'

    $obj = Get-View -Id $esx.Parent -Property 'Name','Parent'

    $folder = Get-View -Id $vm.Parent -Property 'Name'

    while ($obj.Parent -isnot [VMware.Vim.Computeresource] -and !$obj.Parent){

        $obj = Get-View -Id $obj.Parent -property 'Parent'

    }

    $vm | Select Name,@{N='VMHost';E={$esx.Name}},@{N='Cluster';E={$obj.Name}},@{N='Folder';E={$folder.Name}}

}


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

esxi1979
Expert
Expert
Jump to solution

Wow ..thanks.. i will give it a try..

That looks complex code .. in particular what is purpose of

while ($obj.Parent -isnot [VMware.Vim.Computeresource] -and !$obj.Parent)...

0 Kudos
LucD
Leadership
Leadership
Jump to solution

In fact something similar happens behind the covers when you use a PowerCLI cmdlet.

From the VM there is a chain of parent objects.

The While loop follows this chain, until it has reached a cluster object, or there is no more parent.

The latter one is to avoid errors when there is a VM on a standalone ESXi node (no cluster)


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

0 Kudos