I am using this code (courtesy of LucD) to find a cluster's parent.
$cl = Get-View -ViewType ClusterComputeResource -Property Name,Parent
$dc = Get-View -Id (Get-view -Id $cl.Parent -Property Name,Parent).Parent -Property Name
$dc.Name
I want to modify this in a way that it will give me a host's parent. I tried it like this, but no luck, because $cluster.name is always "host"
$hosts = Get-View -ViewType HostSystem -Property Name,Parent
$cluster = Get-View -Id (Get-view -Id $hosts.Parent -Property Name,Parent).Parent -Property Name
$cluster.Name
How can I achieve this?
Try like this.
It will handle stand-alone ESXi nodes correctly, i.e. blank Cluster name
Select Name,@{N='Cluster';E={(Get-View -Id $_.Parent | where{$_ -is [VMware.Vim.ClusterComputeResource]}).Name}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
When running this script, I connect on 2 vcenters. Some of the hosts, report their cluster to be on vcenterA, when the host itself is located in vCenterB. It could be that some values match on both vCenters?
For multiple vCenter connections, try to run it this way.
Get-View -ViewType HostSystem -Property Name,Parent -Server $vc |
Select @{N='vCenter';E={$vc.Name}},Name,@{N='Cluster';E={(Get-View -Id $_.Parent -Server $vc | where{$_ -is [VMware.Vim.ClusterComputeResource]}).Name}}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference