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

EXE Script sensor returns 0 in PRTG (but returns value in PowerShell)

Votes:

0

Hi Support,

I created a PowerShell script which works and returns the correct value when executed in PowerShell ISE on the PRTG server, however when executed in PRTG it returns value 0

I've tested with both the "security context" and " Windows credentials of parent device" options on the sensor, same output.

I've also tried to save output to log, but this does not contain any information about why this fails (as far as I can see anyway).

The script (identifiable values removed)

Import-Module UpdateServices
$WSUSServer = Get-WsusServer -Name SERVERNAME.DOMAIN -PortNumber XXXX
$LastReportTime = Get-WsusComputer -UpdateServer $WSUSServer -ComputerTargetGroups "Server" -IncludeSubgroups | Sort-Object -Property LastReportedStatusTime | Where-Object {$_.LastReportedStatusTime -lt (Get-Date).adddays(-14)} | select fulldomainname, LastReportedStatusTime
$OverdueComputers = $LastReportTime.FullDomainname | Where-Object {$_.LastReportedStatusTime -lt (Get-Date).adddays(-14)}
$OverdueComputersCount = $OverdueComputers.Count

#Write result output
Write-Host "<prtg>"
Write-Host "<result>" 
"<channel>Overdue Servers</channel>"
"<value>"+ $OverdueComputersCount +"</value>" 
"</result>"
"<text>" + "" + $OverdueComputersCount + " servers are overdue to report in WSUS: " + $OverdueComputers + "</text>"
Write-Host "</prtg>"

Any hints on how to get this script to return the correct values in PRTG is much appreciated.

exe powershell zero-value

Created on Aug 27, 2021 8:15:17 AM

Last change on Aug 31, 2021 8:18:10 AM by  Florian Lesage [Paessler Support]



5 Replies

Votes:

0

Hello Martin,

Thank you for your message.

In the latest versions of PRTG, we have released new PowerShell security features which could affect the PowerShell script. To check if it's the case, I invite you to proceed with the steps below:

  • Replace Write-Host with Write-Output
Import-Module UpdateServices
$WSUSServer = Get-WsusServer -Name SERVERNAME.DOMAIN -PortNumber XXXX
$LastReportTime = Get-WsusComputer -UpdateServer $WSUSServer -ComputerTargetGroups "Server" -IncludeSubgroups | Sort-Object -Property LastReportedStatusTime | Where-Object {$_.LastReportedStatusTime -lt (Get-Date).adddays(-14)} | select fulldomainname, LastReportedStatusTime
$OverdueComputers = $LastReportTime.FullDomainname | Where-Object {$_.LastReportedStatusTime -lt (Get-Date).adddays(-14)}
$OverdueComputersCount = $OverdueComputers.Count

#Write result output
Write-Output @"
<prtg>
<result>
<channel>Overdue Servers</channel>
<value>$($OverdueComputersCount)</value> 
</result>
<text>$($OverdueComputersCount) servers are overdue to report in WSUS: $($OverdueComputers)</text>
</prtg>
"@
  • Disable new PowerShell security feature In case the first option didn't work, you can try to disable the feature under Setup > System Administration > Monitoring (/systemsetup.htm?tabid=2) > Experimental Features | PowerShell Security Enhancement.

Then, execute new scan of the existing sensor and/or create a new sensor with the corresponding script.

  • Return execution error in sensor message If an error occurs during the execution of the script (not visible on the web interface), adding a Try catch could help to return it in the sensor message and check where it could come from. Here is a piece of code you can use for that:
try {
# your code
} catch {
    write-output @"
{
"prtg": {
	"error": 1,
	"text": "$($_.exception.message) At line : $($_.InvocationInfo.ScriptLineNumber)"
}
"@
}

If you have questions, let me know.

Regards.

Created on Aug 31, 2021 8:46:53 AM by  Florian Lesage [Paessler Support]

Last change on Sep 6, 2021 9:13:11 AM by  Florian Lesage [Paessler Support]



Votes:

0

Hi Florian,

Thanks for your reply on this issue.

Regarding your solution proposals:

- Modifying the script as suggested with "Write-Output" did not change anything, script still works when executed in ISE, but returns 0 when executed in PRTG.

- PowerShell Security Enhancement is already disabled in our configuration.

As for the "try/catch", I can't get this to work, by replacing the "# your code" bit with my code, but I'm properly doing it wrong. When I try this it returns with "Missing closing '}' in statement block or type definition"

Any addition troubleshooting advise I can try would be much apriciated.

Created on Aug 31, 2021 10:19:55 AM



Votes:

0

Hi Martin,

Thank you for your feedback.

Missing braces

I indeed noticed the missing curly bracket "}" and added it in the previous post (missing at the end of the code). Please, add it and check if there is any error message displayed (in the sensor message field) when executing a manual scan.

Sensor debug logs

I also invite you to enable the sensor log files by selecting the option Store result in the Settings tab. For the next scans, PRTG will generate the debug files in the Logs\sensors folder (located by default under "C:\ProgramData\Paessler\PRTG Network Monitor\Logs\sensors"). Note that the path of the data folder can be different and therefore I invite you to open PRTG Administration Tool on the corresponding server to check if it's the case.

Then, open the log file (ending with .txt only) which has the ID of the sensor. Which data do you get there ?

Regards.

Created on Sep 1, 2021 7:55:33 AM by  Florian Lesage [Paessler Support]

Last change on Sep 1, 2021 7:57:22 AM by  Florian Lesage [Paessler Support]



Votes:

0

Hi again Florian,

Thanks for the updated script, the "try/catch" script now works, and returns this:

Response not well-formed: "({ "prtg": { "error": 1 "text": "Access to the path 'Update Services' is denied. At line : 3" } )" (code: PE132)

This indicates access denied to some path, which is likely the "C:\Program Files\Update Services" folder on the WSUS server, however if access right privileges on this is the cause, why does the script return valid values when executed from a non-administrative Powershell instance from the PRTG server?, it is only from within PRTG this seems to fail and return 0 values.

So while I can't rule out any corrupt folder/file access privileges on the WSUS server, I doubt this is the root cause, as the user PRTG is set to use actually has administrative access to the server.

Created on Sep 2, 2021 2:19:32 PM



Votes:

0

Hi Martin,

I apologize for the delay.

Can you please share the script without confidential information to have a look at it. Do you execute a command remotely by using Invoke-Command by any chance ? If that's the case, can you check if providing the credentials explicitly works, by following the steps below:

  • Pass the credentials of the device to the script by adding the following in the Parameters field of the sensor Settings tab:
"%windowsuser" "%windowspassword"
  • Get the credentials in the script by adding params there
param(
    [string] $windowsuser,
    [string] $windowspassword
)

The script should then use the credentials defined in the parent device settings and should not encounter issue if the account has administrator rights.

Created on Sep 6, 2021 9:40:30 AM 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.