Monitoring IIS with PowerShell

While this article doesn’t apply directly for SharePoint admins, it is targeting any users who are in charge of maintaining an IIS server and who wish to use PowerShell to monitor their servers. The processed associated with the various application pools defined in IIS ​are called “w3wp”. For the scope of this example, we will show you how you can use PowerShell to monitor the CPU usage for these w3wp processes on a given server.

Let us start off by looking at what is available to us in terms of PowerShell cmdlets to obtain information about a specific process. The Get-Process cmdlet returns a full list of all processes currently running on a server. The figure below shows the execution of this cmdlet on my local server:

20150417-01.png

We can see from the screen above that I have 3 w3wp processes listed. Let us now use a select statement on that list of processes to obtain a reference only to these 3 processes. The line of PowerShell code required to achieve this will look like the following:

Get-Process | Where{$_.ProcessName -eq “w3wp”}

Executing this line of PowerShell will return a result similar to the following:

20150417-02.png

Now what we need to do is to declare a custom expression in order to allow us to obtain the CPU % used by each of our processes. By default, the Process object returned by the Get-Process cmdlet doesn’t expose a CPU percentage property. Therefore we have to declare it manually. This can be done with the following lines of PowerShell:

$CPU = @{
Name = ‘CPU ‘
Expression = {
$TotalSec = (New-TimeSpan -Start $_.StartTime).TotalSeconds
[Math]::Round( ($_.CPU * 100 / $TotalSec), 2)
}
}

With the above declaration, we are defining a new variable named $CPU, that represents total % of CPU consume by a specific process. This percentage value is obtained by calculating the time spent since the process was first initiated, and dividing it by the CPU load. I know you guys are probably already lost by now, so let’s dig into a step by step explanation. The first line of the Expression is the following:

$TotalSec = (New-TimeSpan -Start $_.StartTime).TotalSeconds

Let us start by dissecting this command into two major parts. The first one being the $_.StartTime portion. In our context the operator $_ represents the current process. So let’s execute the following lines of Powershell and see their expected outcome, where $processes[0] represents the first w3wp process out of the three:

$processes  = Get-Process | Where{$_.ProcessName -eq “w3wp”}

$processes[0].StartTime

20150417-03.png

* es my server is in french “Avril” is French for “April”

This gives us the time at which the process was first started. then the second part of the script simply returns the number of seconds that went by since the process was first started (in our case 11:28:56) up until now. Executing the entire line returns the following:

(New-TimeSpan -Start $processes[0].StartTime).TotalSeconds

20150417-04.png

Let us now move on to the second line of the Expression declaration. In summary, what this line does is round up (using 2 decimal digits) the result of the total time in second our process used CPU and divides it by the overall time spent running the process in seconds. The result should give us an average of how much CPU load our process has been using on the server.

 

Now that we understand a bit more how to obtain the current CPU % of a process, let us look at what the complete script will look like:

$CPU = @{
Name = ‘CPU ‘
Expression = {
$TotalSec = (New-TimeSpan -Start $_.StartTime).TotalSeconds
[Math]::Round( ($_.CPU * 100 / $TotalSec), 2)
}
}

$processes  = Get-Process | Where{$_.ProcessName -eq “w3wp”}

foreach($process in $processes)

{

$process | SELECT Name, $CPU

}

20150417-05.png

Microsoft Premier Field Engineer – SharePoint

Leave a Comment

Your email address will not be published. Required fields are marked *

*
*