There’s Sum-thing Happening Here

calculatorI am one of those IT Pros who keeps close tabs on system resources. I like to know what is being used and by what. As you might imagine, a cmdlet like Get-Process, is pretty useful to me. One of the things I’m always checking is how much memory Google Chrome is taking. I don’t mean to pick on Chrome as I derive great benefit from it. But because I keep it open for days at a time I think system resources get a little carried away. So every once in a while I like to see how many Chrome processes are running and how much they are using.

Now, what I really want is a total. I can get that for a single property easy enough using Measure-Object.

But ideally I’d like to get totals for all of the properties I see with Get-Process. So I wrote an advanced PowerShell function called Get-ProcessTotal.

The main part of the function gets all instances of a process and then creates a custom object with a sum total for several properties.

The output also includes the computer name, in case you are querying a remote computer, the total count and also the original process objects in case you need to do something else with them.

That could be sufficient, but all the values would be in bytes and the default display would be a list. Instead I wanted default output like I get with Get-Process. So I took advantage of PowerShell’s extensible type system and created my own format data xml file. Actually what I did was to find the section in the DotNetTypes.format.ps1xml file for process objects, copied it and tweaked it.

My original idea was to have a separate XML file. If I was building a module, that would be the best choice. But this was a single, stand-alone function so I added some logic to create the XML file on first use and add it to my PowerShell session. The XML content is stored as a Here string in the function.

In short, the function first tests to see if there is format data for an object of type My.ProcessTotal. If not, in then tests for the XML file which I’m storing in the WindowsPowerShell folder. If it doesn’t exist, the file is created. In any event once the file exists it is then loaded into PowerShell using Update-FormatData.

How does PowerShell know what type to use? I told it. When I defined my custom object I also gave it a new type name.

Once the function is loaded into my session I can now get a result like this:

The output is just like Get-Process except the values are sums. I also added an option to get an average instead just in case. If you pipe to Get-Member you’ll see the new type definition, as well as the other non-default properties.

There are some other examples in the comment based help. The script also creates an alias so if you don’t want it be sure to comment out the line at the end.

I hope some of you will kick it around and let me know what you think. Or at the very least I hope you picked up a new technique or idea.

Get Process Detail

The other day I posted a snippet of code that I as using to monitor process memory utilization for a few web browsers. I thought the information and technique were useful enough to modularize in the form of a function. Perhaps I want to check working set on a different process or even on a different computer. This is what I came up with. Continue reading

Process Snapshot

Yesterday I ended up running an impromptu browser test, comparing memory utilization. See what Twitter can do to your time!! The browsers themselves are really irrelevant. What you might find useful is the little PowerShell code I put together to periodically check and compare the browser processes. Continue reading