How to check port status via Power Shell 3.0-4.0

To Expert
How to check port status via Power Shell 3.0 - 4.0

Situation
Sometime application software such as ERP disconnect from server. after that I have to ping-test connection between client and server . It 's OK (Ping ComputerName -n 100)

The next step , I focus at port status of this application . It is establish already or not but I don't know a syntax with Windows Power Shell 3.0 - 4.0
Please recommend

Thank you for help / Best Regard
ThenkungAsked:
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.

SreRajCommented:
Hi,

You could use the following commands to check Port Status

$Port = New-Object Net.Sockets.TcpClient
$Port.Connect("ComputerName",Port)
$Port.Connected

If the port is in connected status, then $Port.Connected will return True. If the port is closed or filtered, then it will return false.

$Port = New-Object Net.Sockets.TcpClient
$Port.Connect("ComputerName",Port)
if($Port.Connected){
        Write-Host "Port is Connected"
}Else{
       Write-Host "Port is blocked or filtered"
}
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I don't understand what you are after exactly. Do you want to perform a check on the client for reachability of the application port? Then above test should follow a Test-Connection -Quiet -Count 1 $Servername (that's "ping").

Or do you need to check on client and/or server for an established connection?
Dan McFaddenSystems EngineerCommented:
I was wondering the same thing Qlemo.  I interpreted it as first a ping then a port connection test.

I've used the following script to hack a monitor together:

$HostName = "<YourServerNameGoesHere>"
$ApplicationPort = 80
$PingCount = 3

if (Test-Connection -ComputerName $HostName -Count $PingCount -Quiet)
{
    Write-Host $HostName "replied to" $PingCount "pings."
    Write-Host "Connecting to remote host:" $HostName":"$ApplicationPort
    $Connection = New-Object System.Net.Sockets.TcpClient
    $Connection.Connect($HostName, $ApplicationPort)
    if (!$Connection.Connected)
    {
        Write-Host "The connection to" $HostName "on port" $ApplicationPort "timed out."
    } else {
        Write-Host "The connection to" $HostName "on port" $ApplicationPort "was successful."
    }
    $Connection.Close()
} else {
    Write-Host $HostName "failed to respond to PING."
}

Open in new window


Dan

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
ThenkungAuthor Commented:
Thank
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.