18

I'm trying to move the verification & creation of my performance counter groups, and the counters themselves, out of my web service and into a powershell script that's run during deployment.

Can this be done? Or am I stuck using a simple app to build the groups & counters?

Thanks much :)

b34r
  • 589
  • 1
  • 10
  • 22
  • I'd also love to know if there is a way to update the counters from a powershell script or command line? – Ron Tuffin Oct 08 '10 at 09:23
  • How do we use PowerShell to create a permanent counter for Average64, those requiring AverageBase? – Kode Nov 20 '17 at 20:51

1 Answers1

14

Figured this out a while ago, but never posted.
Here's my solution:

//save out the type name for sanity's sake
$ccdTypeName = 'System.Diagnostics.CounterCreationData'
$CounterCollection = New-Object System.Diagnostics.CounterCreationDataCollection

//create as many counters as we'd like, and add them to the collection. here's just one:
$CounterCollection.Add( (New-Object $ccdTypeName "Counter Name", "Counter Description", NumberOfItems32) )

//create the category with the counter collection
[System.Diagnostics.PerformanceCounterCategory]::Create($perfCounterCategoryName, $perfCounterVersion, [Diagnostics.PerformanceCounterCategoryType]::SingleInstance, $CounterCollection); 
b34r
  • 589
  • 1
  • 10
  • 22
  • 1
    Nice example of how to in detail on TechNet as of 2014. http://blogs.technet.com/b/omx/archive/2014/04/23/use-powershell-to-create-and-update-performance-counters.aspx – wonster Jan 22 '15 at 00:51
  • Note from the blogs.technet.com post, "The instances will go away when PowerShell exits. These instances are only available while your script is running." If you convert either this SO answer to C# or the blog's code to C#, they are permanent. – Andy Dec 13 '16 at 21:13