Skip to content

Instantly share code, notes, and snippets.

@keyan1603
Last active September 5, 2024 19:49
Show Gist options
  • Save keyan1603/0f1214707b8e5cc60eb2720bbc850337 to your computer and use it in GitHub Desktop.
Save keyan1603/0f1214707b8e5cc60eb2720bbc850337 to your computer and use it in GitHub Desktop.
Add multiple App Pool recycles at specific time of day
# Use the appropriate base image
FROM mcr.microsoft.com/windows/servercore:ltsc2019
# Copy the PowerShell script to the container
COPY set-recycle-times.ps1 /set-recycle-times.ps1
# Run the PowerShell script to set the recycle times
RUN powershell -ExecutionPolicy Bypass -File /set-recycle-times.ps1
Import-Module WebAdministration
# Set the recycle times for the application pool
$appPoolName = "DefaultAppPool" # Replace with your app pool name
$recycleTimes = @("01:00:00", "17:00:00")
# Clear existing recycle times
Clear-ItemProperty -Path "IIS:\AppPools\$appPoolName\recycling\periodicRestart\schedule" -Name "value"
# Add new recycle times
foreach ($time in $recycleTimes) {
New-ItemProperty -Path "IIS:\AppPools\$appPoolName\recycling\periodicRestart\schedule" -Name "value" -Value $time
}
Import-Module WebAdministration
# Define the app pool name
$appPoolName = "DefaultAppPool"
# Clear existing schedule
Clear-WebConfiguration "/system.applicationHost/applicationPools/add[@name='$appPoolName']/recycling/periodicRestart/schedule"
# Add recycling times directly using WebConfiguration
Add-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST" `
-filter "system.applicationHost/applicationPools/add[@name='$appPoolName']/recycling/periodicRestart/schedule" `
-name "." -value @{ value = "02:00:00" }
Add-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST" `
-filter "system.applicationHost/applicationPools/add[@name='$appPoolName']/recycling/periodicRestart/schedule" `
-name "." -value @{ value = "14:00:00" }
Write-Host "Recycle times added for $appPoolName."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment