PowerCLI Script to Automate Host Performance Reports

Automate Host Performance Reports

Here is a very simple Powercli script for host performance reports.  You can take this script and schedule it on your Windows vCenter to automate host performance reports in your environment.  The script can be run on demand or scheduled and can be customized for any date range.  Small and simple was the plan with this script.

Script Details

In the first 15 lines, we are just creating variables, loading the VMware modules, and connecting to vCenter.  This is how most scripts start off and should be no big surprise to anyone who does scripting regularly.  Remember to set information like the ID/PW, date range, and vCenter server.  Future versions of this script will encrypt the password in a credential file.  Also notice you can either select the current date and subtract or add time or just put in a regular date format (12/1/2018).

The second part is looping through all the hosts in the environment and pulling CPU, Memory, Disk, and Network data.  Depending on the size of your environment, this could take some time depending on your date range.  Most environments will complete within a few minutes.

As you can see, I use to have the script pull VM data as well as host data but decided to have 2 scripts and separate the tasks.  You can easily add a loop to collect VM data but I do recommend keeping them separate unless you would want host and VM data in the same spreadsheet.

The last section just outputs the variable arrays into a CSV file.

That’s it.  See how easy it is to automate host performance reports in your environment?

Please let me know if this script was helpful to you or if you have any changes you thing I should add to the script.  The purpose was to keep the script easy and small but I would be happy to include your ideas into a new script and post it in a future article.  If you want to receive more scripts to help you automate your environment, signup for our email list and you will get updated when I create new posts.

There is no reason you should be running your reports manually.  Automate host performance reports today and spend your time doing other things!

PowerCLI Script Host Performance

Get-PerformanceData.ps1

#requires -version 2
<#
.SYNOPSIS
  This script will gather performance data from a VC server and export the information in a csv file.

.DESCRIPTION
  <Brief description of script>

.PARAMETER <Parameter_Name>
  vCenter Name
  Number of Days
  vCenter Account
  vCenter Account Password

.INPUTS
  vCenter Name, Number of Days, vCenter Account, vCenter Account Password.

.OUTPUTS
  CSV file stored in the script working directory and will be called "(Date)-PerformanceData-(ServerName).csv"

.NOTES
  Version:        1.0
  Author:         TJ Totland
  Creation Date:  2018-06-30
  Purpose/Change: Updated to template
  
.EXAMPLE
  .\Get-PerformanceData.ps1 ServerName 7 VMWareID password


#>
 param(    
    [Parameter(Mandatory=$True)]  
    [String]$VCServer,

    [Parameter(Mandatory=$True)] 
    

[int]

$Days, [Parameter(Mandatory=$False)] [String]$AdminID, [Parameter(Mandatory=$False)] [String]$AdminPW ) #Add-PSSnapin VM* Get-Module -Name VMware* -ListAvailable | Import-Module #$VCServer = “” #$AdminID = “” #$AdminPW = “” #$Days = 7 $OutputCSV = “.\” + (get-date).tostring(“yyyyMMdd”) +”-PerformanceData-” + $VCServer + “.csv” cd $PSScriptRoot $allvms = @() $allhosts = @() $Days = 7 Connect-VIServer -server $VCServer -User $AdminID -Password $AdminPW $hosts = Get-VMHost $vms = Get-Vm foreach($vmHost in $hosts){ $hoststat = “” | Select HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin, DiskMax, DiskAvg, DiskMin, NetMax, NetAvg, NetMin $hoststat.HostName = $vmHost.name $statcpu = Get-Stat -Entity ($vmHost) -Start (get-date).AddDays(-$Days) -Finish (get-date) -MaxSamples 10000 -stat cpu.usage.average $statmem = Get-Stat -Entity ($vmHost) -Start (get-date).AddDays(-$Days) -Finish (get-date) -MaxSamples 10000 -stat mem.usage.average $statdisk = Get-Stat -Entity ($vmHost) -Start (get-date).AddDays(-$Days) -Finish (get-date) -MaxSamples 10000 -stat disk.usage.average $statnet = Get-Stat -Entity ($vmHost) -Start (get-date).AddDays(-$Days) -Finish (get-date) -MaxSamples 10000 -stat net.usage.average $cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum $mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum $disk = $statdisk | Measure-Object -Property value -Average -Maximum -Minimum $net = $statnet | Measure-Object -Property value -Average -Maximum -Minimum $hoststat.CPUMax = [System.Math]::Round($cpu.Maximum) $hoststat.CPUAvg = [System.Math]::Round($cpu.Average) $hoststat.CPUMin = [System.Math]::Round($cpu.Minimum) $hoststat.MemMax = [System.Math]::Round($mem.Maximum) $hoststat.MemAvg = [System.Math]::Round($mem.Average) $hoststat.MemMin = [System.Math]::Round($mem.Minimum) $hoststat.DiskMax = [System.Math]::Round($disk.Maximum) $hoststat.DiskAvg = [System.Math]::Round($disk.Average) $hoststat.DiskMin = [System.Math]::Round($disk.Minimum) $hoststat.NetMax = [System.Math]::Round($net.Maximum) $hoststat.NetAvg = [System.Math]::Round($net.Average) $hoststat.NetMin = [System.Math]::Round($net.Minimum) $allhosts += $hoststat } $allhosts | Select HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin, DiskMax, DiskAvg, DiskMin, NetMax, NetAvg, NetMin | Export-Csv $OutputCSV -noTypeInformation $allhosts | Select HostName, MemAvg, CPUAvg, DiskAvg, NetAvg | FT DisConnect-VIServer -server $VCServer -confirm:$false

TJ Totland

Todd "TJ" Totland is a computer and network engineer working for IBM. He is certified as a MCSE, MCT, CNE, and CNA with vast experience in many technologies used in businesses today. TJ has designed, built, and managed hundreds of different types of computer and network systems for large and small customers since 1990. He is a subject Matter Expert in Cloud Technologies and has vast knowledge in VMware products.