从我托管的Asp.Net网站获取IIS的运行会话数

Imr*_*zvi 7 c# asp.net iis hosting

我在IIS 6.0中托管Asp.Net网站

我们必须在web.config中重置会话超时

我的客户希望我只有在没有会话正在运行时才重置它(没有人使用该站点).

我们没有使用Membership,SessionState设置为InProc

我如何知道是否有人使用该网站或任何会话正在运行.

我无法在托管网站中的源代码或除web.config之外的任何其他文件中进行更改.

Dom*_*icz 3

我不太擅长 PowerShell,所以希望您能查找正确的语法。但 ...

一种选择是运行 Powershell 脚本并检查会话计数,如下所示:

更新:将“会话总数”更改为“活动会话”

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!
Run Code Online (Sandbox Code Playgroud)

将其保存在桌面上,名称为“ChangeIIS.ps1”。

现在,Powershell 不喜欢您只运行 .bat 文件等脚本。出于安全原因,您必须授予对进程或您的用户的访问权限。所以请执行以下操作:

  1. 打开命令提示符并以管理员身份运行
  2. 类型powershell
  3. 类型Set-ExecutionPolicy -scope process Bypass
  4. 类型sl <path to directory of .sp1 file>sl[set-location] 就像cd命令提示符中一样
  5. 类型$ '.\ChangeIIS.ps1'

它将立即运行并重置该值。

这是我的博客的链接,介绍了我如何 以更分步的方式创建 PowerShell 脚本