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 save output of batch file to variable

Votes:

0

Hi,

I am trying to save the output of a batch file to a variable, this is what I do:

$output = & "C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXE\check_emc_hw.bat"

The problem is, that when I run the script in powershell on the probe, $output contains the output of the check_emc_hw.bat batch.

If I run the script within a PRTG sensor, the $output is empty.

If I try the following, $output is empty, as well. & "C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXE\check_emc_hw.bat" | tee-Object -Variable output | Out-Null

Probe OS is Server 2008 R2, Powershell version is 2.0 and the probe version is V16.2.24.4666

Can anybody tell me, why Prtg won't save the output to $output?

Thanks,

Matthias

powershell program-output prtg

Created on Sep 5, 2016 11:51:28 AM

Last change on Sep 6, 2016 5:27:30 AM by  Luciano Lingnau [Paessler]



9 Replies

Votes:

0

Dear user

There are a few differences when using Powershell via PRTG:

  • Unless you use the security context of the sensor's parent device, the shell runs with the local system user. This can affect the output.
  • Since the PRTG probe is a 32 bit service, it starts the 32 bit Powershell. Script execution (Set-ExecutionPolicy) must be set for this Powershell version as well.
  • Because the PRTG service has no GUI context, it cannot run anything accessing the GUI. However, file output itself is possible.

Please check if you can isolate the issue.

Created on Sep 6, 2016 11:30:38 AM by  Arne Seifert [Paessler Support]



Votes:

0

Hi,

thanks for your answer, I already considered these things.

I used local system as well as the credentials of the parent device, both had the same result. (Although security context should not matter, as this script only uses the Navseccli to connect to our EMC Storage)

I tested the Script in both 32 and 64bit powershell, both worked for me (on the command line).

The script does not access any gui objects, it can be run totally headless.

I am kind of stuck here, because, at the moment, I don't see any particular reason, why the script should not work. Are there any differences, how PRTG runs powershell scripts (besides the ones you mentioned)?

Thanks,

Matthias

Created on Sep 6, 2016 1:00:35 PM



Votes:

0

Dear user

Please expand your script to include more extensive logging. Please also enable the "Write Exe result to disk" option in the sensor settings and review the according log file. It is usually located in C:\ProgramData\Paessler\PRTG Network Monitor\Logs (Sensors). Are you getting any useful information?

Created on Sep 6, 2016 6:53:47 PM by  Arne Seifert [Paessler Support]



Votes:

0

Hi,

i changed the powershell script to only run the bactch file and not to save the output to $output.

It seems like the batch file is not run at all because in the sensor logs I get zero output.

I checked permissions and Local System has rights to execute the script.

So, the batch file is not executed at all, but I can't find a reason why in the logs.

BR,

Matthias

Created on Sep 7, 2016 7:03:11 AM



Votes:

0

Dear user

Which sensor message are you getting in PRTG?

Created on Sep 7, 2016 9:13:46 AM by  Arne Seifert [Paessler Support]



Votes:

0

There are no sensor error messages, this is the output of the sensor from the log files:

OUTPUT 0:Hardware OK Result:

You can find the script here: http://pastebin.com/CqwhQwk6 (If you want, I can paste it here, as well!)

KR,

Matthias

Created on Sep 7, 2016 12:29:37 PM



Votes:

0

Dear user

The script seems to have multiple statements which output something to the console. Please make sure that during the program execution, just one single line is written to the console, in the format

value:message

Are you now getting the desired result? If not, please do it reversely: Begin with just a fixed output, like

write-host "1:Okay"

and work from there, expanding the script step by step.

Created on Sep 7, 2016 3:45:33 PM by  Arne Seifert [Paessler Support]



Votes:

0

Hello,

these multiple outputs were for debugging reasons (I tried to look into the sensor logs, if $output contains something, which was not the case).

In the beginning, the script output only occured after "' PRTG Sensor condition", but it did not work, because $output doesn't contain anything.

For some strange reason, PRTG does not run the batch script at all...is there some kind of security setting, which prevents the script to run, when used in a powershell sensor?

Created on Sep 12, 2016 7:20:24 AM



Votes:

0

Dear user

Please contact [email protected] Please describe the issue and include screenshots of the Exe/Script sensor. We need

  • The sensor overview tab
  • The sensor logs tab
  • The sensor settings (if necessary, multiple screenshots to cover all settings)

Please capture the complete PRTG interface in any screenshot, as a cut-out removes important context.

Created on Sep 12, 2016 10:43:15 AM by  Arne Seifert [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.