New Question
 
 
PRTG Network Monitor

Intuitive to Use.
Easy to manage.

200.000 administrators have chosen PRTG to monitor their network. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free PRTG
Download >>

 

What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general. You are invited to get involved by asking and answering questions!

Learn more

 

Top Tags


View all Tags


Attempting Invoke-Restmethod as custom exe sensor (PS1) receiving error

Votes:

0

Your Vote:

Up

Down

Having issues. When I run the following script on a system outside of PRTG, everything works as expected:

param (
        [string] $UserName = "username!",
        [string] $Pass = "password!"
)
$Pass = ConvertTo-SecureString $Pass -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ($UserName, $Pass)

$R = Invoke-RestMethod -Uri "http://WEBSERVER" -Method Get -UseBasicParsing -Credential $cred | Select-Object "messages*"

The expected return looks like this:

messages_persistent             : 0
messages_unacknowledged_ram     : 0
messages_ready_ram              : 0
messages_ram                    : 0
messages_unacknowledged_details : @{rate=0.6}
messages_unacknowledged         : 0
messages_ready_details          : @{rate=0.0}
messages_ready                  : 0
messages_details                : @{rate=0.6}
messages                        : 0

It returns as a PSObject.

When I run this same script in PRTG, I get the following error:

sensors\EXEXML\Get-ProdMessages.ps1:7 char:9
+ $cred = New-Object Management.Automation.PSCredential ($UserName, $Pa ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodExcept 
   ion
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power 
   Shell.Commands.NewObjectCommand
 
Invoke-RestMethod : {"error":"Unauthorized","reason":"\"Unauthorized\"\n"}
At C:\Program Files (x86)\PRTG Network Monitor\custom 
sensors\EXEXML\Get-ProdMessages.ps1:9 char:6
+ $R = Invoke-RestMethod -Uri "WEBSERVER ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:Htt 
   pWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShe 
   ll.Commands.InvokeRestMethodCommand

What can I do to get this to work?

custom-script-exe custom-sensor powershell

Created on Feb 26, 2019 10:10:48 PM by  Rob Dunn (10) 1

Last change on Feb 27, 2019 2:08:21 PM by  Dariusz Gorka [Paessler Support]



4 Replies

Votes:

0

Your Vote:

Up

Down

Hi there,

How do you fill the parameters from within PRTG or are these predefined in the script itself?

Best regards.

Created on Feb 27, 2019 11:30:08 AM by  Dariusz Gorka [Paessler Support]



Votes:

0

Your Vote:

Up

Down

They are predefined within the script itself, at least at this time...

Thanks for responding!

Created on Feb 27, 2019 1:27:25 PM by  Rob Dunn (10) 1



Votes:

0

Your Vote:

Up

Down

Interesting thing, I switched the PS1 to this:

param (
        [string] $user = "user1",
        [string] $Pass = "password1"
)
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"

$Headers = @{
        Authorization = $basicAuthValue
    }
$R = Invoke-RestMethod -Uri "http://WEBSERVER" -Method Get -UseBasicParsing -Headers $Headers | Select-Object "messages*"

Tested in the PowerShell console, it works fine. In PRTG, I get the following:

Invoke-RestMethod : {"error":"Object Not Found","reason":"\"Not Found\"\n"}
At C:\Program Files (x86)\PRTG Network Monitor\custom 
sensors\EXEXML\Get-ProdMessages.ps1:12 char:6
+ $R = Invoke-RestMethod -Uri "http://WEBSERVER ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:Htt 
   pWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShe 
   ll.Commands.InvokeRestMethodCommand

Created on Feb 27, 2019 2:17:18 PM by  Rob Dunn (10) 1

Last change on Feb 27, 2019 2:28:35 PM by  Dariusz Gorka [Paessler Support]



Votes:

1

Your Vote:

Up

Down

Well, I ran into two things...

One, the Internet Explorer First Run Wizard needed to be run or configured to be disabled via GPO.

The other issue was that I was running into an escape character problem. The actual URL has %2F in it - something you wouldn't know since I neutered the info in the link for the example above.

Using this solution: https://stackoverflow.com/a/30927141 and a little JSON conversion magic has done the trick for me. I'm able to now return data for the custom sensor!

Created on Feb 27, 2019 4:17:50 PM by  Rob Dunn (10) 1



Please log in or register to enter your reply.


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.