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 if elseif

Votes:

0

Hi, I have a pretty straight forward Powershell script:

$variable = do something

If ($variable -eq "Something 1"){
write-host "0:All Good"
exit 0}
Elseif ($variable -eq "Something 2"){
write-host "1:Not so good"
exit 1}
Else {exit 1}

If I run such a script directly on a server, the if/elseif's are processed correctly.

If I run this same script as a Custom EXE sensor, I never seem to get past the first IF statement.

Any ideas?

elseif if powershell

Created on Aug 18, 2015 10:47:05 AM

Last change on Aug 18, 2015 10:57:39 AM by  Stephan Linke [Paessler Support]



9 Replies

Votes:

0

What errors do you get? Or are you stuck with All is good?

Created on Aug 18, 2015 10:58:13 AM by  Stephan Linke [Paessler Support]



Votes:

0

No errors. If the $variable equals something that should trigger the ELSEIF statement, PRTG persists on returning "All is good". The sensor is green and responding like nothing is wrong.

If I run it directly on the server being monitored, powershell correctly reports "Not so good" indicating that the logic of the script is correct.

Created on Aug 18, 2015 11:17:12 PM



Votes:

0

What parameters do you pass to it in the sensor configuration?

Created on Aug 19, 2015 5:56:37 AM by  Stephan Linke [Paessler Support]



Votes:

0

Nothing but -hostname %host. Here's the script:

param (
	[string]$hostname
	)

$LogonMode = Invoke-Command { Add-PSSnapin Citrix.XenApp.Commands; $(Get-XAServer -ServerName $hostname).LogonMode } -ComputerName $hostname | Select -Expand Value

If ($LogonMode -eq "AllowLogOns")
{write-host "0:All Logons Allowed"
exit 0}
ElseIf ($LogonMode -eq "ProhibitNewLogOnsUntilRestart")
{write-host "1:Prohibit Logons Until Server Restart"
exit 1}
ElseIf ($LogonMode -eq "ProhibitNewLogOns")
{write-host "2:Prohibit Logons Only"
exit 1}
ElseIf ($LogonMode -eq "ProhibitLogOns")
{write-host "3:Prohibit Logons and Reconnections"
exit 1}
Else
{exit 1}

It's simply identifying the logon mode of a XenApp server. If I run this directly on a XenApp server, the ELSEIF's are processed correctly according to its actual logon mode.

When prtg runs it, it constantly stops at "0:All Logons Allowed".

Created on Aug 19, 2015 6:04:34 AM



Votes:

0

Hi Stan,

Can you try the following: http://pastebin.com/raw.php?i=spaxSS4e It works, when I set the logonmode manually to the corresponding strings. Also, looks a bit better than the if statements :)

Created on Aug 19, 2015 9:41:42 AM by  Stephan Linke [Paessler Support]



Votes:

0

It works...but something doesn't like the $hostname variable.

If I specify "-hostname %host" in the parameters field of the sensor, it always returns back 'Allow logons'.

If I edit the .ps1 and replace $hostname with the name of the actual server, the sensor works as expected.

Created on Aug 20, 2015 5:59:45 AM



Votes:

0

Strange...what's the actual hostname?

Created on Aug 20, 2015 7:25:02 AM by  Stephan Linke [Paessler Support]



Votes:

0

Call it "server1".

I have some other sensors which use the invoke-command to run other XenApp commands, these too also use the $hostname variable in the script. I pass it via the same "-hostname %host" parameter in the sensor without error.

Created on Aug 20, 2015 10:19:42 AM



Votes:

0

Hm...really not sure what's causing this - What's the content of $LogonMode - is it possible that its a PSObject and you'll need to access a property of it, not the object itself? That should explain, why it's stuck on the first...

Created on Aug 21, 2015 6:51:03 AM by  Stephan Linke [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.