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

前端 未结 8 535
别那么骄傲
别那么骄傲 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:05

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

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

    When using powershell use the following:

    $appPoolName = "xxxAppPool"
    &"$env:windir\system32\inetsrv\appcmd" set APPPOOL $appPoolName /processModel.idleTimeout:0.00:00:00
    
    0 讨论(0)
  • 2021-02-05 09:10

    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.

    0 讨论(0)
  • 2021-02-05 09:16

    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.

    0 讨论(0)
  • 2021-02-05 09:17
    %windir%\system32\inetsrv\appcmd set config -section:applicationPools
       -applicationPoolDefaults.processModel.idleTimeout:00:00:00
    
    0 讨论(0)
提交回复
热议问题