简体   繁体   中英

Setting app pools to recycle at multiple specific times via powershell or appcmd does not display both times in GUI/IIS

I am attempting to set app pools to recycle at multiple times in the day in iis 8.5, I've tried using powershell and app command and when testing on a server that has no sites/applications in the pool it seems to work perfectly, however when trying to set using either method on a server that has sites in the app pools I'm seeing strange behavior, It seems to work however in the GUI of IIS if i look at the recycling settings of the app pool it only shows one of the times specified. Powershell script initially tried using is:

function Set-ApplicationPoolRecycleTimes {
    param (
        [string]$ApplicationPoolName,
        [string[]]$RestartTimes
    )
    Import-Module WebAdministration
    Write-Output "Updating recycle times for $ApplicationPoolName"
    # Delete all existing recycle times
    Clear-ItemProperty IIS:\AppPools\$ApplicationPoolName -Name Recycling.periodicRestart.schedule    
    Clear-ItemProperty IIS:\AppPools\$ApplicationPoolName -Name Recycling.periodicRestart.time
    foreach ($restartTime in $RestartTimes) {
        Write-Output "Adding recycle at $restartTime"
        # Set the application pool to restart at the time we want
        New-ItemProperty -Path "IIS:\AppPools\$ApplicationPoolName" -Name Recycling.periodicRestart.schedule -Value @{value=$restartTime}
        Set-ItemProperty -Path "IIS:\AppPools\$ApplicationPoolName" -Name Recycling.periodicRestart.time -Value "00:00:00"
    } # End foreach restarttime
} # End function Set-ApplicationPoolRecycleTimes
$apppoolname1 = "app pool's name"
$restartat = @("1:45", "18:45")
Set-ApplicationPoolRecycleTimes -ApplicationPoolName $apppoolname1 -RestartTimes $restartat

Again this seems to work perfectly unless there are sites in the application pool. When sites exist it seems to work except that the gui only shows one of the times set: 在此处输入图片说明

however querying the value show's both times:

Import-Module WebAdministration
(Get-ItemProperty ('IIS:\AppPools\app pool name') -Name Recycling.periodicRestart.schedule.collection) | select value

value   
-----   
18:45:00
01:45:00

also attempted using appcmd but finding the same results, works perfectly on a server with no sites in the app pool, but when run against servers with sites, missing one of the times in the gui, querying shows both times. I have turned logging on for app pool recycles to confirm it's happening at both times but wondering if I'm just overlooking something obvious.

appcmd script:

CD C:\windows\System32\inetsrv
$V1 = "app pool name"
#clears any existing schedule
cmd.exe /c appcmd.exe set apppool /apppool.name: $V1 /-recycling.periodicRestart.schedule
#setting desired recycles 
cmd.exe /c appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='$v1'].recycling.periodicRestart.schedule.[value='01:45:00']" /commit:apphost
cmd.exe /c appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='$v1'].recycling.periodicRestart.schedule.[value='18:45:00']" /commit:apphost

I tried your PowerShell script with the application pool which contains a site or without site.in both the condition your posted script is working.

you could try to use the below script:

    function Set-ApplicationPoolRecycleTimes {

    param (
        [string]$ApplicationPoolName,
        [string[]]$RestartTimes
    )

    Import-Module WebAdministration

    Write-Output "Updating recycle times for $ApplicationPoolName"

    # Delete all existing recycle times
    Clear-ItemProperty IIS:\AppPools\$ApplicationPoolName -Name Recycling.periodicRestart.schedule

    foreach ($restartTime in $RestartTimes) {

        Write-Output "Adding recycle at $restartTime"
        # Set the application pool to restart at the time we want
        New-ItemProperty -Path "IIS:\AppPools\$ApplicationPoolName" -Name Recycling.periodicRestart.schedule -Value @{value=$restartTime}

    } # End foreach restarttime

} # End function Set-ApplicationPoolRecycleTimes
$apppoolname = "abcsite"
$restartat = @("05:55", "12:55", "17:00")

Set-ApplicationPoolRecycleTimes -ApplicationPoolName $apppoolname -RestartTimes $restartat

在此处输入图片说明

or

appcmd.exe set config  -section:system.applicationHost/applicationPools /+"[name='test1'].recycling.periodicRestart.schedule.[value='07:00:00']" /commit:apphost

    appcmd.exe set config  -section:system.applicationHost/applicationPools /+"[name='test1'].recycling.periodicRestart.schedule.[value='18:25:00']" /commit:apphost

or

 Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/applicationPools/add[@name='test1']/recycling/periodicRestart/schedule" -name "." -value @{value='07:00:00'}

Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/applicationPools/add[@name='test1']/recycling/periodicRestart/schedule" -name "." -value @{value='18:25:00'}

The above scripts are tested with the IIS 10 (Windows 10) and IIS 8.5(windows server 2012r2)

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