Guest User

powerPing

a guest
Jun 26th, 2015
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function setup(){ # Load a few initial variables and call the main function.
  2.     $loop = 'true'
  3.     $runPath = 'c:\scripts' # read-host "Where is powerPing located? [type the directory path]"
  4.     $failures = @()
  5.     $content = (GET-CONTENT $runPath\hosts.txt)
  6.     $upperBound = $content.getUpperBound(0)
  7.     $it = [int]"-1"
  8.     # Loads the config options as variables to be used later.
  9.     $smtpServer = (get-content $runPath\config.txt)[0]
  10.     $alertSubject = (get-content $runPath\config.txt)[1]
  11.     $alertSubject2 = $alertSubject
  12.     $alertBody = (get-content $runPath\config.txt)[2]
  13.     $alertRecipient = (get-content $runPath\config.txt)[3]
  14.     $alertSender = (get-content $runPath\config.txt)[4]
  15.     main
  16. }
  17. function main(){
  18.     clear
  19.     while($loop) {
  20.     $it = [int]$it + [int]1
  21.     $iteration = $it + 1
  22.     write-host "Iteration $iteration"
  23.     $selHost = (GET-CONTENT $runPath\hosts.txt)[$it] # Grabs the line number $it from hosts.txt and wraps it in $selHost.
  24.     write-host "Connecting to: $selHost"
  25.     $alive = test-connection $selHost -count 2 -delay 1 -quiet
  26.     start-sleep -s 1
  27.    
  28.     function sendAlert(){ # Sends email alert using variables defined in config.txt and restarts the looping process.
  29.         write-host "ERMAHGERD! ALERTTTT!"
  30.         #send-mailmessage -to "$alertRecipient" -from "$alertSender" -subject "$alertSubject2" -body "$alertBody" -smtpServer "$smtpServer"
  31.     }
  32.    
  33.     function failTesting(){ # Tests the failed host three more times and then adds it to $failures and calls sendAlert if it doesn't pass.
  34.         clear
  35.         for($i = 0; $i -lt 3; $i++){
  36.             write-host "Connecting to: $selHost"
  37.             $alive = test-connection $selHost -count 2 -delay 1 -quiet
  38.             if($alive){
  39.                 write-host "The connection to $selHost has succeded!"
  40.                 $alert = 'false'
  41.                 main
  42.             }
  43.             elseif($alive = 'False'){
  44.                 write-host "The connection to $selHost has failed!"
  45.                 $alert = 'true'
  46.             }
  47.         }
  48.         if($alert){
  49.             start-sleep -s 1
  50.             write-host "The connection to $selhost failed too many times."
  51.             write-host "Adding $selHost to list of failures."
  52.             $failures += ,$selhost
  53.             write-host "Failures: $failures"
  54.             sendAlert
  55.         }
  56.     }
  57.    
  58.     if($alive){ # What to do if the test-connection returns "true".
  59.         if($failures -contains $selhost){
  60.             $temp = @()
  61.             foreach($computer in $failures){ # Removes $selhost from $failures if it was added previously, but has come back online.
  62.                 if($computer -ne $selhost){
  63.                     $temp += $computer
  64.                 }
  65.             }
  66.             $failures = $temp
  67.            
  68.             write-host "The connection to $selHost has succeded!"
  69.             start-sleep -s 1
  70.         }
  71.         elseif($failures -notcontains $selhost){
  72.             write-host "The connection to $selHost has succeded!"
  73.             start-sleep -s 1
  74.         }
  75.     }
  76.     elseif($alive = 'False'){ # What to do if the test-connection returns "false".
  77.             write-host "The connection to $selHost has failed!"
  78.         if($failures -notcontains $selhost){
  79.             write-host "Trying three more times..."
  80.             start-sleep -s 3
  81.             failTesting
  82.         }
  83.         elseif($failures -contains $selhost){
  84.             start-sleep -s 1
  85.         }
  86.     }
  87.    
  88.     if([int]$it -ge [int]$upperBound){ # This code restarts the loop at the first hostname
  89.         $it = [int]"-1"
  90.         clear
  91.     }
  92.     #End of loop
  93.     }
  94. }
  95. setup
Advertisement
Add Comment
Please, Sign In to add comment