PowerShell and Performance Monitoring

PowerShell, Disks and Perfmon

I will guide you through integrating pure PowerShell cmdlets with WMI classes that specialise in performance measurements.

Topics for PowerShell and Perfmon

 ♣

Our Mission

Let you into a secret, when I ran this script I was amazed that the disk time was 98%.  The disk idle time was 2%, so at least the maths added up to 100%.  However, I refused to believe the figures as the computer I was not running any programs.  I checked my code, could I have reversed the variables?  To double-check I launched Perfmon, whereupon I could see the ‘problem’.  SearchIndexer was busy at work making heavy use of the disk.  Phew, my script was spot on, silly me for doubting it.  I mention this because when ever I turn PowerShell onto the operating system, I always learn something new.

PowerShell Objectives

  • To see how easy it is to create $variables.
  • To create PowerShell scripts to measure the performance of resources such as disks.

Guy’s Advice

Either start with the basics in Example 1 (recommended), or else if you are in a hurry, cut to the chase, and head for Example 2.

Example 1: Research WMI’s Classes for PerfDisk Counters

# PowerShell Performance Monitor with PerfDisk Counters

Clear-Host
$WMIClass = "perfdisk"
$WMIPerf = Get-WmiObject -List |where {$_.name -Match "$WMIClass"}
$WMIPerf; "There are " + $WMIPerf.count + " matches for $WMIClass"

Learning Points

Note 1:  Guy loves employing variables in PowerShell.  There are two advantages of variables, firstly, we can easily change the filter (-Match).  Secondly creating the variable $WMIPerf allows us to call for the .count property.  Incidentally, I also believe that variables help me draw your attention to the most significant feature of the script.

Note 2: The result reveals 2 x 2 WMI Classes.  Two classes for Logical Disk and two for Physical Disk.  Also, two for RawData and two for Formatted data.  Another confession, when I erroneously used FormattedData instead of RawData, one of my other scripts did not produced the numeric data I had hoped for.

Win32_PerfFormattedData_PerfDisk_LogicalDisk
Win32_PerfRawData_PerfDisk_LogicalDisk
Win32_PerfFormattedData_PerfDisk_PhysicalDisk
Win32_PerfRawData_PerfDisk_PhysicalDisk

Challenge: Just for fun edit "perfdisk" to plain "perf".  Run the script again.  I got a 189 ideas for more scripts!

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Example 2: Win32_PerfRawData_PerfDisk_LogicalDisk

This, and similar scripts, will only return data if you are logged on as an administrator.

# PowerShell script to research disk properties
Clear-Host

$Disk = Get-WmiObject -class Win32_PerfRawData_PerfDisk_LogicalDisk
$Disk | Get-Member

Learning Points

Note 1:  The above little script reveals over 50 disk properties.  The main features of these counters are Read or Write, Average or Percentage.

Example 3: Preliminary Script to Get to Know PerfDisk

What we are going to do is create a Disk Object, and then interrogate some of its properties.  For instance, we may want see if the disk is primarily reading or writing data.

# PowerShell Script to measure disk reads and writes

Clear-Host
$Disk = Get-WmiObject -class Win32_PerfRawData_PerfDisk_LogicalDisk `
-filter "name= ‘_Total’ "
$DBytes = $Disk.DiskBytesPerSec;
$DRead = $Disk.DiskReadBytesPerSec;
$DWrite = $Disk.DiskWriteBytesPerSec;

"Disk KBytes / sec = " + [int]($DBytes /1000)
"Disk Reads / sec = " + [int]($DRead /1000)
"Disk Writes / sec = " + [int]($DWrite /1000)

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Example 4: PowerShell Script to Measure Disk Activity

The principle behind this script is taking two snapshots.  We measure disktime once, tell the script to sleep, then measure the same counters for a second time.  Script languages in general, and PowerShell in particular are good at maths, therefore, there are a rich set of techniques to compare two sets of data.  Finally, we are going to add formatting to display the results clearly.

Clear-Host
$numRep=3
$Sleep=5
$Idle1=$DiskTime1=$T1=$Idle2=$DiskTime2=$T2=$numRep=3

for ($i=1; $i -le $numRep; $i++)
{
$Disk = Get-WmiObject -class Win32_PerfRawData_PerfDisk_LogicalDisk `
-filter "name= ‘_Total’ "
[Double]$Idle1 = $Disk.PercentIdleTime
[Double]$DiskTime1 = $Disk.PercentDiskTime
[Double]$T1 = $Disk.TimeStamp_Sys100NS

start-Sleep $Sleep

$Disk = Get-WmiObject -class Win32_PerfRawData_PerfDisk_LogicalDisk `
-filter "name= ‘_Total’ "
[Double]$Idle2 = $Disk.PercentIdleTime
[Double]$DiskTime2 = $Disk.PercentDiskTime
[Double]$T2 = $Disk.TimeStamp_Sys100NS

"Repetition $i … counting to $numRep…"

$PercentIdleTime =(1 – (($Idle2 – $Idle1) / ($T2 – $T1))) * 100
"`t Percent Disk Idle Time is " + "{0:n2}" -f $PercentIdleTime
$PercentDiskTime =(1 – (($DiskTime2 – $DiskTime1) / ($T2 – $T1))) * 100
"`t Percent Disk Time is " + "{0:n2}" -f $PercentDiskTime

}

Learning Points

Note 1:  Most of the code is inside a simple ‘For’ loop.  Inside the loop are three sections, the first snapshot, the second comparison data capture, and result section containing the simple maths

Note 2: The formatting employs -f to control the decimal places {0:n2).  Zero refers to the one and only element and :n2 means two places of decimal.

Where Next?

The main purpose of this page is to get you started with PowerShell.  I firmly believe that once you get success from a few simple command, you will be curiosity to achieve more with PowerShell.

See WMI Memory ยป

Summary of PowerShell and Performance Monitoring

PowerShell can link with WMI classes to produce performance measurements.  You can use PowerShell to monitor both physical and logical disk counters.  The key to investigation your particular machine is to research WMI classes containing perfdisk, then use Get-Member to reveal the appropriate properties.

If you like this page then please share it with your friends

 


See More Windows PowerShell Examples of Real-life Tasks

PowerShell Tutorials  • PowerShell Examples  • IpConfig  • Get-Counter  • PowerShell NetSh

Monitor Performance – PowerShell  • PowerShell temp   • PowerShell Delete Temporary files

PowerShell WOL (Wake-on-Lan)  • Services   • Change Computer Description Registry

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.