Advertisement
Catatonic27

powerPing

May 1st, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # The variables in order of their appearence:
  2. # $it = Main while loop interation number.
  3. # $content = The entire contents of the hosts.txt as an array.
  4. # $upperBound = The zero-based number of array objects in hosts.txt. i.e.: Five objects will return a value of 4.
  5. # $selHost = The current line# of hosts.txt that the script is operating on.
  6. # $alive = Did the test-connection work? Returns a boolean value.
  7.  
  8. function main(){ # Load a few initial variables and call secondary.
  9.     clear
  10.     $failures = @()
  11.     $content = (GET-CONTENT .\hosts.txt)
  12.     $upperBound = $content.getUpperBound(0)
  13.     # Loads the config options as variables to be used later.
  14.         $smtpServer = (get-content .\config.txt)[0]
  15.         $alertSubject = (get-content .\config.txt)[1]
  16.         $alertBody = (get-content .\config.txt)[2]
  17.         $alertRecipient = (get-content .\config.txt)[3]
  18.         $alertSender = (get-content .\config.txt)[4]
  19.         #write-host "$smtpServer"
  20.         #write-host "$alertSubject"
  21.         #write-host "$alertBody"
  22.         #write-host "$alertRecipient"
  23.         #write-host "$alertSender"
  24.  
  25.     function secondary(){ # Contains the main loop and a few other misc things.
  26.         while([int]$it -lt [int]$upperBound) { # Stops the loop if the iteration number exceeds the number of objects in $content.
  27.             $it = [int]$it + [int]1 # Advances the value of $it by +1 every time it runs.
  28.             $iteration = $it + 1
  29.             write-host "Iteration $iteration"
  30.             $selHost = (GET-CONTENT .\hosts.txt)[$it] # Grabs the line number $it from hosts.txt and wraps it in $selHost.
  31.             write-host "Connecting to: $selHost"
  32.             $alive = test-connection $selHost -count 2 -delay 1 -quiet
  33.             $alertSubject
  34.             start-sleep -s 1
  35.            
  36.             function testing(){ # Decides what to do with the results of the test-connection.
  37.                 if($alive){ # What to do if the test-connection returns "true".
  38.                     if($failures -contains $selhost){
  39.                         $temp = @()
  40.                         foreach($computer in $failures){ # Removes $selhost from $failures if it was added previously, but has come back online.
  41.                             if($computer -ne $selhost){
  42.                                 $temp += $computer
  43.                             }
  44.                         }
  45.                         $failures = $temp
  46.                        
  47.                         write-host "The connection to $selHost has succeded!"
  48.                         start-sleep -s 1
  49.                         secondary
  50.                     }
  51.                     elseif($failures -notcontains $selhost){
  52.                         write-host "The connection to $selHost has succeded!"
  53.                         start-sleep -s 1
  54.                         secondary
  55.                     }
  56.                 }
  57.                 elseif($alive = 'False'){ # What to do if the test-connection returns "false".
  58.                         write-host "The connection to $selHost has failed!"
  59.                     if($failures -notcontains $selhost){
  60.                         write-host "Going in for failTesting..."
  61.                         start-sleep -s 3
  62.                         failTesting
  63.                     }
  64.                     elseif($failures -contains $selhost){
  65.                         start-sleep -s 1
  66.                         secondary
  67.                     }
  68.                 }
  69.             }
  70.             start-sleep -s 1
  71.             testing
  72.         }
  73.         # The following chunk of code runs at the end of every loop.
  74.         $it = "-1"
  75.         start-sleep -s 1
  76.         write-host "Restarting loop in five seconds..."
  77.         start-sleep -s 5
  78.         clear
  79.         secondary
  80.     }
  81.     function sendAlert(){ # Sends email alert using variables defined in config.txt and restarts the looping process.
  82.         send-mailmessage -to "$alertRecipient" -from "$alertSender" -subject "$alertSubject" -body "$alertBody" -smtpServer "$smtpServer"
  83.         secondary
  84.     }
  85.     function failTesting(){ # Tests the failed host three more times and then adds it to $failures and calls sendAlert if it doesn't pass.
  86.         for ($i = 0; $i -lt 3; $i++){
  87.             $alive = test-connection $selHost -count 2 -delay 1 -quiet
  88.             if($alive){
  89.                 write-host "The connection to $selHost has succeded!"
  90.                 secondary
  91.             }
  92.             elseif($alive = 'False'){
  93.                 write-host "The connection to $selHost has failed!"
  94.             }
  95.         }
  96.         start-sleep -s 1
  97.         write-host "The connection to $selhost failed too many times."
  98.         $failures += ,$selhost
  99.         sendAlert
  100.     }
  101.     $it = "-1"
  102.     secondary
  103. }
  104. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement