Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.