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.
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).
Method 1 (Quick)
Copy the code into memory
Launch PowerShell
Right-click the PowerShell symbol
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)
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.
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!
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.
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
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
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.
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.
$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
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.
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.