How to quickly estimate the current number of user connections (sessions) to the IIS sites on webserver running on Windows Server? Such information will allow to determine and predict the load on the server, choose the best time for the maintenance and updates of the website, predict the IIS server’s load when the number of users increases.
The easiest way to determine the number of active user sessions on the IIS Web site is to use the performance counters in Windows Performance Monitor.
Open the Performance Monitor console by running the perfmon command and go the Performance monitor section (Monitoring Tools — > Performance Monitor).
Then you need to add the necessary counters to the monitor window (by default, the total CPU usage counter is displayed, but you can remove it). To add a new counter, click the green button on the toolbar (you can see it on the screenshot) or press Ctrl+N on the keyboard.
In the list of available counters, find and expand the Web Service group. In this category, we are interested in three counters:
- Current Anonymous Users – the number of anonymous IIS users;
- Current Non-Anonymous Users – the number of authorized IIS users;
- Current Connections – total number of active connections on the IIS server.
Select the desired counter and in the Instances of selected objects choose one or more IIS websites for which you want to display connection information. The information about users of all websites on the server is stored in the _Total instance. Now you only have to click the Add >> button to move the counter to the list of the counters to be added in the right pane.
Add all the necessary counters in the same way and click OK.
Now the information about the number of user sessions in the Performance Manager console is being displayed in the real time (by default, the counter values are displayed as linear graphs). If you select any of the counters in the bottom pane, you can view its last, average, minimum or maximum value for a given period of time.
You can add custom performance counters to this console and save them in a separate view, which can be used later to quickly access the web server load data.
You can access the IIS performance counters from PowerShell. To do this, you can use the Get-Counter cmdlet. The list of all available Web Service performance counters can be displayed as follows:
(Get-Counter -ListSet 'Web Service').counter
To get information about the current number of active connections on the IIS server (the counter \Web Service(*)\Current Connections), use this command:
Get-Counter -Counter “\Web Service(*)\Current Connections”
As you can see, this command returned both the total number of connections to the IIS server and the statistics for each of the sites.
- The values of several counters can be displayed if you specify them separated by the commas;
- With the –Continuous option, the information about the value of the counter is constantly displayed in the console until you’ll interrupt it using CTRL+C.
You can get the number of active sessions for a specific IIS site. For example, to get the current number of connections on a site named Site1, run the following command:
Get-Counter "web service(Site1)\current connections" -ComputerName web-srv01
You can specify the name of the server on which the counter value is checked. When you are checking the number of connections on the site locally, specifying localhost is not allowed:
In order not to specify the server name each time, you can use the environment variable COMPUTERNAME:
Get-Counter "web service(Site1)\current connections" -ComputerName $env:COMPUTERNAME
To get the numeric value of the counter “current connections” of the entire IIS web server (total users on IIS), you can use this command:
((Get-Counter -Counter 'web service(_total)\current connections' -computer $env:COMPUTERNAME) | Select-Object -Expand countersamples).Cookedvalue
Let’s try using a simple script to create several additional sessions with our site and check the counter value. You can increase the number of connections to the IIS website using the Invoke-WebRequest cmdlet, or you can simply open several windows in the browser:
$counter = 20
for($i=1;$i -le $counter;$i++){
$SiteAdress = "http://localhost:9666/"
Start-Process $SiteAdress
}
Check the value of the current connections counter and make sure that it increases.
If several IIS sites are running on the server, and you need to get the number of connections to each of them in a form of table, you can use this script (to receive data from IIS into PowerShell, you need to load the WebAdministration module):
import-module webadministration
function get-CurrentConnection($Site) {
Get-Counter "web service($Site)\current connections,web service($Site)\ Bytes Received/sec,web service($Site)\Bytes Sent/sec" -ComputerName $env:COMPUTERNAME
}
$IISsites = dir IIS:\Sites | Select Name
$CurrentConnection = @()
foreach ($site in $IISsites)
{
Write-Host $site
$ConnCount = New-Object psobject | get-CurrentConnection -Site $site.name
$CurrentConnection += $ConnCount
}
$CurrentConnection|out-gridview
You can also display the numerical values of connection counters for all sites (the first value is the total number of connections to IIS):
Get-wmiObject -class Win32_PerfRawData_W3SVC_WebService | select-object -expand currentconnections
You can display information about the amount of received/sent data for each site or the entire web server using the Web service(sitename)\Bytes Received/sec
and Web service(sitename)\Bytes Sent/sec
counters.
So, we looked at a way to get information about the load on sites running on the IIS web server.
4 comments
Hello
Error in powershell ise as admin:
PS C:\WINDOWS\system32> import-module webadministration
function get-CurrentConnection($Site) {
Get-Counter “web service($Site)\current connections,web service($Site)\ Bytes Received/sec,web service($Site)\Bytes Sent/sec” -ComputerName $env:SCP-Portatil
}
$IISsites = dir IIS:\Sites | Select Name
$CurrentConnection = @()
foreach ($site in $IISsites)
{
Write-Host $site
$ConnCount = New-Object psobject | get-CurrentConnection -Site $site.name
$CurrentConnection += $ConnCount
}
$CurrentConnection|out-gridview
@{name=Default Web Site}
Get-Counter : Unable to connect to the specified computer or the computer is offline.
At line:3 char:1
+ Get-Counter “web service($Site)\current connections,web service($Site …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidResult: (:) [Get-Counter], Exception
+ FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand
If a leave without machine name error say:
@{name=naveotest.istinfor.com}
Get-Counter : The specified counter path could not be interpreted.
At C:\Ageinfo\iisConnections.ps1:3 char:2
+ Get-Counter “web service($Site)\current connections,web service($Sit …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidResult: (:) [Get-Counter], Exception
+ FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand
Can we find out each user details how many are logged in. THe question i asked is because there might be chance that a single user can open two – three sessions.
Is there a way to see the current connections per application? I have the main site with 12 apps and want to know which app is hitting more connections, with this I can only get the grand total for the site.