VMware Cloud Community
matthewcdove
Contributor
Contributor

Retrieving HostID and HostCluster

I have the following script that is functioning (mostly due to the help of this group).  I want to add to items to the output of each, the VC it comes from and the VC  Cluster.  I have the VC names in the text file, but I would like that added to the output file so I can sort later by VC.

In reviewing all the tables for the library information, I can't see how to get the HostID (vc) and the cluster that it comes from thru the ExtensionData.

Does anyone know what string to add to gather the hostid and cluster?

SCRIPT:

$vCenters = Get-content 'C:\VMInventory\vCenters.txt'

Foreach ($vCenter in $vCenters)

{

Connect-VIServer -Server $vCenter –User username –Password password

}

$Data = @()

Foreach ($VIServer in $Global:DefaultVIServers)

{

$NewInfo = Get-VM | select Name, Notes, PowerState, MemoryMB, @{n="HostMemoryUsage";e={$_.ExtensionData.Summary.QuickStats.HostMemoryUsage}}

$Data += $NewInfo

}

$Data | Export-csv –path "C:\VMInventory\Mem_Host_Sum1.csv"

0 Kudos
1 Reply
LucD
Leadership
Leadership

The expensive way to get the cluster is by doing

Get-Cluster -VM $vm

or as a calculated property in yoru Select

@{N="Cluster";E={Get-Cluster -VM $_ | Select -ExpandProperty Name}}

The less expensive way is to follow the Parent property till you reach a ClusterComputeResource.

The vCenter lives in the Uid property of the |VirtualMachine object.

@{N="vCenter";E={$_.Uid.Split(“:”)[0].Split(“@”)[1]}}


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

0 Kudos