Test connectivity to port

This topic contains 13 replies, has 3 voices, and was last updated by Profile photo of 37mm 37mm 1 year, 5 months ago.

  • Author
    Posts
  • #31079
    Profile photo of 37mm
    37mm
    Participant

    I want to adjust the code to to test connectivity to a port lets say 3 times. If all 3 fail to then send an email. I presume a counter is my solution but I am at an impass. This is what I have but I know there is a better way! Cheers fellas

    ### Port Test
    $25Port  = Test-NetConnection 8.8.8.8 -Port 25 -InformationLevel Quiet 
    
    ### IF statment to Test the Port
    if ($25Port -match "False")
    {    
    if ($25Port -match "False")
        {
        if ($25Port -match "False")
            {
            if ($25Port -match "False")
                {
                $sentemail = Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $Credentials
                }
            }
        }
    } 
    
  • #31080
    Profile photo of Mark Hammonds
    Mark Hammonds
    Participant

    Maybe some thing like this?

    for($i = 0; $i -lt 3; $I++){
    	if(Test-NetConnection 8.8.8.8 -Port 25 -InformationLevel Quiet){"Port Open"}
    }
    elseif($I -eq 3){
    Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $Credentials
                }
    }
    
  • #31081
    Profile photo of 37mm
    37mm
    Participant

    yep, and I dont need the variable in there, that was a mistake. I will try it now thanks

    $sentemail = Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $Credentials

    should just be this

    Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $Credentials

  • #31082
    Profile photo of 37mm
    37mm
    Participant

    It doesn't execute the elseif statement {piece} in my testing of it.

    I see you edited the code let me retry

  • #31083
    Profile photo of 37mm
    37mm
    Participant

    Unexpected token 'elseif' in expression or statement.

  • #31085
    Profile photo of Mark Hammonds
    Mark Hammonds
    Participant

    oops thats why you dont edit in note pad lol had one to many } also forgot to break out of lookp on success. try that. 😀 as you can see im learning too.

    for($i = 0; $i -lt 3; $I++){
    	if(Test-NetConnection 8.8.8.8 -Port 25 -InformationLevel Quiet){
    "Port Open" 
    Break
        }
    
    elseif($I -eq 3){
    Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $Credentials
    }
    }
    
  • #31086
    Profile photo of Chris Wolfenden
    Chris Wolfenden
    Participant

    A slight rework of Mark's code:

    $fail = 0
    for($i = 0; $i -lt 3; $i++){
    	if(Test-NetConnection 8.8.8.8 -Port 25 -InformationLevel Quiet){"Port Open"}
    	else{
    		$fail ++
    		if ($fail -eq 3){
    			Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $Credentials
    		}
    	}
    }
    
    
  • #31087
    Profile photo of Mark Hammonds
    Mark Hammonds
    Participant

    OOOO Chris has better code no need for the Break out.

  • #31088
    Profile photo of 37mm
    37mm
    Participant

    Awesome, works great. Now What is going with the For (?) and the $fail++ , to expand what are these doing?

  • #31089
    Profile photo of Mark Hammonds
    Mark Hammonds
    Participant

    $Fail is equal to 0 at beginning of script $Fail++ enumerates it + 1 for every loop until it gets to 3 . a similar thing is happening in the For loop it is adding 1 to $i until it gets to 3

    when ever a variable has an integer in it and you do ++ you increment it + 1.

    its like saying
    $fail = 0
    $Fail = $Fail + 1

    I hope I explained that well I came from VB and sometimes my logic is off.

  • #31090
    Profile photo of Chris Wolfenden
    Chris Wolfenden
    Participant

    $fail++ is equivalent to $fail = $fail + 1. In other words add 1 to $fail variable (keep track of the failures). If you hit 3 failures, send message.

    As far as the for loop, a for loop is an old iteration loop found in majority of programming languages. Someone else might be able to point you to a good article or write-up on it, but in general the syntax is:

    for (INITIALIZATION; CONDITION; AFTERTHOUGHT)
    {
    // Code for the for-loop's body goes here.
    }

    INITIALIZATION – Runs before the for loop is executed, in our example it initialized $i = 0.
    CONDITION – After each iteration the condition is checked. If true, the for loop iterates again, if false the for loop is complete
    AFTERTHOUGHT – Runs upon completion of each iteration of the loop. In our example, adds 1 to $i.

  • #31091
    Profile photo of Chris Wolfenden
    Chris Wolfenden
    Participant

    Ooops Mark beat me to it.

  • #31093
    Profile photo of 37mm
    37mm
    Participant

    Thanks Fellas

  • #31095
    Profile photo of 37mm
    37mm
    Participant

    Very thorough explanation Really appreciate it!

You must be logged in to reply to this topic.