Hi Guys,
i´ve got an problem regarding an Custom Sensor in Prtg.
I use the following Powershell-Script to get information from an webpage and to trim it to a short output.
the script:
$webpage=Invoke-WebRequest "private ip-address" $webpage.RawContent | Out-File "C:\Skripte\Tank_S3\webpage_Fuellstand.txt" ASCII -Width 9999 $input_path = "C:\Skripte\Tank_S3\webpage_Fuellstand.txt" $output_file2 = "C:\Skripte\Tank_S3\Fuellstand.txt" $regex = '([0-9]{1,2}%)' $string_fuellstand = Select-String -Path $input_path -Pattern $regex -list | % { $_.Matches } | % { $_.Value } $string_fuellstand = $string_fuellstand.Remove(2) $text = $string_fuellstand + ":Wert ermittelt." $string_fuellstand = $string_fuellstand + ";" + $(get-date).toString('yyyy-MM-dd;HHmmss') $string_fuellstand >> $output_file2 write-host $text
It works fine as long as an User is logged into the PRTG-Computer.
This is the output while someone is logged in:
80;2017-04-12;103427
If no one is logged in, following output is generated:
;2017-04-12;103915
I´ve already tried to give specific access rights to the sensor. The security is set to use the access data of the parenting device.
I´m now a little bit confused because the script alone works if set in the task scheduler without anyone logged in.
Am I missing some point?
Kind regards,
Daniel
Article Comments
Hello Daniel,
thank you for your reply.
The original version in the original post is more than fine. :)
I'm not exactly sure how the logged user could be making a difference. Are any sort of credentials required when you do the Invoke-WebRequest "private ip-address" ?
As for the issue, this does normally have to do with permissions. Keep in mind that the PRTG Probe Service which runs custom sensors uses the LOCAL_SYSTEM account by default. You could use services.msc to change this and specify a different account, does the behavior when running the script change?
Also, when you run the script via a scheduled task, which user account is used to run the script?
Something else you can try to track down the issue is to set the "EXE Result" to "Write EXE result to disk" within the Sensor's settings. Once configured to do so a resulting Text File (Filename: 'Result of Sensor [ID].txt') will be stored under the Logs (Sensors) directory on the next scan of the sensor. This folder's path is by default "C:\ProgramData\Paessler\PRTG Network Monitor" of the probe where the sensor is monitored. This log may tell you more about what happens during the sensor's execution.
Also, on a side note, why do you store the file to disk before processing it further? Have you tried something like this?
$webpage = Invoke-WebRequest -Uri "http://fqdn/url" -UseBasicParsing $regex = '([0-9]{1,2}%)' $string_fuellstand = Select-String -InputObject $webpage.RawContent -Pattern $regex -list | % { $_.Matches } | % { $_.Value } $string_fuellstand = $string_fuellstand.Remove(2) $text = $string_fuellstand + ":Wert ermittelt." $string_fuellstand = $string_fuellstand + ";" + $(get-date).toString('yyyy-MM-dd;HHmmss') write-host $text
Note: I haven't tested the code above as I don't know how the input looks like.
Best Regards,
Apr, 2017 - Permalink
Hi Luciano,
the webrequest leads to an "open" website in our network. You can log in, but the crucial information that the sensor displays is without credentials.
If i change the account to server-admin i get following error-message: "Dem Client fehlt ein erforderliches Recht. (0x522)".
So i set it back to system-account.
Your hint to track down the problem with the sensor-settings to "write the result to disk" was quite good.
I´m not quite sure how much you will understand if i post the german version:
"Invoke-WebRequest : Der Antwortinhalt kann nicht analysiert werden, da das Internet Explorer-Modul nicht verfügbar ist, oder die Konfiguration beim ersten Start von Internet Explorer ist nicht abgeschlossen. Geben Sie den UseBasicParsing-Parameter an, und wiederholen Sie den Vorgang. "
Essentialy: PRTG cant get anything out of the script because the IE-modul is not available.
I edited my Script and used the -usebasicparsing switch and now everything works fine.
Thank you for your help and happy Easter!
Kind regards Daniel
Apr, 2017 - Permalink
Hi Luciano,
your hint with "writing exe result to disk" worked. Prtg couldn´t call the IE modul while logged off.
So i used your -usebasicparsing switch.
And now everything works just fine.
Thank you for your help.
Kind regards Daniel
Apr, 2017 - Permalink
for some reasons i couldn´t edit my post. So, for better readability:
Kind regards,
Daniel
Apr, 2017 - Permalink