简体   繁体   中英

in C#/Powershell - Is it possible to change the Idle TimeOut for an IIS Application Pool?

I want to disable the idle timeout (Set it to Zero) of an application pool and I want to perform this at setup time, is it possible to perform this action from C# or PowerShell?

If you are using PowerShell 2 or later, you should have access to Set-ItemProperty . You'll also want to load the WebAdministration module.

You can then do (example taken from here )

Set-ItemProperty ("IIS:\AppPools\$name") -Name processModel.idleTimeout -value ( [TimeSpan]::FromMinutes(0))

and verify that the value was changed with

Get-ItemProperty ("IIS:\AppPools\$name") -Name processModel.idleTimeout.value

@R0MANARMY's answer (currently the most popular) didn't work for me. It runs fine, but the subsequent check shows that the idle timeout is unchanged.

Based on this blog post , that answer modifies an in-memory copy of the object. I modified the sample code in R0MANARMY's answer as:

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

I use the following function to generically grab an app pool object:

$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 + ")")
    }
}

And from an unrelated piece of code, here's where I place a site in a new App Pool. It's just an example how to use 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")
}

Use Get-Member to learn what properties your $pool has, then use Set-WMIInstance to modify them.

When using powershell use the following:

$appPoolName = "xxxAppPool"
&"$env:windir\system32\inetsrv\appcmd" set APPPOOL $appPoolName /processModel.idleTimeout:0.00:00:00

Get Application Pool Configuration (For Reference)

$appPoolName = 'MyAppPoolName'
$appPoolPath = '/system.applicationHost/applicationPools/Add[@name="' + $appPoolName + '"]//.'
Get-WebConfiguration $appPoolPath -PSPATH iis:

Set Application Pool Idle Timeout

$appPoolName = 'MyAppPoolName'
$appPoolPath = '/system.applicationHost/applicationPools/Add[@name="' + $appPoolName + '"]/processModel'
Set-WebConfigurationProperty $appPoolPath -Name idleTimeout -value ([TimeSpan]::FromMinutes(0)) -PSPATH iis:

Here is a complete Powershell sample showing how to create an application pool (for ASP.NET Core) and set many of its values:

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
}

This is the script that i decided to use:

$myApplicationPool = Get-WmiObject -Class IISApplicationPoolSetting -Namespace "root\microsoftiisv2" | Where-Object {$_.Name -eq 'W3SVC/APPPOOLS/DefaultAppPool'}
$myApplicationPool.IdleTimeout=0
$myApplicationPool.Put()

If someone else has a better approach for this please let me know.

Hope this help someone

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM