Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Powershell and PERFMON counters

Status
Not open for further replies.

kmcferrin

MIS
Jul 14, 2003
2,938
US
Hi all, I'm curious if the following is possible. I know that using Powershell you can access and read PERFMON counters. But what if you have a Powershell script that does some activity that needs to log data into a custom counter?

For example, let's say that I want to write a Powershell script that processes files in a specific way. I want to be able to log the results of a batch process into a counter that can be watched by PERFMON or a third-party monitoring tool that can read these counters. The Powershell script would need to be able to create a new object for the job name and two counters for that object (Elapsed Time and Files Processed), then write the values to those counters. Of course ideally it would verify the existence of the object/counters and then create it if they did not exist.

Am I dreaming here?

________________________________________
CompTIA A+, Network+, Server+, Security+
MCTS:Windows 7
MCSE:Security 2003
MCITP:Server Administrator
MCITP:Enterprise Administrator
MCITP:Virtualization Administrator 2008 R2
Certified Quest vWorkspace Administrator
 
I posted the same question on MSDN forums and got the code below in response. I thought that I would include it here in case anyone else ran into the issue.

Code:
You're in luck!  Ignore the numbers on each line.

Part 1...  To add a custom counter:

To add:

[URL unfurl="true"]http://msdn.microsoft.com/en-us/library/5e3s61wf.aspx[/URL] 

  1 $counterdatas=new -object System.Diagnostics.CounterCreationDataCollection

  4 $cdcounter1=new -object System.Diagnostics.CounterCreationData
  5 $cdcounter2=new -object System.Diagnostics.CounterCreationData
  6 cdCounter1.CounterName = "Counter1";
  7 $cdCounter1.CounterName = "Counter1";
  8 $cdCounter2.CounterName = "Counter2";
  9 $cdCounter1.CounterHelp = "help string1"
 10 $cdCounter2.CounterHelp = "help string2"
 11 $cdCounter1.CounterType = System.Diagnostics.PerformanceCounterType.NumberOfItems64
 12 [System.Diagnostics.PerformanceCounterType]
 13 $cdCounter1.CounterType = "NumberOfItems64"
 14 $cdCounter2.CounterType = "NumberOfItems64"
 15 $CounterDatas.Add($cdCounter1)
 16 $CounterDatas.Add($cdCounter2)
 18 [System.Diagnostics.PerformanceCounterCategory]::Create("MultiCounter Category", "Category help","SingleInstance",$CounterDatas)

Part 2...  Write a value:

[URL unfurl="true"]http://msdn.microsoft.com/en-us/library/wzdx16ez.aspx[/URL] 

 34 $mycounter=new -object new System.Diagnostics.
PerformanceCounter
 35 $mycounter=new -object System.Diagnostics.PerformanceCounter
 36 $myCounter.CategoryName = "Multi Counter Category";
 37 $myCounter.CounterName = "SingleInstance";
 42 $mycounter.readonly=$false
 43 $mycounter.increment() <-See above URL for different increment options
 44 $mycounter.rawvalue

I haven't actually tested it yet. One of our .NET developers ended up writing a helper DLL that handles some of the more advanced functions, and I can load that via Add-Type and make use of it's methods instead.

________________________________________
CompTIA A+, Network+, Server+, Security+
MCTS:Windows 7
MCSE:Security 2003
MCITP:Server Administrator
MCITP:Enterprise Administrator
MCITP:Virtualization Administrator 2008 R2
Certified Quest vWorkspace Administrator
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top