5

#PSTip Get requests that are currently executing in a worker process

Note: This tip requires PowerShell 2.0 or above.

IIS lets you view all worker processes of a server when you double click the Worker Process tile in IIS manager (inetmgr.exe).

wp1 wp2

If you double click a process you will get the requests that are currently running inside that worker process. The information gives you a good view of the current requests but this is only good for the server you’re currently working. Using PowerShell you can get the information from multiple servers and view all requests in one view.

Using WMI we can get the worker processes of a server by getting the instances of the WorkerProcess class.

PS> Get-WmiObject WorkerProcess -Namespace root\WebAdministration -ComputerName IIS1

__GENUS          : 2
__CLASS          : WorkerProcess
__SUPERCLASS     : Object
__DYNASTY        : Object
__RELPATH        : WorkerProcess.ProcessId=4252
__PROPERTY_COUNT : 3
__DERIVATION     : {Object}
__SERVER         : IIS1
__NAMESPACE      : root\WebAdministration
__PATH           : \\IIS1\root\WebAdministration:WorkerProcess.ProcessId=4252
AppPoolName      : SharePoint - mysite
Guid             : 0fb641ca-b913-4628-99ff-6ffd13ee4b52
ProcessId        : 4252
PSComputerName   : IIS1

__GENUS          : 2
__CLASS          : WorkerProcess
__SUPERCLASS     : Object
__DYNASTY        : Object
__RELPATH        : WorkerProcess.ProcessId=8512
__PROPERTY_COUNT : 3
__DERIVATION     : {Object}
__SERVER         : IIS1
__NAMESPACE      : root\WebAdministration
__PATH           : \\IIS1\root\WebAdministration:WorkerProcess.ProcessId=8512
AppPoolName      : DefaultAppPool
Guid             : 0237506e-4924-45a8-b334-284202d83d8d
ProcessId        : 8512
PSComputerName   : IIS1

If you pipe the result to the Get-Member cmdlet you will find a method called GetExecutingRequests.

PS> Get-WmiObject WorkerProcess -Namespace root\WebAdministration -ComputerName IIS1

   TypeName: System.Management.ManagementObject#root\WebAdministration\WorkerProcess
Name                 MemberType    Definition
----                 ----------    ----------
PSComputerName       AliasProperty PSComputerName = __SERVER
GetExecutingRequests Method        System.Management.ManagementBaseObject GetExecutingRequests()
GetState             Method        System.Management.ManagementBaseObject GetState()
AppPoolName          Property      string AppPoolName {get;set;}
Guid                 Property      string Guid {get;set;}
(...)

The GetExecutingRequests method lets you see the requests that were executing at the time that the method was run. Requests execute fast so you might get empty results when you execute the method. To restrict the result to a specific pool name, use the -Filter parameter.

Get-WmiObject WorkerProcess -Namespace root\WebAdministration -ComputerName IIS1 -Filter "AppPoolName='SharePoint - mysite'" |
Invoke-WmiMethod -Name GetExecutingRequests

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     :
__DYNASTY        : __PARAMETERS
__RELPATH        :
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
OutputElement    : {}
PSComputerName   :

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     :
__DYNASTY        : __PARAMETERS
__RELPATH        :
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
OutputElement    : {mysite, mysite, mysite}
PSComputerName   :

The resultant objects you want to take a look at are the ones that have a value in the OutputElement property. The following is the result of the above highlighted OutputElement property:

__GENUS          : 2
__CLASS          : HttpRequest
__SUPERCLASS     : Object
__DYNASTY        : Object
__RELPATH        :
__PROPERTY_COUNT : 14
__DERIVATION     : {Object}
__SERVER         :
__NAMESPACE      :
__PATH           :
ClientIPAddress  : 10.10.10.10
ConnectionId     : cf0000006001800f
CurrentModule    : ManagedPipelineHandler
GUID             :
HostName         : mysite
LocalIPAddress   : 11.11.11.11
LocalPort        : 80
PipelineState    : 128
SiteId           : 1202063871
TimeElapsed      : 1544
TimeInModule     : 1529
TimeInState      : 1529
Url              : /Pages/Default.aspx
Verb             : GET
PSComputerName   :
(...)

Using the following snippet you can create a custom object that captures the server name, application pool name and ID, and all relevant properties of the OutputElement object.

$wp = Get-WmiObject WorkerProcess -Namespace root\WebAdministration -ComputerName IIS1,IIS2,IIS3

foreach($w in $wp)
{
    $w | Invoke-WmiMethod -Name GetExecutingRequests |
    Select-Object -ExpandProperty OutputElement | Foreach-Object{
        [PSCustomObject]@{
            AppPoolName     = $w.AppPoolName
            ProcessId       = $w.ProcessId
            ClientIPAddress = $_.ClientIPAddress
            CurrentModule   = $_.CurrentModule
            HostName        = $_.HostName
            LocalIPAddress  = $_.LocalIPAddress
            LocalPort       = $_.LocalPort
            SiteId          = $_.SiteId
            Url             = $_.Url
            Verb            = $_.Verb
            PSComputerName  = $w.__SERVER
        }
    }
}

AppPoolName     : SharePoint - mysite
ProcessId       : 5080
ClientIPAddress : 10.10.10.10
CurrentModule   : IIS Web Core
HostName        : mysite
LocalIPAddress  : 11.11.11.11
LocalPort       : 80
SiteId          : 1202063871
Url             : /Pages/_layouts/IMAGES/MasterPages/Dropdown.png
Verb            : GET
PSComputerName  : IIS1
Filed in: Columns, Tips and Tricks Tags: , , , ,

5 Responses to "#PSTip Get requests that are currently executing in a worker process"

  1. Greg Bray says:

    Great post! Just a few notes:

    1. I had to add “-Authentication PacketPrivacy -Impersonation Impersonate” to the Get-WmiObject call to work because without it I was getting an Access Denied error message.

    2. For Server 2008 R2 this requires that the ‘IIS Management Scripts and Tools’ feature (aka Web-Scripting-Tools) is installed, otherwise you will get an error stating “Get-WmiObject : Invalid namespace”. You can fix this in an Administrative PowerShell prompt using:

    ipmo ServerManager
    get-windowsfeature web* #Optional, shows currently installed features
    add-windowsfeature Web-Scripting-Tools

    3. Also this only shows active requests that are still being processed by IIS. These requests usually only stay active for a short amount of time so you may not see anything if you have a low amount of traffic unless you run it multiple times

    • ShayLevy says:

      Thanks for the comments Greg! I should have mention that the Web-Scripting-Tools feature is needed on 2008 R2 as I did bump into this when testing.

      Thank you for bringing -Authentication to my attention. I didn’t include it because my tests I didn’t needed to :), I shouldn’t have assume that everybody have full rights.

  2. Jason says:

    While the method is listed, I get invalid method when attempting to execute. Any thoughts? Common problem?

  3. Stephen says:

    We enabled SSL Off Loading on the F5 for a corporate application so we can get the original IP address in the logs, but we would really like to be able to display that address and the network user name (we use Windows Authentication) for active requests. Is there a way to get that information added to this?

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: