LoadGen automatically get VMware Host CPU % Utilisation via PowerShell/Vmware PowerCLI

During loadtesting with LoadGen Load and Performance (https://www.loadgen.com/load-and-performance-testing) I was making screenshots of the VMware vCenter host CPU % utilisation. Doing so, required a person to be active during the loadtest and capture screenshots.

Requirments:
1. VMware PowerCLI installed: https://code.vmware.com/web/tool/12.0.0/vmware-powercli
2. LoadGen – Load and Performance installed
3. VMware host where the VDI’s are available


To automate this proces the power of LoadGen custom start actions and Powershell+VMware PowerCLI was used. See the script below:

##############ENTER vSphere Information#######
$vcuser = "DOMAIN\USERNAME"
$vcpassword = "PASSWORD"
##############################################
Set-PowerCLIConfiguration -Scope AllUsers  -InvalidCertificateAction Ignore -Confirm:$false | Out-Null
Set-PowerCLIConfiguration -Scope AllUsers  -ParticipateInCEIP $false -Confirm:$false | Out-Null
Connect-VIServer -Server VCENTER.FQDN -user $vcuser -Password $vcpassword  | Out-Null

$LoadHost = Get-VMHost -Name "VMWAREHOST.FQDN"
$LoadHostAllInfo = $LoadHost  | Select-Object *
$CurrentTime = Get-Date -Format dd-MM-yyyy-HH-mm
#Value is based on 100 minutes (total loadtest time). This is 6000 seconds. Start-Sleep is 30 seconds, so the loop needs to run 200 times (6000/30).
$ValueTotalTime = 200

do{
    $TotalLoad = $LoadHost | Get-Stat -Stat cpu.usage.average -IntervalMins 1 -MaxSamples 1 | Where{$_.Instance -eq "" -and $_.Value -gt 1} | Select-Object -Property @{Name='Timestamp';Expression={Get-Date $_.Timestamp -Format 'hh:mm:ss'}}, @{Name='Value';Expression={[math]::round($_.Value)}} | Export-Csv "D:\CPULoadInformation-$($CurrentTime).csv" -NoTypeInformation -Append
    Start-Sleep -Seconds 30
    $ValueTotalTime--
    Write-Host "Loop: " $ValueTotalTime
} while($ValueTotalTime -gt 0)

The script has some small variables which need to be changed or assumed you have a D:\ drive on your system:
1. $vcuser
2. $vcpassword
3. Get-VMHost -Name “VMWAREHOST.FQDN” (your host where the VM’s are hosted)
4. Connect-VIServer -Server VCENTER.FQDN (your vCenter server)
5. Export-Csv “D:\CPULoadInformation-$($CurrentTime).csv” (assuming you have a D:\ drive. Else just change the path)


The script needs to be called from a .bat/.cmd file from LoadGen Director:


Set the .bat/.cmd script in the Load scenario events -> Load scenario stared:

When the Loadtest will start the following screen will popup so you can see the progress and that the script is being run:

The output that will be generated will look as follows:

With some easy Excel magic the following is created (manually)

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *