Don't ask me why (it's complicated), but we had a situation where we wanted to recycle several of our production IIS App Pools on one of our web server.
We want to do this as a Windows Schedule Task, because it was required to be done during very early mornings (daily job, executed once).
Our initial plan was to have a PowerShell script and ran it through Windows Task Scheduler.
So we have asked a script from ChatGPT for that (BTW: who write PowerShell script manually these days?)
This is what was provided:
<#
.SYNOPSIS
Recycles a specific IIS Application Pool.
.DESCRIPTION
This script recycles a given IIS Application Pool using the WebAdministration module.
You must run it with Administrator privileges.
.PARAMETER AppPoolName
The name of the IIS Application Pool to recycle.
.EXAMPLE
.\Recycle-AppPool.ps1 -AppPoolName "MyAppPool"
#>
param (
[Parameter(Mandatory=$true)]
[string]$AppPoolName
)
# Import IIS module
Import-Module WebAdministration -ErrorAction Stop
# Check if the app pool exists
$appPool = Get-ChildItem IIS:\AppPools | Where-Object { $_.Name -eq $AppPoolName }
if ($null -eq $appPool) {
Write-Host "Application Pool '$AppPoolName' not found." -ForegroundColor Red
exit 1
}
try {
Write-Host "Recycling Application Pool: $AppPoolName ..." -ForegroundColor Cyan
Restart-WebAppPool -Name $AppPoolName
Write-Host "Successfully recycled Application Pool: $AppPoolName" -ForegroundColor Green
}
catch {
Write-Host "Failed to recycle Application Pool '$AppPoolName'. Error: $_" -ForegroundColor Red
exit 1
}
We could run this following manner:
.\Recycle-AppPool.ps1 -AppPoolName "MyAppPool"
One line for each app pool we want to recycle.
But then, when we asked ChatGPT, to give command line for this to run in Windows Task Scheduler, it has provided this:
powershell.exe
-NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\Recycle-AppPool.ps1" -AppPoolName "MyAppPool"
However, proactively, it has provided much simpler approach as well. That was the surprise here.
ChatGPT has suggested to use appcmd.exe.
Never heard that before. Then I realized it is a command line utility which get installed when we install IIS stack.
It was much easier to use it (though it had less error handling compared to PowerShell script).
%windir%\system32\inetsrv\appcmd.exe
recycle apppool /apppool.name:"MyAppPool"
At the end we choose to use appcmd.exe.
That's something we learned, this week. Thanks ChatGPT.