The following script is working on the PRTG server in a power shell without problems but from PRTG (custom sensor) not. Please assist. Thank you.

--------------------------------------

[string]$status = ""
[string]$result = ""

if ($args.count -eq 2) {
	[string]$server = $args[0]
	[string]$queue = $args[1]
	if ($server.Contains(".")) {
		#remove domain part from name
		[string]$queueName = $server.Substring(0, $server.IndexOf(".")) + "\" + $queue
	}
}
else {
	$status = "Missing parameters Server Queue"
}

if ($status -eq "") {
	Try {
		$objItem = Get-Queue -server $server | Where-object {$_.Identity -eq $queueName} | Select-object MessageCount
		if ($objItem -is [Object]) {
			$result = $objItem.MessageCount.ToString()
			$result = $result.Trim()
		}
		else {
			$status = "No results for Server=" + $server + ", Queue=" + $queue
		}
	}
	Catch {
		$status = "Executing Get-Queue Server=" + $server + ", Queue=" + $queueName
	}	
}

if ($status.Length -eq 0) {
	write-host $result, ":OK"
	exit 0
}
else {
	$status = "0:ERROR " + $status
	write-host $status
	exit 1
}

Article Comments

Hello,

which error do you get in PRTG? Is this a Powershell 1 script? On which Windows is PRTG installed?

Best Regards.


Jan, 2011 - Permalink

Hi,

PRTG Error: "ERROR Executing Get-Queue Server=EXFEVPS01.99eight.com, Queue=EXFEVPS01\Submission" (see Catch in Try statement of above script)

PRTG Server: Windows 2003 SP2 with latest updates, Windows Management Framework and Exchange 2007 Mgmt Tools installed, checked that Exchnage snap-ins are registered

Powershell version: v2

Many thanks, Jurgen


Jan, 2011 - Permalink

Dear Jürgen,

Powershell 2 is not yet supported by PRTG I'm afraid. That is most likely the reason.

Best Regards.


Jan, 2011 - Permalink

Is version 2 of powershell still not supported in the latest version? I would like to run a similar script since exchange 2007/2010 queues are no longer handled by WMI. You guys have to keep the software updated so we can monitor efficiently.


May, 2012 - Permalink

Yes, Powershell v2 is unfortunately still not supported, but seeing as this was the first request for it since over one year, the priority is not very high. Sorry.


May, 2012 - Permalink

Well I am having a similar issue to this. I am running the command "%SystemRoot%\system32\WindowsPowerShell\v1.0\powerShell.exe -PSConsoleFile ""C:\program files\Microsoft\Exchange Server\v14\bin\exshell.psc1" and then running the command Start-Transcript c:\shelloutput.txt; $MessageCount = get-queue -server 01dalsvmowa2 | foreach -begin {$total=0} -process {$total+=$_.messageCount} -end {$total}; exit $MessageCount; Stop-Transcript; from the powershell. It works when I run it manually, but when I go through PRTG it doesn't run at all. I have the same credentials that I used to run manually and everything else looks the same. Any ideas why it wouldn't run correctly?


May, 2012 - Permalink

Maybe running the PRTG Probe Service under your own account does help?


May, 2012 - Permalink

To be able to run Powershell scripts it is necessary to modify the ExecutionPolicy of Powershell from its default 'restricted' value to at least 'remotesigned'. This can be done from a Powershell-prompt with the command: 'Set-ExecutionPolicy RemoteSigned'

Because PRTG is still a 32bit programm it starts the 32bit vorsion of powershell even if you are running a 64bit system.
To enable Powershell-scripting from PRTG on such systems it is necessary to set the execution policy in a different way. Start the Commandline with adminrights and execute the following command:
c:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe "Set-ExecutionPolicy RemoteSigned"

For a complete list and descriptions of the options please refer to http://www.windowsecurity.com/articles/PowerShell-Security.html -> Going behind the defaults

If you are trying to get values via Powershell from a remote machine keep in mind that PRTG is running as a service in the 'system'-context. You can manually set the security context in the sensor settings or use 'New-PSSession' which can connect to the remote machine with alternative credentials.

With kind regards


Jun, 2012 - Permalink