This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
This post will give 2 examples of using Performance monitor in MSH.
the first example with WMI and the Second example with the PerformanceCounter Object (.NET)
let's start with the WMI way:
get the % processortime of MSH :
(get-wmiobject Win32_PerfFormattedData_PerfProc_Process -filter "name = 'msh'").PercentProcessorTime
b.t.w. take care with this as the name is not the Key (the ID is), so if you get nothing back on this command, its not because there is not MSH process (as we are SURE there is, as we are in it ;-) ), but that there are more, and we need to use the ID.
MSH>gps msh
Handles NPM(K) PM(K) WS(K) VS(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
372 13 44764 38516 182 5,84 3616 msh
563 13 34760 30672 173 4,41 5208 msh
MSH>(get-wmiobject Win32_PerfFormattedData_PerfProc_Process -filter "IDProcess = 5208").PercentProcessorTime
0
, leave out the property you will see all instances, so if you not use the key always take care that you can get an array back.
(get-wmiobject Win32_PerfFormattedData_PerfProc_Process -filter "name = 'msh'") foreach {$_.PercentProcessorTime}
but how I got to the name of this class ?
maybe you use : "
Wmi-Help Part 1 ", but you did not see those classes, this is because most of the time you don't need them most of the time.
but don't worry, It was prepared for this allready ;-)
# Booleans to filter non win32_ classes and perfmon Classes
$bWin32 = $true
$bPerf = $true
You can find this switches in the script, if you set both to $false it will not filter those classes out, so you can look up the available performance classes, you can get the properies by just not specifying one, or using GM as you have seen allready.
Note that there are 2 kinds of classes :
Win32_PerfFormattedData
Win32_PerfRawData
normaly try to use the PerfFormattedData classes as they are allready "Cooked",
the RawData You have to compute yourself.
for more info :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/performance_counter_classes.asp(also I will show a little example of the difference in the .NET version)
The .NET way :
# Get Perfcounter
$pc = new-object system.diagnostics.PerformanceCounter
$pc.CategoryName = "Process"
$pc.CounterName = "% Processor Time"
$pc.InstanceName = "msh"
$pc.nextvalue()
but How did I get the Inputparameters here ?
looked up in Performance monitor ?... No much better I just asked ;-)
First lets look at that Object
MSH>$pc = new-object system.diagnostics.PerformanceCounter
MSH>$pc
CategoryName :
CounterHelp :
CounterName :
CounterType :
InstanceLifetime : Global
InstanceName :
ReadOnly : True
MachineName : .
RawValue :
Site :
Container :
First it needs a Category, as said we don't want to look this up, so how do we get them, there is a other class in .NET for that :
MSH>[System.Diagnostics.PerformanceCounterCategory]::GetCategories()
CategoryName CategoryHelp CategoryType MachineName
------------ ------------ ------------ -----------
Terminal Services Session Terminal Services per-session r... MultiInstance .
IP The IP performance object consi... SingleInstance .
ASP.NET State Service ASP.NET State Service SingleInstance .
RSVP Interfaces RSVP Interfaces performance cou... MultiInstance .
Server The Server performance object c... SingleInstance .
this gives you a list of all available categories:
so we pick one :
MSH>$cat = new-object System.Diagnostics.PerformanceCounterCategory("Process")
MSH>$cat
CategoryName CategoryHelp CategoryType MachineName
------------ ------------ ------------ -----------
Process The Process performance object ... MultiInstance .performance object c... SingleInstance .
Ok, lets get the Counternames of that Object:
MSH>$cat.GetCounters()
Exception calling "GetCounters" with "0" argument(s): "Counter is not single instance, an instance name needs to be specified.".
At line:1 char:17
+ $cat.GetCounters( <<<< )
Oops, we have to have an InstanceName for that So first lets ask that :
MSH>$cat.GetInstanceNames()
VCSExpress
PRONoMgr
notepad#15
evmgr
boinc
MowConsole
notepad#13
(Notice the Numbering so we don't have the Name problems as with WMI, as this keeps the instances unique)
so now we can get the counters.
MSH>$cat.GetCounters("notepad#13")
CategoryName : Process
CounterHelp : % Processor Time is the percentage of elapsed time that all of process threads used the processor to execution instructi
ons. An instruction is the basic unit of execution in a computer, a thread is the object that executes instructions, and
a process is the object created when a program is run. Code executed to handle some hardware interrupts and trap condit
ions are included in this count.
CounterName : % Processor Time
CounterType : Timer100Ns
InstanceLifetime : Global
InstanceName : notepad#13
ReadOnly : True
MachineName : .
RawValue : 91562500
Site :
Container :
.....
.....
notice that already a sample is taken, but it's RAW (see WMI part).
in .net there are no different classes for "cooked" and "raw" data,
there are just 2 methods you can call GetSample for Raw data, nextValue for the "cooked" value, if you choose the raw data, you get the calculation info back also.
MSH>$pc.nextvalue()
0
MSH>$pc.nextSample()
RawValue : 44062500
BaseValue : 0
SystemFrequency : 3000160000
CounterFrequency : 10000000
CounterTimeStamp : 127786339665937500
TimeStamp : 3882482312876355
TimeStamp100nSec : 127786339664843750
CounterType : Timer100Ns
mostly you can use the "cooked" data but if you can not (NT4 ?), you can calculate your own values, but as this is not common I point you to the MSDN article for that.
or this scripting guy's article
http://www.microsoft.com/technet/scriptcenter/resources/qanda/apr05/hey0421.mspxor for a more torough explanation look here : (Realy Recomended!!)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/html/scripting11182003.aspalso I reccomend you to take a look at Lee's script as this gives an good example of an implemented solution, I hope this info helps you with that, take also notice about the Multi-CPU issue, you still need to calculate that ("medium cooked")
You see that you can do a lot with performance counters in MSH, and that especialy the .NET classes are very powerfull,.
Enjoy monitoring ;-)
gr /\/\o\/\/
PS. as was writing Lee Holmes wrote a example script also (inspired by the same NG post), :
http://www.leeholmes.com/blog/AccessingPerformanceCountersInMSH.aspx