/build/static/layout/Breadcrumb_cap_w.png

Meter Software Using Windows PowerShell

Video Transcript

Hi, this is Greg Shields and I am going to show you how you can use Windows PowerShell and some creative coding to actually help you meter the application use on a system. You will see here that I have a copy of Microsoft Word that is running and also a copy of Windows PowerShell. Now here in this copy of Windows PowerShell I can get some very interesting information about the use of Microsoft Word.

In the case of Windows PowerShell, Windows PowerShell is not interested in Word as an application, what it is actually interested in is Word as a process. I can use the Get-Process cmdlet to see all the different processes on the system. This does not do me a lot of good because what I am interested in is not the amount of memory or the amount of processing power those processes are using. Instead, more information about when that application was actually instantiated.

Let me go ahead and trim this down just a little bit. What I want to take a look at is not just Get-Process, but I want to Get-Process where the object is named winword. So, I'm just focusing this on the Microsoft Word process. Once I have done that, what I want to do is select out the name of that process and then one of the actual perimeters that are not brought up in the default Get-Process result. That is called StartTime. You will see if I run this command I see winword under the name and the actual start time when winword was actually began. This tells me when winword was started, or when it was double clicked, or essentially when it was instantiated here on this system. What is interesting is that this is a fairly long way in which I can actually result this information. I am going to show you a little bit of a short hand here that could be a little bit more useful for you.

You will notice that I am using this dotted notation here and I am doing Get-Process winword.' I am resolving this first and ultimately using dotted notation to pull out the starttime for winword. You will see the results are relatively similar but a little bit different formatting. Here I see Monday, March 28, 2011 at 4:58:24. Now, what I want to do is I want to Get-Process for winword and show the start time. But knowing what the starttime was is only useful for me if I can actually subtract that starttime from the current time. The way that I can do that is actually by using the command Get-Date.

The Get-Date command actually shows me what is today's time and date. What I want to do is use the Get-Date command with the Get-Process command and I want to run Get-Date and use dotted notation to subtract the current date from the results of that starttime query. So what I am doing is effectively subtracting the current date and time from the starttime for the winword process. Let me go ahead and run this. You will see here that the winword process had been running for about twelve minutes right now, so this gives me some interesting information about how long the user has been using the winword process.

Now this is only useful if all I am looking for is Microsoft Word, but I may be looking for all the different processes on a system.' Because if I am trying to collect usage information and metering information I want to do that for any application that may be running. There are a lot of ways I can do that. One of the ways I want to show you. I am going to type out this command because it is rather complex is by using a foreach loop. You will see if I use a foreach loop and I create a $Proc in Get-Process. What I am doing with this first command is, I am saying, for each process in the results of the Get-Process command. Then I want to do something. What I want to do is create another variable here called $ElapsedTime and I want to correspond that $ElapsedTime variable to the command that I just ran. Get-Date.Subtract($Proc) which in this case is the process resulting from the Get-Process cmdlet and through each iteration of starttime' Then, any time that occurs I want to write out, so Write-Host $Proc.name, so the name of that process, and $ElapsedTime.Minutes.' Just the minutes that are associated with each process.' If I run this (and close out that foreach loop there) you can see the processes that are running on the system as well as also the number of minutes that each one of these processes has been running. You will see here that winword has been running for fourteen minutes currently.

So this is a mechanism by which I can determine how long each process has been running on the system. With a little bit of extra wrapper code around this and also some other information that collects all these results and puts them into a report, I can potentially use PowerShell to create an application metering solution. Now as you can imagine doing so is going to require quite a bit more code. And so you may want to use PowerShell or you may or you may want to look at some alternate third party solutions that do all this for you.


Comments

This post is locked
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ