-
Notifications
You must be signed in to change notification settings - Fork 0
/
Uptime Monitor - Variables Unconfigured.ps1
120 lines (91 loc) · 4.87 KB
/
Uptime Monitor - Variables Unconfigured.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Create an array that contains hostnames.
$names = @("host1","host2","host3","etc")
# Define email address to send and receive alerts
$fromAddress = "[email protected]"
$toAddress = "[email protected]"
# Define smtp server and port to send alerts using the $fromAddress
$smtpServer = "smtpServerHostname"
$smtpPort = 587
$hostDownEmailBody=@"
<head>
<style type='text/css'>
body {
font-family: Calibri;
font-size: 11pt;
color: red;
}
span{
font-family: Calibri;
font-size: 11pt;
color: black;
}
</style>
</head>
<body>
<p><span>$name </span><b>has gone down!</b></p>
</body>
"@
# Create an empty array that data will be added to in the loop below, the resulting array will be used to push ping and uptime results too and is needed for the XAML GUI.
$ResultsTableArray = @()
# Create an empty array that stored ping result data will be added to later on, [System.Collections.ArrayList] allows the size to not be fixed.
# Create intial ping data, true 6x, to begin the stored ping data
# Create a sample DownStatus array to compare with PingData for confirmation that a host has been down.
[System.Collections.ArrayList]$PingData = @()
[System.Collections.ArrayList]$InitialData = 1,1,1,1,1,1
[System.Collections.ArrayList]$DownStatus = 0,0,0,0,0,0
# Loop through each hostname provided in $names and add properties to $array that will be displayed as columns in a table in the GUI.
foreach ($name in $names) {
$object = New-Object System.Object
#$object | Add-Member -type NoteProperty -name Index -Value $index1
$object | Add-Member -type NoteProperty -name Host -Value $name
$object | Add-Member -type NoteProperty -name IP -Value "x.x.x.x"
$object | Add-Member -type NoteProperty -name PingStatus -Value "?"
$object | Add-Member -type NoteProperty -name Uptime -Value "dhms"
$ResultsTableArray += $object
$PingData.Add($InitialData) #this line causes print output of 0-18 on start of script run
}
while ($true) { # Endless while loop
# Define index variable for second loop (ping test loop)
$index2 = 0
foreach ($name in $names) {
#Write-Host $index2
if ( Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue ) {
# Get-CimInstance needs to be run with domain admin privileges
#$wmi = Get-CimInstance Win32_OperatingSystem -computer $name
#$LBTime = $wmi.Lastbootuptime
#[TimeSpan]$uptime = New-TimeSpan $LBTime $(get-date)
$ResultsTableArray[$index2].IP = Test-Connection -ComputerName $name -Count 1 | Select-Object -ExpandProperty IPV4Address
$ResultsTableArray[$index2].PingStatus = "Up"
$ResultsTableArray[$index2].Uptime = "$($uptime.days)d$($uptime.hours)h$($uptime.minutes)m$($uptime.seconds)s"
# Build the ping data storage arrays
$null, $rest = $PingData[$index2]
[System.Collections.ArrayList]$PingData[$index2] = $rest
[void]$PingData[$index2].Add(1)
} else {
$ResultsTableArray[$index2].IP = Test-Connection -ComputerName $name -Count 1 | Select-Object IPV4Address
$ResultsTableArray[$index2].PingStatus = "Down"
$ResultsTableArray[$index2].Uptime = "n/a"
# Build the ping data storage arrays
$null, $rest = $PingData[$index2]
[System.Collections.ArrayList]$PingData[$index2] = $rest
[void]$PingData[$index2].Add(0)
}
# Compare-Object will return $null when comparing arrays that are equal but indexed in different order, i.e. it returns $null for (0,0,1) vs (0,1,0), but here the array we compare with is (0,0,0,0,0,0) so order does not matter anyway.
# Define conditions for excluding particular hosts from Send-Mailmessage action if they do not warrant an email alert when down.
if (($name -eq "host1") -And ((Compare-Object -ReferenceObject $PingData[$index2] -DifferenceObject $DownStatus) -eq $null)) {
Write-Host "host1 down"
}
elseif (($name -eq "host2") -And ((Compare-Object -ReferenceObject $PingData[$index2] -DifferenceObject $DownStatus) -eq $null)) {
Write-Host "host2 down"
}
elseif (($name -ne "host1" -or $name -ne "host2") -And ((Compare-Object -ReferenceObject $PingData[$index2] -DifferenceObject $DownStatus) -eq $null)) {
Send-Mailmessage -smtpServer $smtpServer -Port $smtpPort -from $fromAddress -to $toAddress -subject "$name has gone down!" -body $hostDownEmailBody -BodyAsHTML -priority High
}
else {
}
$index2 ++
}
#Write-Host "";
$ResultsTableArray | Format-Table
Start-Sleep -s 60
}