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?


Article Comments

Attention: This article is a record of a conversation with the Paessler support team. The information in this conversation is not updated to preserve the historical record. As a result, some of the information or recommendations in this conversation might be out of date.

Hi there,

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

Best regards.


Feb, 2019 - Permalink

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

Thanks for responding!


Feb, 2019 - Permalink

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

Feb, 2019 - Permalink

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!


Feb, 2019 - Permalink