Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



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.

Pre-requisites and Checklist

Download PowerShell from Microsoft's site.  One interesting point is that there are different versions of PowerShell for XP, Windows Server 2003 and Vista.  (For Windows Server 2008, you need Add Feature).PowerShell IpConfig

Method 1 (Quick)

  • Copy the code into memory
  • Launch PowerShell
  • Right-click the PowerShell symbolPowerShell and Ipconfig
  • Edit --> Paste
  • Press enter to execute the code
  • See screenshot to the right

Method 2 (Best)

  • Prepare to run cmdlets with this PowerShell command:
    set-ExecutionPolicy RemoteSigned
  • Copy the code below into a text file.
  • Save the file with a .ps1 extension, for example network.ps1
  • In PowerShell, navigate to where you saved network.ps1
  • Issue this command:
    .\network 
    (dot backslash filename)

Example 1: Research WMI's Classes for PerfDisk Counters

# PowerShell Performance Monitor with PerfDisk Counters
# Guy Thomas September 2008 Version 1.3

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: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft 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.

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
# Guy Thomas September 2008 Version 1.1
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
# Guy Thomas September 2008 Version 2.4
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 v10

SolarWinds' Orion 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.

Perhaps the NPM's best feature is the way it suggests solutions to network problems.  Its second best feature is 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 take advantage of SolarWinds' offer.

Download a free trial of the 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. 

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.

 


See More Windows PowerShell Examples of Real-life Tasks

PowerShell Tutorials  • PowerShell Examples  • IpConfig  • PowerShell NetSh

Monitor Performance - PowerShell  • Get-Counter  • PowerShell temp

PowerShell WOL (Wake-on-Lan)  • Services   • Windows PowerShell

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, SolarWinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: