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 ?
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
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
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)...
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