What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

Powershell script output

Votes:

0

I have a straightforward need to monitor a few endpoints by whether or not the port is opened/listening. It needs to be done this way due to the nature of the tunnel and security. Simple PowerShell script does the work just fine: (IP addresses sanitized)

Test-NetConnection xxx.xxx.xxx.xxx -p 443

The output is as such:

ComputerName     : xxx.xxx.xxx.xxx
RemoteAddress    : xxx.xxx.xxx.xxx
RemotePort       : 443
InterfaceAlias   : NIC1
SourceAddress    : xxx.xxx.xxx.xxx
TcpTestSucceeded : True

All we want to alert on is is the last lie is "false"

What we get is an alert in any status and the error Response not well-formed: "( ComputerName : xxx.xxx.xxx.xxx RemoteAddress : xxx.xxx.xxx.xxx RemotePort : 443 InterfaceAlias : NIC1 SourceAddress : xxx.xxx.xxx.xxx TcpTestSucceeded : True )" (code: PE132)

alert powershell prtg

Created on Sep 21, 2021 11:34:16 PM

Last change on Sep 22, 2021 5:26:20 AM by  Florian Lesage [Paessler Support]



5 Replies

Votes:

0

Hello,

Thank you for your message.

When using a custom script with the EXE/Script or EXE/Script Advanced sensor, PRTG expects to receive a response which follows a specific format according to the sensor, that you can find in this manual: https://www.paessler.com/manuals/prtg/custom_sensors

Therefore, in the current case you could use the following for the EXE/Script sensor:

$endpoint = Test-NetConnection -ComputerName IP/DNS -port PORT -WarningAction SilentlyContinue
$value = switch($endpoint.TcpTestSucceeded){"True" {1}; "False" {0}}

if ($value -eq 1){
    Write-Output "$($value):TCP test succeeded"
}else{
    Write-Output "$($value):TCP test failed"
}

Otherwise, for the EXE/Script Advanced sensor you need to return a JSON such as the one below:

$endpoint = Test-NetConnection -ComputerName IP/DNS -port PORT -WarningAction SilentlyContinue
$value = switch($endpoint.TcpTestSucceeded){"True" {1}; "False" {0}}

Write-Output @"
{
    "prtg": {
        "result": [
            {
                "channel": "TCP Test",
                "value": $value
            }
        ]
    }
}
"@

Then, when the channel is created you can apply a custom lookup to replace the current values with meaningful information. Here is the manual which describe how to use custom lookup: https://www.paessler.com/manuals/prtg/define_lookups

Finally, you can of course improve the script above to add parameters and then pass the data via PRTG for each of your endpoints, instead of creating many scripts.

param(
    [string] $address = "",
    [int] $port = 0
)

If you have questions, let me know.

Regards.

Created on Sep 22, 2021 5:45:42 AM by  Florian Lesage [Paessler Support]



Votes:

0

Thank you so much, this has solved my issue and is very useful for us!

Created on Sep 22, 2021 5:17:30 PM



Votes:

0

Thank you very much for your feedback. Glad to hear that it works.

Have a nice day.

Created on Sep 23, 2021 5:31:58 AM by  Florian Lesage [Paessler Support]



Votes:

0

HI,

I can see this working for me when its true but it doesnt work when it fails and is false because the returned out put is different using powershell these days.

PingSucceeded  : False 

$endpoint = Test-NetConnection -ComputerName IP/DNS -port PORT -WarningAction SilentlyContinue
$value = switch($endpoint.TcpTestSucceeded){"True" {1}; "False" {0}}

if ($value -eq 1){
    Write-Output "$($value):TCP test succeeded"
}else{
    Write-Output "$($value):TCP test failed"
}

can anyone help with how to add in the false state as well.

thanks

Created on Jul 18, 2022 11:56:21 AM

Last change on Jul 18, 2022 12:51:02 PM by  Felix Wiesneth [Paessler Support]



Votes:

0

Hi Marcus,

Thank you for your message.

According to the manual of PowerShell command Test-NetConnection, the latter indeed returns PingSucceeded however this happens when a port is not defined.

When a specific port is provided, it returns a boolean value for TcpTestSucceeded: https://docs.microsoft.com/en-us/powershell/module/nettcpip/test-netconnection?view=windowsserver2022-ps.

Therefore, I invite you to execute the command below manually on the corresponding probe server (where the script is) and make sure that TcpTestSucceeded is returned properly.

Test-NetConnection -ComputerName <IP/DNS> -Port <port>

The command should provide the same output as the one visible in the first post of this article.

Regards.

Created on Jul 18, 2022 2:30:51 PM by  Florian Lesage [Paessler Support]

Last change on Jul 18, 2022 2:33:35 PM by  Florian Lesage [Paessler Support]




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.