Enthusiast
Enthusiast

find a host's parent (cluster)

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?

0 Kudos
3 Replies
Leadership
Leadership

Try like this.

It will handle stand-alone ESXi nodes correctly, i.e. blank Cluster name

Get-View -ViewType HostSystem -Property Name,Parent |

Select Name,@{N='Cluster';E={(Get-View -Id $_.Parent | where{$_ -is [VMware.Vim.ClusterComputeResource]}).Name}}


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

0 Kudos
Enthusiast
Enthusiast

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?

0 Kudos
Leadership
Leadership

For multiple vCenter connections, try to run it this way.

foreach($vc in $global:DefaultVIServers){

   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

0 Kudos