# For the Get-View calls later in the script, I use 'splatting'# All cmdlet parameters are placed in a hash table
#
# The exception is the SearchRoot parameter, the variable used on that parameter
# is not yet initialised at this point
# The Get-View cmdlet allows one to specify which properties to fecth for an object
# instead of retrieving the complete object.
# This shortens the execution time of the cmdlet
$sCluster = @{
ViewType = 'ClusterComputeResource'
Property = 'Name'
PipelineVariable = 'cluster'
}
$sVMHost = @{
ViewType = 'HostSystem'
Property = 'Name','Runtime.PowerState','Summary.Hardware'
PipelineVariable = 'esx'
}
$sVM = @{
ViewType = 'VirtualMachine'
Property = 'Name','Summary.Runtime.PowerState','Config.Hardware'
PipelineVariable = 'vm'
Filter = @{'Summary.Config.Template'='False'}
}
# The script uses 3 nested foreach loops
# Cluster-VMHost-VM
Get-View @sCluster |
ForEach-Object -Process {
# The SearchRoot parameter will only return objects that are under the entity specified on this parameter
Get-View @sVMHost -SearchRoot $cluster.MoRef |
ForEach-Object -Process {
Get-View @sVM -SearchRoot $esx.MoRef |
ForEach-Object -Process {
# The script uses Get-View and hence has to deal with vSphere objects
# These objects, and their properties, are described in the API Reference
# https://code.vmware.com/apis/968/vsphere
$vm | Select @{N='Cluster';E={$cluster.Name}},
@{N='VMHost';E={$esx.Name}},
@{N='HostState';E={$esx.RunTime.PowerState}},
@{N='HostCPU';E={$esx.Summary.Hardware.NumCpuCores}},
@{N='CPUType';E={$esx.Summary.Hardware.CpuModel}},
@{N='MemoryMB';E={[math]::Round($esx.Summary.Hardware.MemorySize/1KB)}},
@{N='HyperThread';E={-not ($esx.Summary.Hardware.NumCpuCores -eq $esx.Summary.Hardware.NumCpuThreads)}},
@{N='VM';E={$vm.Name}},
@{N='VMState';E={$vm.Summary.Runtime.PowerState}},
@{N='VMSockets';E={$vm.Config.Hardware.NumCpu/$vm.Config.Hardware.NumCoresPerSocket}},
@{N='VMCoresPerSocket';E={$vm.Config.Hardware.NumCoresPerSocket}},
@{N='VMLogicalCPUs';E={$vm.Config.Hardware.NumCpu}},
@{N='VMTotalMemoryGB';E={$vm.Config.Hardware.MemoryMB/1KB}}
}
}
}