Test connectivity to port

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
            }
        }
    }
} 

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
            }
}

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

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

I see you edited the code let me retry

Unexpected token ‘elseif’ in expression or statement.

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. :smiley: 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
}
}

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
		}
	}
}

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

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

$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.

$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.

Ooops Mark beat me to it.

Thanks Fellas

Very thorough explanation Really appreciate it!