-
Notifications
You must be signed in to change notification settings - Fork 0
/
systempinger.ps1
58 lines (50 loc) · 2.84 KB
/
systempinger.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
# This script when run from a scheduled task upon start of the system will continually run and ping
# a list of computers to determine if they are available.
# Systems that are reported down, then come back up, will in the next cycle be alerted that they are back online.
$computers = get-content C:\pathto\pingnodes.txt # List of computers to ping
$emailFrom = "[email protected]" # Email address to send notification from
$emailTo = "[email protected]" # Email address to send notification to
$unresponsive = @() # Initialize empty array to store previously unresponsive computers
while ($true) { # Loop continuously
$failed = @() # Initialize empty array to store failed computers
$responded = @() # Initialize empty array to store previously unresponsive computers that have now responded
foreach ($computer in $computers) {
if (!(Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue)) {
$failed += $computer # Add failed computer to the array
}
elseif ($unresponsive -contains $computer) {
$responded += $computer # Add previously unresponsive computer to the array
}
}
$body = "Pinging the following computers:`n`n$($computers -join "`n")`n`n"
if ($failed) { # If there are failed computers
$body += "This script pings every 10 minutes a list of systems found in \\server\share\pingnodes.txt. It is run as a scheduled task that launches upon system startup. The following systems are NOT responding:`n`n$($failed -join "`n")"
$smtpServer = "yourSMTPserver.com"
$messageParams = @{
To = $emailTo
From = $emailFrom
Subject = "Computer(s) not responding"
Body = $body
SmtpServer = $smtpServer
}
Send-MailMessage @messageParams
}
if ($responded) { # If there are previously unresponsive computers that have now responded
$body = "The following computers have come back online:`n`n$($responded -join "`n")"
$smtpServer = "YourSMTPServer.com"
$messageParams = @{
To = $emailTo
From = $emailFrom
Subject = "Computer(s) back online"
Body = $body
SmtpServer = $smtpServer
}
Send-MailMessage @messageParams
$unresponsive = $unresponsive | Where-Object { $_ -notin $responded } # Remove previously unresponsive computers that have now responded
}
if ($failed -or $unresponsive) { # If there are failed computers or previously unresponsive computers
$unresponsive += $failed # Add currently unresponsive computers to the array of previously unresponsive computers
$unresponsive = $unresponsive | Select-Object -Unique # Remove duplicates
}
Start-Sleep -Seconds 600 # Wait for 10 minutes before pinging again
}