PSMetrics Part 2: PowerShell Collector

Posted On 2016-11-11 by dwirch
Tags: Powershell Scripting Windows PSMetrics
Views: 429

Welcome back for part 2 of the series. In this post, I'll be putting together a simple collector script with PowerShell for gathering metrics on a Windows host, and sending the data to the ingestor.

The idea behind this script is to have it run at certain intervals, to gather performance measurement and send them off to the ingestor. For this particular example, I'll be grabbing total processor utilization as a percentage.

I see this script as being a starting point, or "template". Eventually, there will be four scripts. Each script will cover a category of measurements such as CPU, memory, storage, and networking. Further, other scripts can be built off the same template for specific applications or services, such as Exchange, IIS, SharePoint, etc. Basically, if you can take a measurement via PowerShell, you can send it up to the database.

The first section of the script is some basic setup.

$CounterName="\processor(_total)\% processor time"



Nothing magical here; we're just setting some values for what we want to watch and where we want to store it.

  • Grab the hostname from the environment.
  • Define what performance counter that is going to be observed. In this case, it is Total % Processor Time.
  • The next three variables define how the data is going to be stored in the database. A little organization now saves headaches later.
  • And finally, the $Endpoint variable stores the URL of the ingestor that we are storing data to.

We're also going to want to timestamp the datapoint, so we need to grab the current date/time.

$TimeStamp=Get-Date -format u

For this exercise, I'm just going to use the in-built Windows performance counters, which we can grab with Get-Counter. As mentioned previously, any measurement can be stored. Use your imagination!

$CounterValue=Get-Counter "\\$HostName$CounterName"

Now to put all the data together in a querystring, in preparation for sending it to the ingestor.


For this, I'm using Invoke-WebRequest to send the data to the ingestor, and getting the content back. The content will contain any error codes, if any.

$Results=(Invoke-WebRequest -Uri $Endpoint$Values -DisableKeepAlive -Method Post).content

At this point, this script will return "success" for a successful run. Any other errors are not trapped here, and you'll need to check the IIS logs on the ingestor to see what failed run was caused by. You could also post the URL manually in a browser to see if there is a problem.

In the final version of this script, I will be adding event logging to the Windows event log. In the event of a failed run, you'd just crack open the PSMetrics custom event log on the monitored host to see what the error is.

This temporary, rudimentary error processing shown below is basically just a placeholder.

if($Results -ne "success")
    # the remote web server returned an error - display it to the user

    write-host "The remote server has returned an error message -`n" -ForegroundColor red
    write-host " $Results" -ForegroundColor Yellow
    write-host "`nPlease check that you are passing all the required values" -ForegroundColor Red

So there we are. We have a basic collector. As the project progresses, I'll be improving on this script, adding things such as logging and error checking. Stay tuned for Part 3!

About the Author

dwirch has posted a total of 172 articles.


Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Recent Forum Posts

Job Spammer: Sathya Narayana
dwirch posted on March 15, 2017 at about 7:18 in Spammers

Job Spammer: Manish Kumar
dwirch posted on March 14, 2017 at about 17:44 in Spammers

Site Migration Complete
dwirch posted on March 12, 2017 at about 11:10 in Site News

Site Migration Complete
dwirch posted on March 11, 2017 at about 0:35 in Site News

Job Spammer: Jason Weinstein
dwirch posted on March 10, 2017 at about 14:00 in Spammers

Job Spammer: Ryan Wilson
dwirch posted on March 8, 2017 at about 17:00 in Spammers