You are here: Sysadmins of the North » Windows Server » Get IIS Current Connections using PowerShell

Get IIS Current Connections using PowerShell

Do you want to know how to get the current number of connections to IIS hosted websites? The 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 WMI/CIM and Win32_PerfRawData_W3SVC_WebService. Read on…

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 better flexibility to create advanced monitoring scripts.

Win32_PerfRawData_W3SVC_WebService

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\cimv2Code language: PowerShell (powershell)

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
Code language: PowerShell (powershell)
Get-CimInstance -EA SilentlyContinue -Query "select Name,CurrentConnections from Win32_PerfRawData_W3SVC_WebService" -Namespace root\cimv2 | Select-Object Name,CurrentConnectionsCode language: PowerShell (powershell)

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
Code language: PowerShell (powershell)

Get-Counter

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"Code language: PowerShell (powershell)

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).CookedvalueCode language: PowerShell (powershell)

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 using Get-Counter. Performing these actions at the right time might help you identifying the website / websites responsible for port exhaustion 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.

Show Your Support

donate with Paypal

If you want to step in to help me cover the costs for running this website, that would be awesome. Just use this link to donate a cup of coffee ☕($10 USD or €10 EUR for example). And please share the love and help others make use of this website. Thank you very much! <3 ❤️

Leave a Comment

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

Scroll to Top