Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function setup(){ # Load a few initial variables and call the main function.
- $loop = 'true'
- $runPath = 'c:\scripts' # read-host "Where is powerPing located? [type the directory path]"
- $failures = @()
- $content = (GET-CONTENT $runPath\hosts.txt)
- $upperBound = $content.getUpperBound(0)
- $it = [int]"-1"
- # Loads the config options as variables to be used later.
- $smtpServer = (get-content $runPath\config.txt)[0]
- $alertSubject = (get-content $runPath\config.txt)[1]
- $alertSubject2 = $alertSubject
- $alertBody = (get-content $runPath\config.txt)[2]
- $alertRecipient = (get-content $runPath\config.txt)[3]
- $alertSender = (get-content $runPath\config.txt)[4]
- main
- }
- function main(){
- clear
- while($loop) {
- $it = [int]$it + [int]1
- $iteration = $it + 1
- write-host "Iteration $iteration"
- $selHost = (GET-CONTENT $runPath\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
- start-sleep -s 1
- function sendAlert(){ # Sends email alert using variables defined in config.txt and restarts the looping process.
- write-host "ERMAHGERD! ALERTTTT!"
- #send-mailmessage -to "$alertRecipient" -from "$alertSender" -subject "$alertSubject2" -body "$alertBody" -smtpServer "$smtpServer"
- }
- function failTesting(){ # Tests the failed host three more times and then adds it to $failures and calls sendAlert if it doesn't pass.
- clear
- for($i = 0; $i -lt 3; $i++){
- write-host "Connecting to: $selHost"
- $alive = test-connection $selHost -count 2 -delay 1 -quiet
- if($alive){
- write-host "The connection to $selHost has succeded!"
- $alert = 'false'
- main
- }
- elseif($alive = 'False'){
- write-host "The connection to $selHost has failed!"
- $alert = 'true'
- }
- }
- if($alert){
- start-sleep -s 1
- write-host "The connection to $selhost failed too many times."
- write-host "Adding $selHost to list of failures."
- $failures += ,$selhost
- write-host "Failures: $failures"
- sendAlert
- }
- }
- 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
- }
- elseif($failures -notcontains $selhost){
- write-host "The connection to $selHost has succeded!"
- start-sleep -s 1
- }
- }
- 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 "Trying three more times..."
- start-sleep -s 3
- failTesting
- }
- elseif($failures -contains $selhost){
- start-sleep -s 1
- }
- }
- if([int]$it -ge [int]$upperBound){ # This code restarts the loop at the first hostname
- $it = [int]"-1"
- clear
- }
- #End of loop
- }
- }
- setup
Advertisement
Add Comment
Please, Sign In to add comment