Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # The variables in order of their appearence:
- # $it = Main while loop interation number.
- # $content = The entire contents of the hosts.txt as an array.
- # $upperBound = The zero-based number of array objects in hosts.txt. i.e.: Five objects will return a value of 4.
- # $selHost = The current line# of hosts.txt that the script is operating on.
- # $alive = Did the test-connection work? Returns a boolean value.
- function main(){ # Load a few initial variables and call secondary.
- clear
- $failures = @()
- $content = (GET-CONTENT .\hosts.txt)
- $upperBound = $content.getUpperBound(0)
- # Loads the config options as variables to be used later.
- $smtpServer = (get-content .\config.txt)[0]
- $alertSubject = (get-content .\config.txt)[1]
- $alertBody = (get-content .\config.txt)[2]
- $alertRecipient = (get-content .\config.txt)[3]
- $alertSender = (get-content .\config.txt)[4]
- #write-host "$smtpServer"
- #write-host "$alertSubject"
- #write-host "$alertBody"
- #write-host "$alertRecipient"
- #write-host "$alertSender"
- function secondary(){ # Contains the main loop and a few other misc things.
- while([int]$it -lt [int]$upperBound) { # Stops the loop if the iteration number exceeds the number of objects in $content.
- $it = [int]$it + [int]1 # Advances the value of $it by +1 every time it runs.
- $iteration = $it + 1
- write-host "Iteration $iteration"
- $selHost = (GET-CONTENT .\hosts.txt)[$it] # Grabs the line number $it from hosts.txt and wraps it in $selHost.
- write-host "Connecting to: $selHost"
- $alive = test-connection $selHost -count 2 -delay 1 -quiet
- $alertSubject
- start-sleep -s 1
- function testing(){ # Decides what to do with the results of the test-connection.
- if($alive){ # What to do if the test-connection returns "true".
- if($failures -contains $selhost){
- $temp = @()
- foreach($computer in $failures){ # Removes $selhost from $failures if it was added previously, but has come back online.
- if($computer -ne $selhost){
- $temp += $computer
- }
- }
- $failures = $temp
- write-host "The connection to $selHost has succeded!"
- start-sleep -s 1
- secondary
- }
- elseif($failures -notcontains $selhost){
- write-host "The connection to $selHost has succeded!"
- start-sleep -s 1
- secondary
- }
- }
- elseif($alive = 'False'){ # What to do if the test-connection returns "false".
- write-host "The connection to $selHost has failed!"
- if($failures -notcontains $selhost){
- write-host "Going in for failTesting..."
- start-sleep -s 3
- failTesting
- }
- elseif($failures -contains $selhost){
- start-sleep -s 1
- secondary
- }
- }
- }
- start-sleep -s 1
- testing
- }
- # The following chunk of code runs at the end of every loop.
- $it = "-1"
- start-sleep -s 1
- write-host "Restarting loop in five seconds..."
- start-sleep -s 5
- clear
- secondary
- }
- function sendAlert(){ # Sends email alert using variables defined in config.txt and restarts the looping process.
- send-mailmessage -to "$alertRecipient" -from "$alertSender" -subject "$alertSubject" -body "$alertBody" -smtpServer "$smtpServer"
- secondary
- }
- function failTesting(){ # Tests the failed host three more times and then adds it to $failures and calls sendAlert if it doesn't pass.
- for ($i = 0; $i -lt 3; $i++){
- $alive = test-connection $selHost -count 2 -delay 1 -quiet
- if($alive){
- write-host "The connection to $selHost has succeded!"
- secondary
- }
- elseif($alive = 'False'){
- write-host "The connection to $selHost has failed!"
- }
- }
- start-sleep -s 1
- write-host "The connection to $selhost failed too many times."
- $failures += ,$selhost
- sendAlert
- }
- $it = "-1"
- secondary
- }
- main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement