-
Notifications
You must be signed in to change notification settings - Fork 0
/
iis-apppool-stop.ps1
46 lines (37 loc) · 1.33 KB
/
iis-apppool-stop.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
param(
#AppPool Name
[Parameter(Mandatory=$True)][string]$appPoolName
)
# Load IIS module:
Import-Module WebAdministration
# Get the number of retries
$retries = "20"
# Get the number of attempts
$delay = "500"
# Check if exists
if(Test-Path IIS:\\AppPools\$appPoolName) {
# Stop App Pool if not already stopped
if ((Get-WebAppPoolState $appPoolName).Value -ne "Stopped") {
echo "Stopping IIS app pool $appPoolName"
Stop-WebAppPool $appPoolName
$state = (Get-WebAppPoolState $appPoolName).Value
$counter = 1
# Wait for the app pool to the "Stopped" before proceeding
do{
$state = (Get-WebAppPoolState $appPoolName).Value
echo "$counter/$retries Waiting for IIS app pool $appPoolName to shut down completely. Current status: $state"
$counter++
Start-Sleep -Milliseconds $delay
}
while($state -ne "Stopped" -and $counter -le $retries)
# Throw an error if the app pool is not stopped
if($counter -gt $retries) {
throw "Could not shut down IIS app pool $appPoolName. `nTry to increase the number of retries ($retries) or delay between attempts ($delay milliseconds)." }
}
else {
echo "$appPoolName already Stopped"
}
}
else {
echo "IIS app pool $appPoolName doesn't exist"
}