Tuesday, January 25, 2011

Nothing To DO With PowerShell

Just a quick update to plug one of my other projects: py-ad-ldap.  Have you ever wanted to run scripts against Active Directory but not wanted to run them on Windows?  You haven't?  Well, if you had, then this would have been the Python module for you.  I haven't packaged it up for download yet (hopefully tomorrow), but it's pure Python, so there are no fancy requirements for installing it, so mosey on down to the repository and take a look at the source files if you're interested.

Sunday, January 23, 2011

Getting Performance Counters in PowerShell

I had a friend ask me recently if there was an easy way to monitor arbitrary performance counters in a script, and I didn't really have time to think about it at the time, but this weekend I decided to give it a whirl, and I came up with this function, that I'll definitely be putting in my profile at work.

The way it works is pretty straightforward, but you need to know some background about Performance Counters if you haven't used them before.  There are basically four pieces of information needed to retrieve a counter:

  • Category
  • Counter
  • Instance
  • Computer
If these don't ring a bell right away, fire up Perfmon, right-click and select Add Counters, and you'll see something like this:

In the top-left box you can see the Categories, and if you expand them you'll see the Counter names, and bottom left are the Instances.

Classic example: you want to find out the Current Disk Queue Length on your C: drive.
  • Category = 'LogicalDisk'
  • Counter = 'Current Disk Queue Length'
  • Instance = 'C:'
  • Computer = '.' (default, it means "this computer")
Using Get-PerformanceCounter, you'd do it like this:

Get-PerformanceCounter LogicalDisk 'C:' 'Current Disk Queue Length'

Or lets say you want to get all of the counters for the C: drive:

Get-PerformanceCounter LogicalDisk 'C:'

Anyway, here's the function, and you can also find it here: http://poshcode.org/2475