In a previous article, I gave you an introduction to using performance counters with Hyper-V and PowerShell. Of course you can use performance counters to manage all sorts of things, but personally, memory is the most critical. Even though I’m running Hyper-V in a non-production setting and don’t have access to massive server farms and back-end storage, the concepts that I am discussing will still apply to those instances. So even though this article is going to look specifically at Dynamic Memory counters, you might need something else. I encourage you, as with all of my content, to look at the techniques and concepts and not focus entirely on the end result.

Finding Instances

I am running Hyper-V on my Windows 10 box and will be querying the local computer. But there’s no reason this won’t work with a remote Hyper-V server. Although I have not tested anything with Window Server 2019. To keep the code re-usable I’ll define a variable for the Hyper-V host. You naturally will set it to whatever you need.

I want to check for counter instances on a single virtual machine. In the previous article, I talked about getting counter data from instances so I’m going to use a regular expression pattern to retrieve the instances associated with a given VM.

This is what I get.

Dynamic Memory perfomance counters

I can pass this array of counters to the Get-Counter cmdlet.

Getting counter data

There are a few more but you get the idea.

Monitoring Performance Counters

One of the nice features in Get-Counter is that you can watch a given set of counters. I will trust that you will read full cmdlet help and examples. But I can run this command to collect 10 samples of the specified counters, taking a reading every 2 seconds.

Don’t get too gung-ho on your sampling interval.

The PowerShell output is far from user-friendly. You might want to save all the output into a variable or export to an XML file. Don’t use CSV. Or, let’s have PowerShell format the results into something a bit more manageable.

This is much easier to read.

Formatted performance counters in PowerShell

Or send it to Out-Gridview.

PowerShell performance counters in Out-Gridview

Of course, none of this is actual monitoring. Here’s a code sample that achieves the same result as my earlier snippet: 10 samples at a 2-second interval.

In this example, I am using the PowerShell console window as a monitoring tool. The performance data is written to the console with a refresh every 2 seconds.

Another option is to use my ConvertTo-WPFGrid function which is part of my PSScriptTools module which you can install from the PowerShell Gallery. With this command, I can display the performance counter data in a graphical form that will automatically refresh itself. Here’s a complete code snippet.

When run I get something like this:

Display performance counters in a WPF form

My code snippet is getting 5 samples every 10 seconds. You could also use a Do Loop or other techniques to achieve the same effect.

Fine-Tuned Performance Gathering

Before I wrap up for today let me leave you with some techniques for fine-tuning the capture of performance counter data.  Let’s say that I am interested in the Pressure counters for a single virtual machine. I can fine tune the regular expression pattern to retrieve just those performance counter instances.

Getting a subset of performance counters

Using this as the foundation and with some additional regular expression voodoo, I can build a little bit of monitoring code for a single virtual machine.

Finding the right dimensions takes a little bit of experimentation.

Memory Pressure Performance Data

Next time I’ll wrap up this series on Hyper-V performance counters in PowerShell with a technique I think you’ll find intriguing.

If you’re having problems with your PowerShell scripts use this guide to troubleshoot, diagnose problems and solve your PowerShell woes.