20

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?

Seymour
  • 7,043
  • 12
  • 44
  • 51
Bongo Sharp
  • 9,450
  • 8
  • 25
  • 35

8 Answers8

27

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
Roman
  • 19,581
  • 6
  • 68
  • 84
  • 1
    This answer didn't work for me. I've added a version that works for me: http://stackoverflow.com/a/41111237/260303 – Tony S Yu Dec 12 '16 at 23:13
  • Works great for me but you need to make sure "WebAdministration" is installed and loaded, I found [this answer](https://stackoverflow.com/a/24393229/2912011) useful in getting this to work. – David Rogers Jul 16 '18 at 20:13
  • It worked for me as follows: $ts = New-TimeSpan -Seconds 0 Set-ItemProperty ("IIS:\AppPools\$name") -Name processModel.idleTimeout -Value $ts – katrash Jun 14 '19 at 20:43
7

@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; }
Community
  • 1
  • 1
Tony S Yu
  • 3,003
  • 30
  • 40
4
%windir%\system32\inetsrv\appcmd set config -section:applicationPools
   -applicationPoolDefaults.processModel.idleTimeout:00:00:00
Kris Ivanov
  • 10,476
  • 1
  • 24
  • 35
  • Alternatively you can use the `ServerManager Class` in your code http://msdn.microsoft.com/en-us/library/microsoft.web.administration.servermanager.aspx – Kris Ivanov Apr 27 '11 at 22:37
  • I was doing a quick proof of concept for both solutions you provided, but they don't seem to work on Window Server 2003 as ServerManager works on iis 7 and appcmd only exist on more recent versions of IIS – Bongo Sharp May 03 '11 at 17:20
1

When using powershell use the following:

$appPoolName = "xxxAppPool"
&"$env:windir\system32\inetsrv\appcmd" set APPPOOL $appPoolName /processModel.idleTimeout:0.00:00:00
xx1xx
  • 1,834
  • 17
  • 16
1

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
}
katrash
  • 1,065
  • 12
  • 13
1

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.

codepoke
  • 1,272
  • 1
  • 22
  • 40
0

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:
Seymour
  • 7,043
  • 12
  • 44
  • 51
  • -Name idleTimeout gives me an error (Propert idleTimeout is not found on ). The right name is processModel.idleTimeout. – Kappacake May 24 '19 at 12:27
0

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.

Bongo Sharp
  • 9,450
  • 8
  • 25
  • 35