Get-EDSData part 2

When we left our intrepid hero, I had just completed a very basic script to collate a single performance counter from the daily performance logs of an Exchange 2013 server into its own .blg file. The code for this script looked like this...


$Data = Import-Counter -path "C:\Program Files\Microsoft\Exchange Server\V15\Logging\Diagnostics\DailyPerformanceLogs\*.blg" -counter \\$Server\"$Counter"
$Data | Export-Counter -Path \\$Server\C$\temp\WorkingSet.blg

For today’s post, I have two goals. One, the code of this script needs a bit of cleaning up. As it turns out, not everyone installs Exchange in the default folder. My second goal is to get this script to tell us if the performance counter in question has gone over a threshold we set with a parameter. First let’s clean up this code

$Data = Import-Counter -path "C:\Program Files\Microsoft\Exchange Server\V15\Logging\Diagnostics\DailyPerformanceLogs\*.blg" -counter \\$Server\"$Counter"

After a little bit of investigating, I figured out that $env:exchangeinstallpath will give you the install directory of Exchange. Additionally, there is no reason for $Counter to be in quotes on that line. So that line turns into

$Data = Import-Counter -Path $env:exchangeinstallpath\Logging\Diagnostics\DailyPerformanceLogs\*.blg -Counter \\$Server\$Counter 

The other line of this script also could use a bit of cleaning up

$Data | Export-Counter -Path \\$Server\C$\temp\WorkingSet.blg

Instead of using \\$Server\C$\Temp, it’s a bit more universal if we use $home. I also needed to run this script a bunch of times in testing, and got tired of it telling me that WorkingSet.blg already exists. If I add the –Force parameter, it will overwrite that file if it exists. With the changes, that line becomes

$Data | Export-Counter -Force -Path $home\WorkingSet.blg

Doesn’t that all look better? Now on to finding a way to set a threshold for this counter, and have the script tell us if the threshold is passed without having to open the .blg file. It’s important to note at this point that for some performance counters you want to know if it goes over your threshold and for other performance counters you want to know if it goes under. You may want to know if you server goes over 80% CPU, or if it goes under 5% available RAM.

To make this all work, first thing we need is a few more parameters.





Here I added 3 parameters. $Threshold with nothing special, and two switch parameters for Over and Under. A switch parameter is automatically set to false unless you add that switch to the command, in which case it is set to true. This will allow us to test going over or under a threshold for any given performance counter.

The next addition to our script is this line.

$Samples = $Data.CounterSamples.Count

This line counts the number of counter samples that occur for our counter in our data set.

Next, let’s count the number of times that our counter goes over our threshold.

$Count = 0

If($Over -eq $True)
  {$Count = ($Data.CounterSamples.CookedValue | Where-Object {$_ -gt $Threshold}).Count}

Here is have an “If” statement. If statements work when the value in the parentheses is true, the script will perform the action in the curly brackets. In this case, if you include the –Over switch in the command line, the script will create a variable called $Count. $Data.CounterSamples.CookedValue gives us just the values from the counter since that is what we want to count. The where-object command looks at each number from the counter samples and sees if it is greater than the threshold we set. Each time it is greater than our threshold, it increments the count method. Net result here is we end up with a variable name $Count that is equal to the number of times our performance counter was greater than the threshold we set. Awesome!

Now let’s add a line to tell us how many times we went over our threshold.

If( ($Count -gt 0) -And ($Over = $True) )
    "$Counter went over the threshold you set $Count times in $Samples samples"

Another If statement, because we only want this line to fire if we asked for the Over results, and if we have a result to give. If both those things are true, it runs the code in the curly brackets, which in this case is one blank line, then a statement that says our counter name back to us and tells us how many times it went over the threshold. This is also where I used the $Samples result.

Let’s do the same thing, but for under thresholds.

$Count = 0

If($Under -eq $True)
  {$Count = ($Data.CounterSamples.CookedValue | Where-Object {$_ -lt $Threshold}).Count}
If( ($Count -gt 0) -And ($Under = $True) )
    "$Counter went under the threshold you set $Count times in $Samples samples"

Just some minor changes here, so I am not going to go through them all again. If you have any questions, ask in the comments.

Now to wrap this thing up, let’s output our data into a .blg file.

$Data | Export-Counter -Force -Path $home\WorkingSet.blg 

"Your performance counter file is ready at $home\WorkingSet.blg"

I already discussed the Export-Counter line. The other line is just there so you know where the WorkingSet.blg file is.

So there you have it. Here is a copy of what I get when I run this script looking at % Processor Time with a threshold of over 50.

PS C:\> ./Get-EDSData.ps1 –Server Ex01 –Counter “Processor(_Total)\% Processor Time” -Threshold 50 -Over

Processor(_Total)\% Processor Time went over the threshold you set 8 times in 10553 samples

Your performance counter file is ready at C:\Users\administrator.LAB\WorkingSet.blg

I have posted this script to TechNet Gallery. It can be found here.

Feel free to let me know what functionality you think this script needs in the comments.