如何用PowerShell提取IIS应用池仅回收时间(多服务器场景)
提取IIS应用池定时回收时间的PowerShell解决方案
问题背景
执行Get-ItemProperty -Path IIS:\AppPools\DefaultAppPool -Name recycling.periodicRestart.schedule.collection会返回包含完整结构的对象,仅需提取其中的时间值(如04:14:00),并批量获取数百台服务器上指定应用池的服务器名、应用池名、回收时间。
单应用池时间提取方法
方法1:基于WebAdministration模块
recycling.periodicRestart.schedule.collection返回的是集合对象,直接访问其value属性即可提取时间:
$apppool = "DefaultAppPool" $recycletime = (Get-ItemProperty -Path IIS:\AppPools\$apppool -Name recycling.periodicRestart.schedule.collection).value
若存在多个定时回收时间,结果会是数组,可使用-join ", "拼接成字符串。
方法2:基于Get-IISAppPool(跨服务器场景友好)
Get-IISAppPool返回的应用池对象中,Recycling.PeriodicRestart.Schedule对应定时回收规则集合,提取Value属性即可:
$apppool = "DefaultAppPool" $recycletime = (Get-IISAppPool $apppool).Recycling.PeriodicRestart.Schedule.Value
多服务器批量采集脚本
结合Invoke-Command实现跨服务器批量查询,自动导入WebAdministration模块:
# 替换为你的目标服务器列表 $servers = @("Server01", "Server02", "Server03") # 替换为需要查询的应用池列表 $appPools = @("DefaultAppPool", "AppPool01", "AppPool02") $results = Invoke-Command -ComputerName $servers -ScriptBlock { param($appPools) Import-Module WebAdministration -ErrorAction Stop foreach ($pool in $appPools) { if (Test-Path IIS:\AppPools\$pool) { $schedule = (Get-ItemProperty -Path IIS:\AppPools\$pool -Name recycling.periodicRestart.schedule.collection).value $recycleTime = if ($schedule) { $schedule -join ", " } else { "无定时回收" } [PSCustomObject]@{ ServerName = $env:COMPUTERNAME AppPoolName = $pool RecycleTime = $recycleTime } } else { [PSCustomObject]@{ ServerName = $env:COMPUTERNAME AppPoolName = $pool RecycleTime = "应用池不存在" } } } } -ArgumentList (,$appPools) # 格式化输出结果,可导出为CSV $results | Select-Object ServerName, AppPoolName, RecycleTime | Format-Table -AutoSize # $results | Export-Csv -Path "C:\AppPoolRecycleTimes.csv" -NoTypeInformation
内容的提问来源于stack exchange,提问作者dbutts
相关产品推荐
相关产品推荐

