try / catch functionality

Hi Folks,

My goal is to telnet few ports from a variable list, against an IP

i have the following script, the problem is that, IF  it catches an error, the script will end in the catch block as opposed to continue trying the rest of the list..., can anybody give me any suggestions on what I'm doing wrong?

thanks,

CLEAR
$PORTS = Get-Content C:\Script\ports.txt
try {
    foreach ($port IN $PORTS){

    Write-host "Testing EPO_IP Against $PORT"
    New-Object system.net.sockets.tcpclient("10.50.50.50", $port) -ErrorAction SilentlyContinue
    }
}
catch {
write-host "EPO_IP failed against $PORT $_" -ErrorAction SilentlyContinue
}

Open in new window

LVL 3
ivan rosaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
You need to move the Try/Catch into the loop:
CLEAR
$PORTS = Get-Content C:\Script\ports.txt
ForEach ($port In $PORTS) {
	Try {
		Write-Host "Testing EPO_IP Against $PORT"
		New-Object system.net.sockets.tcpclient("10.50.50.50", $port) -ErrorAction Stop
	} Catch {
		Write-Host "EPO_IP failed against $PORT $($_.Exception.Message)"
	}
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David SankovskySenior SysAdminCommented:
indeed,
as oBdA said the location is wrong.

The way you wrote it, you are trying the loop rather then opening a telnet.
Also, You can use the test-netconnection cmdlet, it should provide faster results.:

CLEAR
$PORTS = Get-Content C:\Script\ports.txt
ForEach ($port In $PORTS) {
	Try {
		Write-Host "Testing EPO_IP Against $PORT"
		Test-NetConnection -ComputerName 10.50.50.50 -Port $port -ErrorAction Stop
	} Catch {
		Write-Host "EPO_IP failed against $PORT $($_.Exception.Message)"
	}
}

Open in new window

ivan rosaAuthor Commented:
Nailed it, thanks oBdA !!!
ivan rosaAuthor Commented:
that was a big silly me... thanks for pointing it out
$Thanks=Thanks*beyond!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.