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.
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:
powershell
Set-ExecutionPolicy -scope process Bypass
sl <path to directory of .sp1 file>
. sl
[set-location] is like cd
in command prompt$ '.\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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With