Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get running session count from IIS for my hosted Asp.Net web site

I am hosting an Asp.Net website in IIS 6.0

We have to reset the session timeout in web.config

My client want me to reset it only if no session is running (no one is using the site).

We have not used Membership and SessionState is set to InProc

How I get to know if anybody using the site or any session is running.

I can't make change in source code or any other file except web.config in the hosted website.

like image 862
Imran Rizvi Avatar asked Mar 16 '12 09:03

Imran Rizvi


2 Answers

I'm not great at PowerShell, so hopefully you can look up the proper syntax. But ...

One option is to run a Powershell script and check the count of the session like this:

UPDATE: Changed 'Sessions Total' to 'Sessions Active'

write-host Getting performance counters ...

$perfCounterString = "\asp.net applications(__total__)\sessions active" 
$counter = get-counter -counter $perfCounterString 
$rawValue = $counter.CounterSamples[0].CookedValue 

write-host Session Count is $rawValue

if( $rawValue -gt 0)
{
   write-host Session are active - will not stop IIS
   exit
}

write-host Stopping IIS
stop-service "IISAdmin"

# Set values 
$webConfig = "d:\web.config"
$newTimeout = "20"

# Open file and change value
$doc = new-object System.Xml.XmlDocument
$doc.Load($webConfig)
$doc.SelectSingleNode("//sessionState").timeout = $newTimeout
$doc.Save($webConfig)

write-host Starting IIS
start-service "IISAdmin"
write-host Done!

Save this on the desktop as "ChangeIIS.ps1".

Now Powershell doesn't like you just running scripts like .bat files. You have to grant access to the process or your user for security reasons. So do the following:

  1. Open a command prompt and Run As Administrator
  2. Type powershell
  3. Type Set-ExecutionPolicy -scope process Bypass
  4. Type sl <path to directory of .sp1 file> . sl [set-location] is like cd in command prompt
  5. Type $ '.\ChangeIIS.ps1'

It will run now and reset the value.

Here is a link to my blog on how I created the PowerShell script in a more Step-by-step fashion

like image 187
Dominic Zukiewicz Avatar answered Oct 04 '22 08:10

Dominic Zukiewicz


Check the IIS log files for your site and see when the last hit was made (by actual users, rather than search bots).

If the last hit was older than the current session timeout, then no active sessions exist. That is what you are actually looking for, rather than a count.

No programming or hacks required.

like image 29
Neil Moss Avatar answered Oct 04 '22 09:10

Neil Moss