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

前端 未结 8 501
别那么骄傲
别那么骄傲 2021-02-05 08:29

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?

相关标签:
8条回答
  • 2021-02-05 09:17

    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
    }
    
    0 讨论(0)
  • 2021-02-05 09:18

    @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; }
    
    0 讨论(0)
提交回复
热议问题