= Script: powerCLI: vSphere Host Performance Script =
**Summary**: Use this powershell script to query all hosts and their performance status. \\
**Date**: 25 May 2014 \\
**Refactor**: 1 May 2025: Checked links and formatting. \\
{{tag>vmware powershell}}
This script will query all hosts and audit their performance doing. It will tell you your core overcommitment, number of VMs, and more.
= The Script =
# Author: Sjoerd Hooft
# Date: 20140525
# Version Description:
## Second version of Audit Script
## Functions right now
# Connect to vCenter
# Get Host performance information regarding CPUs, RAM, VMs
# Notes
## Stats can be available for: Get-StatType -Entity (Get-VMHost $esxihost) -Interval "Past Week"
## cpu.usage.average
## cpu.usagemhz.average
## cpu.ready.summation
## mem.usage.average
## mem.swapinRate.average
## mem.swapoutRate.average
## mem.vmmemctl.average
## mem.consumed.average
## mem.overhead.average
## disk.usage.average
## disk.maxTotalLatency.latest
## net.usage.average
## sys.uptime.latest
## clusterServices.cpufairness.latest
## clusterServices.memfairness.latest
# Variables
$scriptname = [System.IO.Path]::GetFilenameWithoutExtension($MyInvocation.MyCommand.Path.ToString())
$scriptlocation = Split-Path $myinvocation.mycommand.path
$timestamp = Get-Date -format "yyyyMMdd-HH.mm"
$weekdate = Get-Date -uformat %V
$todayMidnight = (Get-Date -Hour 0 -Minute 0 -Second 0).AddMinutes(-1)
$vcenters = @("vcenter1.getshifting.local","vcenter2.getshifting.local")
$MailServer = "mailserver.getshifting.local"
$toemail = "sjoerd @ _getshifting.com"
$fromemail = "monitoring @ _getshifting.com"
$csvfile = "$scriptlocation\$scriptname-vSphere.csv"
# Start-transcript
start-transcript -path "$scriptlocation\$scriptname.txt"
# Add VMware snapin
if(-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)){
Add-PSSnapin VMware.VimAutomation.Core}
# Extra custom properties for quick host vm cpu counting
New-VIProperty -ObjectType VMHost -name NumvCPUs `
-Value {$TotalvCPU = 0
$Args[0] | Get-VM | Foreach {
$TotalvCPU += $_.NumCPU}
$TotalvCPU
} -Force
New-VIProperty -ObjectType VMHost -name NumPoweredOnvCPUs `
-Value {$TotalvCPU = 0
$Args[0] | Get-VM | Where { $_.PowerState -eq "PoweredOn" } | Foreach {
$TotalvCPU += $_.NumCPU}
$TotalvCPU
} -Force
New-VIProperty -ObjectType VMHost -name NumvRAM `
-Value {$TotalvRAM = 0
$Args[0] | Get-VM | Foreach {
$TotalvRAM += $_.MemoryMB}
$TotalvRAM
} -Force
New-VIProperty -ObjectType VMHost -name NumPoweredOnvRAM `
-Value {$TotalvRAM = 0
$Args[0] | Get-VM | Where { $_.PowerState -eq "PoweredOn" } | Foreach {
$TotalvRAM += $_.MemoryMB}
$TotalvRAM
} -Force
# Functions
# Email functions
Function Send-Email ($subject, $info){
Send-MailMessage -To $toemail -From $fromemail -SmtpServer $mailserver -Subject $subject -Body $info -Attachments "$attachment"}
Function AuditAllHosts ($vcenters){
# Create an array to collect all audit information
$myCol = @()
ForEach ($vcenter in $vcenters){
# Connect to vCenter
Connect-VIServer $vcenter
# Get Audit properties for all hosts
ForEach ($esxihost in (Get-VMhost)){
$cpuaverage = Get-Stat -Entity ($esxihost) -Stat cpu.usage.average -Start $todayMidnight.AddDays(-7) -Finish $todayMidnight.AddDays(-1) | where{$_.Instance -eq ""} | Measure-Object value -average
$ramaverage = Get-Stat -Entity ($esxihost) -Stat mem.usage.average -Start $todayMidnight.AddDays(-7) -Finish $todayMidnight.AddDays(-1) | where{$_.Instance -eq ""} | Measure-Object value -average
$HostInfo = "" |select-Object HostName,Cluster,VMCount,VMCountPoweredOn,NumCPU,NumvCPUs,NumPoweredOnvCPUs,CoreRatio,Hyperthreading,CpuTotalMhz,CPUAverageUsage,MemoryTotalMB,NumvRAM,NumPoweredOnvRAM,RAMLeft,RAMAverageUsage
$HostInfo.HostName = $esxihost.Name
$HostInfo.Cluster = (get-vmhost $esxihost | get-cluster).Name
$HostInfo.VMCount = (get-vmhost $esxihost | get-vm).Count
$HostInfo.VMCountPoweredOn = (get-vmhost $esxihost | get-vm | where {$_.Powerstate -eq "PoweredOn"}).Count
$HostInfo.NumCPU = $esxihost.NumCPU
$HostInfo.NumvCPUs = $esxihost.NumvCPUs
$HostInfo.NumPoweredOnvCPUs = $esxihost.NumPoweredOnvCPUs
$HostInfo.CoreRatio = $HostInfo.NumPoweredOnvCPUs/$HostInfo.NumCPU
$Hostinfo.Hyperthreading = $esxihost.hyperthreadingactive
$HostInfo.CpuTotalMhz = $esxihost.CpuTotalMhz
$HostInfo.CPUAverageUsage = $cpuaverage.average
$HostInfo.MemoryTotalMB = $esxihost.MemoryTotalMB
$HostInfo.NumvRAM = $esxihost.NumvRAM
$HostInfo.NumPoweredOnvRAM = $esxihost.NumPoweredOnvRAM
# Note: The line below does not consider memory usage by overhead like the host itself or overhead for virtual hardware
$HostInfo.RAMLeft = $esxihost.MemoryTotalMB - $esxihost.NumPoweredOnvRAM
$HostInfo.RAMAverageUsage = $ramaverage.average
$myCol += $HostInfo
}
# Disconnect from vCenter
Disconnect-VIServer * -Confirm:$false
}
$myCol |Export-csv -NoTypeInformation $csvfile
# Send Report
$attachment = $csvfile
$subject = "vSphere Audit Report for week $weekdate"
$info = "Find the report attached to this email. Kind regards, Sjoerd Hooft."
Send-Email $subject $info
}
# Cleanup files from last run
Remove-Item $csvfile
#Start Actual Script
AuditAllHosts $vcenters
# Stop logging
stop-transcript
//This wiki has been made possible by://