Trying to get PRTG to execute the below powershell script under a Custom EXE/XML Advanced sensor. Never get a result in PRTG for 'State', however the script runs fine in powershell and receives the below successful output when run in PS;
(NOTE - 6=good, 7=fail)
***PS C:\<PRTGDir>\EXEXML> .\execute-remote-task-no-function.ps1 -RemoteSvr XXX -DestPing xxx.xxx.xxx.xxx <prtg> <result> <channel>State</channel> <value>6</value> </result> </prtg>***
With 'Write results to disk..' on, I get the below error sometimes in the log, it's my only clue to the issue;
***"This sensor requires the powershell 2.0 (or higher) to be installed on the probe system. (new-object : The term 'new-object' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:36 + if ($PSVersionTable.PSVersion -ge (new-object 'Version' 5,0)) { Import-Module Mi ... + ~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (new-object:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException get-content : The term 'get-content' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\Program Files (x86)\PRTG Network Monitor\custom sensors\EXEXML\execute-remote-task-no-function.ps1:13 char:13 + $password = get-content "$secure\svdiautomation.pwd" | convertto-securestring + ~~~~~~~~~~~ + C) (code: PE181)"***
The Custom EXE/XML Advanced sensor is configured with the below parameters to pass to the script;
*'-RemoteSvr au2106sp0339 -DestPing 10.9.212.42'*
The script works absolutely fine, when I launch it directly from Powershell under 'Run-as' the same Windows account used on the sensor's parent for 'Windows Systems'.
Script below;
***param([string]$remotesvr,[string]$pingdest) #=============================================================================================================================================== # Script Purpose: | Execute tasks on remote servers, retrieve data # Date Implemented: | 03/03/2016 # Last Modified: | 03/03/2016 # Version: | 0.1 # Author: | XXXXX # Contact: | XXXXXXX #=============================================================================================================================================== #Global Variables $secure = 'd:\scripts\secure\' $userid = "XXXXXXXXXXXX" $password = get-content "$secure\xxxxxxxxx.pwd" | convertto-securestring $credential = New-Object System.Management.Automation.PsCredential $userid, $password #$remotesvr = "XXXXXXXXXX" #$destping = "xxx.xxx.xxxx.xxxx" #=============================================================================================================================================== #Execute $session = New-PSSession -ComputerName $remotesvr -Credential $credential $result = Invoke-Command -Session $session -Scriptblock {Test-Connection -Computername 1"$($args[0])" -count 1 -quiet} -Argumentlist $pingdest Remove-PsSession $session.id if ($result -eq $true) {$value = "6"} else {$value = "7"} echo "<prtg>" echo "<result>" echo "<channel>State</channel>" echo "<value>$value</value>" echo "</result>" echo "</prtg>"***
Environment: Win2k8 with Powershell 3.0 PRTG: 16.1.21.1422
Article Comments
Hi Andrew,
Thanks for the reply, as I mentioned in my post - the script runs perfectly when I execute it in powershell using the same user account.
There is some problem only when run under PRTG, using the exact same credentials.
Can you explain this part please "Next writes something to a local file (so its not doing anything like powershell remoting)", you mean write the result into a text file? Can you explain "powershell remoting", i'm not familiar with that term. Writing to a file is not an option, it's too inefficient and we have AV scanning which scans on write, so it slows right down.
Will have a look at the page you referenced and also running it under a Standard sensor.
Cheers, Patrick
Mar, 2016 - Permalink
Patrick,
Powershell Remoting is just getting Powershell on one machine to run a command on another machine. Your code above is doing this with "Invoke-Command"
My suggestion was just to narrow down where the problem may lay.
To expand on what I wrote before, you might wish you try to disable the remoting (Invoke-Command) feature, and simply have a test script that just writes "hello world" to a file. Or even got a step further and just a script that returns "6:OK" .All this is just to make sure the basic's work, then you can go back to add one bit of code at a time until everything works.
Another question, can you run any powershell scripts?
And remember, scripts only get run on the probes, so from your probe (or core) server, can you run this script?
Mar, 2016 - Permalink
It seems that you can't use this line:
Invoke-Command -Session $session -Scriptblock
You have to connect remotely differently. This work for Exchange:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $CURI -Authentication Kerberos Import-PSSession $Session -DisableNameChecking
I'm trying to connect by specifying a neutral string so I can connect to any server. Please any help would be appreciated.
May, 2016 - Permalink
Have a look at this page - https://thedomainiown.wordpress.com/prtg-related/prtg-script-remote-portping/ It shows another way to do what you are trying (except it does not ping, it tests a port). This example takes the login cred's out of PRTG so you dont need to store them in a separate file.
As for why your code does not work. I am not sure I can help.
Try going back to basic's and just have a Powershell script that returns: "6:OK" Make sure that runs
Next writes something to a local file (so its not doing anything like powershell remoting)
Try running this under the standard custom exe sensor, since it only returns one value (at least in this example).
Hope something above helps.
Mar, 2016 - Permalink