如何用appcmd.exe清除应用池定期回收计划或跳过重复配置?
解决IIS应用池定期回收计划重复配置问题
一、清除所有定期回收计划
要清空指定应用池的全部定期回收计划,可使用appcmd.exe的clear命令直接重置schedule集合:
appcmd set config -section:system.applicationHost/applicationPools /"[name='AppPoolName'].recycling.periodicRestart.schedule.clear()" /commit:apphost
执行后该应用池下所有已配置的回收计划都会被移除,之后再执行你的添加计划命令就不会因重复项报错。
二、跳过重复配置的解决方案
如果不想清除现有计划,仅需避免重复添加同一时间的计划,可通过先检查再执行的逻辑实现:
批处理脚本实现
# 导出目标应用池的回收计划配置到临时文件 appcmd list config -section:system.applicationHost/applicationPools /"[name='AppPoolName'].recycling.periodicRestart.schedule" /xml > temp_config.xml # 检查目标时间是否已存在,不存在则执行添加命令 findstr /C:"value=\"19:05:00\"" temp_config.xml > nul if %errorlevel% equ 1 ( appcmd set config -section:system.applicationHost/applicationPools /+"[name='AppPoolName'].recycling.periodicRestart.schedule.[value='19:05:00']" /commit:apphost ) # 删除临时文件 del temp_config.xml
PowerShell实现(更简洁)
$appPool = Get-IISAppPool -Name "AppPoolName" $targetSchedule = $appPool.Recycling.PeriodicRestart.Schedule | Where-Object { $_.Value -eq [TimeSpan]"19:05:00" } if (-not $targetSchedule) { $newSchedule = New-Object Microsoft.Web.Administration.ScheduleElement $newSchedule.Value = [TimeSpan]"19:05:00" $appPool.Recycling.PeriodicRestart.Schedule.Add($newSchedule) $appPool.CommitChanges() }
内容的提问来源于stack exchange,提问作者J.Memisevic
相关产品推荐
相关产品推荐

