-
Notifications
You must be signed in to change notification settings - Fork 9
/
check_iis.ps1
95 lines (90 loc) · 3.13 KB
/
check_iis.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<#
.SYNOPSIS
Check IIS Website and AppPool status.
.DESCRIPTION
Check IIS Website and AppPool status and try to start stopped ones.
.PARAMETER Websites
Websites to check status.
Default All.
.PARAMETER AppPools
AppPools to check status.
Default All.
.OUTPUTS
OK: All Websites and AppPools Running.
WARNING: Websites or AppPools stopped, but started manually.
CRITICAL: Websites or AppPools stopped and could not started.
.EXAMPLE
Check a list of Websites and AppPools
.\check_iis.ps1 -Websites 'site 1','site 2','site 3' -AppPools 'apppool 1','app2','test 1'
Check all Websites and AppPools
.\check_iis.ps1
.NOTES
Author: Juan Granados
#>
Param(
[Parameter(Mandatory=$false,Position=0)]
[ValidateNotNullOrEmpty()]
$Websites="All",
[Parameter(Mandatory=$false,Position=1)]
[ValidateNotNullOrEmpty()]
$AppPools="All"
)
$ExitCode = "0"
$Output = ""
$WebsitesRunning = (Get-WebsiteState | Where-Object { ($_.value -eq "Started") }).Count
$AppPoolsRunning = (Get-WebAppPoolState | Where-Object { ($_.value -eq "Started") }).Count
$WebsitesTotal = (Get-WebsiteState).Count
$AppPoolTotal = (Get-WebAppPoolState).Count
$StoppedWebsites = $null
$StoppedAppPools = $null
Function Get-Status ($Names,$List,$Type,[ref]$ExitCode) {
$ReturnOutput=""
foreach ($Item in $List) {
if (($Names -eq "All") -or ($Names -contains $Item.Name)) {
if ($Type -eq "Websites"){
Start-Website -Name $Item.Name -ErrorAction SilentlyContinue
$Result = (Get-WebsiteState -Name $Item.Name).value
} else {
Start-WebAppPool -Name $Item.Name -ErrorAction SilentlyContinue
$Result = (Get-WebAppPoolState -Name $Item.Name).value
}
if ($Result -eq "Started") {
if ($ExitCode.Value -eq 0) {
$ExitCode.Value = "1"
}
if ($ReturnOutput -eq "") {
$ReturnOutput += "$($Type) not started: $($Item.Name)(Started manually). "
} else {
$ReturnOutput +="$($Item.Name)(Started manually). "
}
} else {
$ExitCode.Value = "2"
If ($ReturnOutput -eq "") {
$ReturnOutput += "$($Type) not started: $($Item.Name)(Stopped). "
} else{
$ReturnOutput +="$($Item.Name)(Stopped). "
}
}
}
}
Return $ReturnOutput
}
$StoppedWebsites = Get-WebsiteState | Where-Object { ($_.value -ne "Started") } | % { return @{($_.itemxpath -split ("'"))[1]="$($_.value)" } } | % getEnumerator
if ($StoppedWebsites) {
$Output += Get-Status $Websites $StoppedWebsites "Websites" ([ref]$ExitCode)
}
$StoppedAppPools = Get-WebAppPoolState | Where-Object { ($_.value -ne "Started") } | % { return @{($_.itemxpath -split ("'"))[1]="$($_.value)" } } | % getEnumerator
if ($StoppedAppPools) {
$Output += Get-Status $AppPools $StoppedAppPools "AppPools" ([ref]$ExitCode)
}
$Output += "| WebsitesRunning=$($WebsitesRunning);0;0;0;$($WebsitesTotal) AppPoolsRunning=$($AppPoolsRunning);0;0;0;$($AppPoolTotal)"
if ($ExitCode -eq 2) {
Write-Output "CRITICAL: $($Output)"
Exit(2)
} elseif ($ExitCode -eq 1) {
Write-Output "WARNING: $($Output)"
Exit(1)
} else {
Write-Output "OK: All Websites and AppPools Running. $($Output)"
Exit(0)
}