iis_restart_pool.ps1¶
File:
iis/iis_scripts/iis_restart_pool.ps1
<#
.SYNOPSIS
IIS Application Pool Manager - List and restart pools
.EXAMPLE
.\script.ps1 -Action list
.\script.ps1 -Action restart_all
.\script.ps1 -Action restart_specific -PoolNames "Pool1","Pool2"
#>
param(
[Parameter(Mandatory=$true)]
[ValidateSet("list", "restart_all", "restart_specific")]
[string]$Action,
[Parameter(Mandatory=$false)]
[string[]]$PoolNames
)
Import-Module WebAdministration -ErrorAction Stop
Write-Host "`n=== IIS Application Pool Manager ===" -ForegroundColor Cyan
if ($Action -eq "list") {
Write-Host "MODE: List all application pools" -ForegroundColor Yellow
$Pools = Get-ChildItem IIS:\AppPools
Write-Host "`nFound $($Pools.Count) application pool(s):`n" -ForegroundColor Gray
foreach ($Pool in $Pools) {
Write-Host "Name: $($Pool.Name)" -ForegroundColor Cyan
Write-Host " State: $($Pool.State)" -ForegroundColor Gray
Write-Host " .NET Version: $($Pool.managedRuntimeVersion)" -ForegroundColor Gray
Write-Host " Pipeline Mode: $($Pool.managedPipelineMode)" -ForegroundColor Gray
Write-Host ""
}
} elseif ($Action -eq "restart_all") {
Write-Host "MODE: Restart ALL application pools" -ForegroundColor Yellow
$Pools = Get-ChildItem IIS:\AppPools
Write-Host "`nRestarting $($Pools.Count) application pool(s)..." -ForegroundColor Gray
foreach ($Pool in $Pools) {
$PoolName = $Pool.Name
$State = $Pool.State
Write-Host "`nPool: $PoolName (Current state: $State)" -ForegroundColor Cyan
try {
Restart-WebAppPool -Name $PoolName
Write-Host " Restarted successfully" -ForegroundColor Green
} catch {
Write-Host " ERROR: $($_.Exception.Message)" -ForegroundColor Red
}
}
} elseif ($Action -eq "restart_specific") {
if (-not $PoolNames) {
Write-Host "ERROR: -PoolNames required for restart_specific" -ForegroundColor Red
exit
}
Write-Host "MODE: Restart SPECIFIC application pools" -ForegroundColor Yellow
Write-Host "Pools to restart: $($PoolNames -join ', ')" -ForegroundColor Gray
foreach ($PoolName in $PoolNames) {
Write-Host "`nPool: $PoolName" -ForegroundColor Cyan
$Pool = Get-ChildItem IIS:\AppPools | Where-Object { $_.Name -eq $PoolName }
if ($Pool) {
$State = $Pool.State
Write-Host " Current state: $State" -ForegroundColor Gray
try {
Restart-WebAppPool -Name $PoolName
Write-Host " Restarted successfully" -ForegroundColor Green
} catch {
Write-Host " ERROR: $($_.Exception.Message)" -ForegroundColor Red
}
} else {
Write-Host " ERROR: Pool not found" -ForegroundColor Red
}
}
}
Write-Host "`n=== COMPLETE ===" -ForegroundColor Cyan