Friday 30 May 2014

Use PowerShell to set random periodic restart time on all Application Pools

I have written a PowerShell script that will look at all IIS Application Pools, then set the recycle setting for Specific Time to a random time between 02.01 - 02.59.

It will create a log file D:\AppPoolRestart.txt which contains the App Pool name and the recycle time.



import-module webadministration
$AppPools = get-item -Path IIS:\AppPools\*
If (Test-Path D:\scripts\AppPoolRestart.txt) 
{
remove-item -path D:\AppPoolRestart.txt
}

ForEach ( $app in $appPools )

$minute = Get-Random -Minimum 1 -Maximum 59
$Name = $app.Name
$time = "02:"+"$minute"+":00"
Set-ItemProperty -Path IIS:\AppPools\$Name -Name Recycling.periodicRestart.schedule.collection -Value @{value="$time"}
$output = "$Name"+" $time"
out-file -Filepath D:\AppPoolRestart.txt -inputobject "$output" -append
}