VMware Cloud Community
NucleusVM
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?

Reply
0 Kudos
3 Replies
LucD
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

Reply
0 Kudos
NucleusVM
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?

Reply
0 Kudos
LucD
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

Reply
0 Kudos