VMware Cloud Community
kaizenwerks
Contributor
Contributor

ESX/ESXi Host CPU and Memory usage and export to CSV

Hi all,

I'm looking to get the total amount of CPU and Memory being utilized by all the ESX/ESXi hosts in our environment. The script below exports to csv the host name, CPU cores, total CPU and total Memory but I just need to know how to get the CPU and Memory utilization. Any help would be appreciated!

$vcserver="xxxxx"

*

connect-VIServer $vcserver

*

#Setting Variables

*

Write-Host "Setting Variables...Please wait"

*

$VMs = Get-VM

$VMHs = Get-VMHost

  1. Send VM Host Information to Document #

$report = @()

ForEach ($vmh in $vmhs)

{

$hosts = Get-VMHost $vmh.Name | %{Get-View $_.ID}

$row = "" | select-Object Name, NumCpuCores, Hz, Memory

$row.Name = $hosts.Name

$row.NumCpuCores = $hosts.Hardware.CpuInfo.NumCpuCores

$row.Hz = ::round(($hosts.Hardware.CpuInfo.Hz)/10000, 0)

$row.Memory = ::round(($hosts.Hardware.MemorySize)/1024, 0)

$report += $row

}

$report | Export-Csv "c:\VMwareExport_HostReport.csv" -noTypeInformation

  1. Disconnect session from VC #

*

disconnect-viserver -confirm:$false

*

exit

0 Kudos
3 Replies
LucD
Leadership
Leadership

I'm not sure what you wanted to see for CPU and memory utilisation.

But the following script gives both properties as percentages and the value is the actual realtime value at the time the script is run.

$vcserver = "xxxxx"
connect-VIServer $vcserver
#Setting Variables
Write-Host "Setting Variables...Please wait"
$VMs = Get-VM
$VMHs = Get-VMHost
$report = @()
foreach ($vmh in $vmhs)
{
	$hosts = Get-VMHost $vmh.Name | %{Get-View $_.ID}
	$row = "" | select-Object Name, NumCpuCores, Hz, Memory, CpuUtil, MemUtil
	$row.Name = $hosts.Name
	$row.NumCpuCores = $hosts.Hardware.CpuInfo.NumCpuCores
	$row.Hz = [math]::round(($hosts.Hardware.CpuInfo.Hz)/10000, 0)
	$row.Memory = [math]::round(($hosts.Hardware.MemorySize)/1024, 0)
	$row.CpuUtil = (Get-Stat -Entity $vmh -Stat cpu.usage.average -Realtime -MaxSamples 1 | where {$_.Instance -eq ""}).Value
	$row.MemUtil = (Get-Stat -Entity $vmh -Stat mem.usage.average -Realtime -MaxSamples 1).Value
	$report += $row
}
$report | Export-Csv "c:\VMwareExport_HostReport.csv" -noTypeInformation
disconnect-viserver - confirm:$false

Since the forum SW has some problems with square brackets I attached the script.


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

0 Kudos
kaizenwerks
Contributor
Contributor

I was looking to see how I obtain the total amount of CPU and RAM utilization for all hosts. I understand that this can be done easily in excel but I wanted to understand how this is done in Powershell. Much appreciated.

0 Kudos
kosmasj
Contributor
Contributor

Hello...

How would you make the script get the CPU and Memory for a specific month then average that out?

0 Kudos