The following will get CPU (num of cores) counts from hosts and will also get vCPU counts from VM’s. It will also get the operating system of each VM, its power state and the average CPU for the past week. The end of each section also has a count of the number of objects (hosts and VMs) in the environment.
Connect-VIServer <Your_vCenter_Server>
# define start and finish days (1 week period)
$startdate=(get-date).addDays(-7)
$finishdate=(get-date).addDays(-1)
# initialise counters
$vmcount=0
$hostcount=0
ForEach ($esxhost in (Get-VMHost | Sort Name)){
Write-Host $esxhost.Name","$esxhost.NumCPU
$hostcount++
}
Write-Host "Total Hosts:" $hostcount
ForEach ($vm in (Get-VM | Sort Name)){
If ($vm.PowerState -eq 'PoweredOn'){
$vstats = (Get-Stat -entity $vm -stat cpu.usage.average -Start $startdate -Finish $finishdate | measure-object -property value -average)
# round to two decimal places
$vavg = [system.math]::round($vstats.average,2)
}Else{
$vavg = 0
}
Write-Host $vm.Name","$vm.Guest.OSFullName","$vm.PowerState","$vm.NumCPU","$vavg
$vmcount++
}
Write-Host "Total VM's:" $vmcount
Popularity: 35% [?]
