Get IIS Current Connections using PowerShell

Use Get-Counter in PowerShell to get the current number of connections to IIS hosted websites. This information is stored in Windows Server Performance Counters, and you can get it using `Get-Counter` cmdlet in PowerShell of course. But in post I'll show you a different - and perhaps even faster - method using CIM and Win32_PerfRawData_W3SVC_WebService. Read on...
Published on Saturday, 12 August 2023

Official Zabbix Logo

For compatibility with PowerShell 7 (pwsh), you'll use the Get-CimInstance cmdlet and not Get-WmiObject. This is because Get-WmiObject is not compatible with PowerShell 7. Whether or not Win32_PerfRawData_W3SVC_WebService is faster than Get-Counter, it gives you more flexibility to create advanced monitoring scripts.

See Monitor website performance in IIS with Zabbix for a more complete solution in Zabbix

Query Win32_PerfRawData_W3SVC_WebService in PowerShell

The Web Service object includes counters specific to the World Wide Web Publishing Service and is a subclass of Win32_PerfRawData. You may remember Win32_PerfRawData from my Zabbix posts, where I use it to monitor various aspects of IIS websites and application pools.

You find more information about Win32_PerfRawData_W3SVC_WebService and its properties on wutils.com.

Knowing the property you want to query, CurrentConnections, it's easy to create a WQL or WMI query:

Get-CimInstance -EA SilentlyContinue `
  -Query "select * from Win32_PerfRawData_W3SVC_WebService" `
  -Namespace root\cimv2

This queries Win32_PerfRawData_W3SVC_WebService for all properties (counters) in all websites. If that is a bit too much for your taking, you can add filters. You either use Select-Object to select the objects you want, or you only query the the property you want (and select them to avoid empty values):

Get-CimInstance -EA SilentlyContinue `
  -Query "select * from Win32_PerfRawData_W3SVC_WebService" `
  -Namespace root\cimv2 `
  | Select-Object Name,CurrentConnections
Get-CimInstance -EA SilentlyContinue `
  -Query "select Name,CurrentConnections from Win32_PerfRawData_W3SVC_WebService" `
  -Namespace root\cimv2 `
  | Select-Object Name,CurrentConnections

Use Where-Object as a WHERE clause to select the rows that meet certain conditions, for example list all sites having more than 70 CurrentConnections:

Get-CimInstance -EA SilentlyContinue `
  -Query "select * from Win32_PerfRawData_W3SVC_WebService" `
  -Namespace root\cimv2 `
  | Where-Object { $_.CurrentConnections -ge "70" } `
  | Select-Object Name, CurrentConnections

You can even filter on these results further using Sort-Object for example:

Get-CimInstance -EA SilentlyContinue `
  -Query "select * from Win32_PerfRawData_W3SVC_WebService" `
  -Namespace root\cimv2 `
  | Select Name,CurrentConnections `
  | Sort-Object CurrentConnections -Descending
Get-CimInstance -EA SilentlyContinue `
  -Query "select * from Win32_PerfRawData_W3SVC_WebService" `
  -Namespace root\cimv2 `
  | Where-Object { $_.CurrentConnections -ge "70" } `
  | Select-Object Name, CurrentConnections `
  | Sort-Object CurrentConnections -Descending

Get-Counter cmdlet in PowerShell

Oh, you want to use the Get-Counter cmdlet to query IIS for the total amount of connections? Okay, here's how:

Get-Counter "\Web Service(website.Name)\Current Connections"

Substitute website.Name with the name of the website you want to query the total number of connections for, or use * for all sites ("\Web Service(*)\Current Connections"). To get the number of active connections on your IIS server, use this:

( `
  ( `
    Get-Counter -Counter \"Web Service(_total)\current connections" `
    -computer $env:COMPUTERNAME `
  ) `
  | Select-Object -Expand countersamples `
).Cookedvalue

Conclusion getting current connections to IIS

In this post I showed you various ways of getting the number of current, active, connections to IIS using PowerShell. Either by WQL querying Win32_PerfRawData_W3SVC_WebService or Get-Counter. Performing these actions at the right time might help you identifying a website responsible for TCP/IP port exhaustion, application pool hangs and can help you tune Windows Server and IIS' TCP/IP stack.

Whether or not Win32_PerfRawData_W3SVC_WebService is faster than Get-Counter, it does give you the flexibility to create advanced PowerShell scripts for your Zabbix monitoring templates.