我想禁用一个应用程序池的空闲超时(将其设置为零),并在安装时执行此操作,是否可以通过C#或PowerShell执行此操作?
Set-ItemProperty ("IIS:\AppPools\$name") -Name processModel.idleTimeout -value ( [TimeSpan]::FromMinutes(0))
并验证值是否已更改
Get-ItemProperty ("IIS:\AppPools\$name") -Name processModel.idleTimeout.value
@R0MANARMY的答案(目前最受欢迎的)对我没用。它可以正常运行,但随后的检查显示空闲超时未改变。
根据这篇博客文章,该答案修改了对象的内存副本。我修改了R0MANARMY的答案中的示例代码:
Get-ChildItem IIS:\AppPools\$name | ForEach { $_.processModel.IdleTimeout = [TimeSpan]::FromMinutes(0); $_ | Set-Item; }
%windir%\system32\inetsrv\appcmd set config -section:applicationPools
-applicationPoolDefaults.processModel.idleTimeout:00:00:00
ServerManager类
http://msdn.microsoft.com/en-us/library/microsoft.web.administration.servermanager.aspx - Kris Ivanov$query = "Select * From IIsApplicationPoolSetting WHERE WAMUserName LIKE '%$uServer'"
$query
$pools = Get-WmiObject -Authentication 6 -ComputerName $server -Query $query -Namespace 'root/microsoftiisv2'
if ($pools)
{
foreach ($pool in $pools)
{
Write-Host(" WAM Pool: " + $pool.Name + ", " + $pool.WAMUserName + " (" + $pool.WAMUserPass + ")")
}
}
以下是与编程无关的代码片段,这里演示了如何将网站放置在新的应用程序池中。这只是一个使用Set-WMIInstance的示例。
if ($site.AppPoolID -ne $poolID)
{
# Write-Host("Updating $($site.Name) from $($site.AppPoolID) to $($poolID)")
$wmiArgs = @{"AppPoolID"=$poolID}
[void](Set-WMIInstance -InputObject $site -Arguments $wmiArgs)
} else {
# Write-Host("No update needed")
}
以下是完整的 Powershell 示例,展示如何创建一个应用程序池(用于 ASP.NET Core)并设置许多其值:
Import-Module WebAdministration
$appPoolName = "MyWebPool"
$appPoolFullName = "IIS:\AppPools\$appPoolName"
if(!(Test-Path $appPoolFullName)) {
New-WebAppPool $appPoolName -Force
Set-ItemProperty $appPoolFullName -Name managedPipelineMode -Value Integrated
Set-ItemProperty $appPoolFullName -Name managedRuntimeVersion -Value "" # means "No Managed Code"
Set-ItemProperty $appPoolFullName -Name startMode -Value AlwaysRunning
$3_days = New-TimeSpan -Days 3
Set-ItemProperty $appPoolFullName -Name processModel.idleTimeout -Value $3_days
Set-ItemProperty $appPoolFullName -Name processModel.identityType -Value NetworkService
Set-ItemProperty $appPoolFullName -Name processModel.idleTimeoutAction -Value Suspend
$zero_ts = New-TimeSpan
Set-ItemProperty $appPoolFullName -Name recycling.periodicRestart.time -Value $zero_ts
}
在使用PowerShell时,请使用以下命令:
$appPoolName = "xxxAppPool"
&"$env:windir\system32\inetsrv\appcmd" set APPPOOL $appPoolName /processModel.idleTimeout:0.00:00:00
这是我决定使用的脚本:
$myApplicationPool = Get-WmiObject -Class IISApplicationPoolSetting -Namespace "root\microsoftiisv2" | Where-Object {$_.Name -eq 'W3SVC/APPPOOLS/DefaultAppPool'}
$myApplicationPool.IdleTimeout=0
$myApplicationPool.Put()
获取应用程序池配置(供参考)
$appPoolName = 'MyAppPoolName'
$appPoolPath = '/system.applicationHost/applicationPools/Add[@name="' + $appPoolName + '"]//.'
Get-WebConfiguration $appPoolPath -PSPATH iis:
设置应用程序池空闲超时时间
$appPoolName = 'MyAppPoolName'
$appPoolPath = '/system.applicationHost/applicationPools/Add[@name="' + $appPoolName + '"]/processModel'
Set-WebConfigurationProperty $appPoolPath -Name idleTimeout -value ([TimeSpan]::FromMinutes(0)) -PSPATH iis: