Go to All Forums

Port connection monitor

I have a Windows machine with and application that connects to a remote port on an external server. I need to monitor if the connection is Established and be able to alert if it is not. I have attempted writing a PowerShell script for the custom plugin, but I am no expert in that area. I was hoping to return "Connected" when Established and "Disconnected" when down, then set a threshold to alert on down.

Using Telnet as an example, I was attempting to perform the following: 

Function Get-Data
{
    $Telnet = (Get-NetTCPConnection -RemoteAddress 10.0.7.115 -RemotePort 23 | select `State)
    if($Telnet = "@{State=Established}")
        {$State = "Connected"}
        else
        {$State = "Disconnected"}
    write-output $State
}

I found my if else wasn't working; when the connection was truly up, I could run Get-Data and it would return "Connected". When I severed the connection, I received the "No matching MSFT_NetTCPConnection... followed by "Connected"

Has anybody needed to perform something similar? I also thought about checking for established ports via process, but I would be in the same boat...not know how to return the correct information in the plugin script.

Like (1) Reply
Replies (2)

You are setting your variable to Established. 

I believe you want it to evaluate if it is equal.

You don't need to write a variable, just call it

 

$Telnet = ( Get-NetTCPConnection -RemoteAddress X.X.X.X -RemotePort X -ErrorAction SilentlyContinue).state
    if($Telnet -eq "Established")
        {$state = "Connected"}
        else
        {$State = "Disconnected"}
     $State

Like (0) Reply

Also, don't use telnet :P

Like (0) Reply

Was this post helpful?