Skip to content

Instantly share code, notes, and snippets.

@vScripter
Last active July 26, 2018 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vScripter/05372262f8908290dd0ea93f066e9857 to your computer and use it in GitHub Desktop.
Save vScripter/05372262f8908290dd0ea93f066e9857 to your computer and use it in GitHub Desktop.
PowerCLI example using Get-View filtering to create a VMHost inventory mapping
# Gather a list of Datacenters, but only pull the name
$datacenterQuery = Get-View -Server $viServer -ViewType Datacenter -Property Name -ErrorAction 'Stop'
foreach ($datacenter in $datacenterQuery) {
# Grab a list of clusters, but only pull the name - also use the current datacenter as the search root
$clusterQuery = Get-View -Server $viServer -ViewType ClusterComputeResource -Property Name -SearchRoot $datacenter.MoRef
foreach ($cluster in $clusterQuery) {
# Get the VMhosts that are part of the current cluster, only pull the Name & Summary properties - also use the current cluster as the search root
$clusterVMHosts = Get-View -Server $viServer -ViewType HostSystem -Property Name, Summary -SearchRoot $cluster.MoRef
# iterate through each host and create the custom properties that you want to pull
foreach ($vmHostSystem in $clusterVmHosts) {
[int]$hostUptime = $null
[int]$hostUptimeInDays = $null
[int]$hostCpuCoresPerSocket = $null
$memorySize = $null
$memorySizeGB = $null
$hostUptime = $vmHostSystem.summary.quickstats.uptime / 86400
$hostUptimeInDays = [System.Math]::Round($hostUptime)
$hostCpuCoresPerSocket = ($vmHostSystem.summary.hardware.NumCpuCores) / ($vmHostSystem.summary.hardware.NumCpuPkgs)
$memorySize = (($vmHostSystem.Summary.Hardware.MemorySize / 1024) / 1024) / 1024
$memorySizeGB = [System.Math]::Round($memorySize)
[PSCustomObject] @{
Name = $vmHostSystem.name
vCenterServer = $viServer
Datacenter = $datacenterName
Cluster = $cluster.Name
Version = $vmHostSystem.summary.config.product.version
Build = $vmHostSystem.summary.config.product.build
APIVersion = $vmHostSystem.summary.config.product.apiversion
HAStatus = $vmHostSystem.summary.runtime.dashoststate.state
ConnectionState = $vmHostSystem.summary.runtime.ConnectionState
PowerState = $vmHostSystem.summary.runtime.powerstate
InMaintenanceMode = $vmHostSystem.summary.runtime.inmaintenancemode
UptimeInDays = $hostUptimeInDays
Manufacturer = $vmHostSystem.summary.hardware.vendor
Model = $vmHostSystem.summary.hardware.model
CPUModel = $vmHostSystem.summary.hardware.cpumodel
CPUSockets = $vmHostSystem.summary.hardware.NumCpuPkgs
CPUCores = $vmHostSystem.summary.hardware.NumCpuCores
CPUHyperCores = $vmHostSystem.summary.hardware.NumCpuThreads
CPUCoresPerSocket = $hostCpuCoresPerSocket
MemorySizeGB = $memorySizeGB
NumNICs = $vmHostSystem.summary.hardware.NumNics
NumHBAs = $vmHostSystem.summary.hardware.NumHBAs
RebootRequired = $vmHostSystem.summary.rebootrequired
CurrentEVCMode = $vmHostSystem.Summary.CurrentEVCModeKey
MaxEVCMode = $vmHostSystem.Summary.MaxEVCModeKey
vCenterVersion = $viServerVersion
} # end
} # end foreach $vmHostSystem
} # end foreach $cluster
} # end foreach $datacenter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment