开发者

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

开发者 https://www.devze.com 2023-02-28 23:45 出处:网络
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 a

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.


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.


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:
0

精彩评论

暂无评论...
验证码 换一张
取 消