Hi,
I am facing some peculiar issue in PowerShell 2.0. I am monitoring Exchange 2010 server.I am not an expert in PS but occasionally write/edit few scripts to work with Nagios monitoring tool. Requesting help form PS experts. Your help will be appreciated.
The script ExServiceAlert10.ps1 is attached in this post. The script is working fine and shows no error, if I directly execute it in PowerShell command like below.
PS C:\Windows\system32> D:\ExServiceAlert10.ps1
But shows error when execute in Normal windows command prompt or from Nagios NSClient++ like below.
Normal command Prompt execution: (Script placed at D:)
D:\>echo .\ExServiceAlert10.ps1 ; exit $LastExitCode | powershell.exe -command -
From nagios NSclient++ execution via check_nrpe: (Script placed inside script folder of NSClient++)
cmd /c echo scripts\ExServiceAlert10.ps1; exit $LastExitCode | powershell.exe -command –
The error I am getting by the above 2 ways is as follows.
Missing expression after unary opearator ‘-’.
At line:1 char:2
+ – <<<<
+ CategoryInfo : ParseError: (-:String) [], ParentContainsErrorRecordException
+FullyQualifiedErrorId : MissingExpressionAfterOperator
I am executing this script in PowerShell 2.0 and tried various debugging methods/ posted other forums to solve this for past 1 week with no success.
Besides these I have 7-8 more PS scripts following that format and all are running without any issue except this reported one. Sometimes back I checked without that dash in command line and found scripts were not working in nagios without that dash. Here are some entries from my NSClient++ conf. file NSC.ini for other PS scripts which are working fine.
exch_mail_flow10=cmd /c echo scripts\ExchMailFlow10.ps1; exit $LastExitCode | powershell.exe -command -
exch_mailboxhealth10=cmd /c echo scripts\ExMailboxhealth10.ps1; exit $LastExitCode | powershell.exe -command –
Even the reported errant script (ExServiceAlert10.ps1) is working fine in my test system with that extra dash in command prompt but not working at all in any of the the Prod. system. All the system’s PS versions are 2.0.
I am thinking either I have to enable/disable some PS environment settings on those errant Prod. systems or to escape something inside the script (suspecting newline characters- `n backtick n) . I don’t understand why it is reporting Line:1 and char:2, which is nothing but a comment line in the script.
Also attached here is the NSC.ini config file.
I have seen this particular error reported by few others and their issue resolved after upgrading their PS to Ver 2.0. Mine is already 2.0 and don’t know what to do next.
Please let me know if any further information required here.
Thank you in advance.
–Santosh
Missing expression after unary opearator '-'. At line:1 char
Missing expression after unary opearator '-'. At line:1 char
- Attachments
-
- NSC.ini.txt
- NSclient++ config file
- (18.73 KiB) Downloaded 346 times
-
- ExServiceAlert10.ps1.txt
- My errant script
- (1.77 KiB) Downloaded 337 times
-
- -fno-stack-protector
- Posts: 4366
- Joined: Mon Nov 19, 2012 12:10 pm
Re: Missing expression after unary opearator '-'. At line:1
If you are seeing this also via command line on the windows machine and not exclusively by nrpe. Then this is an issue with how the command is being executed on cmd. I can say that with some minor testing, I was able to run the check just like below:
cd c:\users\admin\
echo desktop\exchangetest.ps1; exit $LastExitCode | powershell.exe -command -
This returned the exact same results as via powershell directly. Just to validate, the nsclient\nrpe users in services.msc do have rights to this script? Also as a slightly side note, there is no escaping or stripping being done. Since you have this configured via nrpe\nsclient and are calling it locally. Also the echo, only echos the name of the file to powershell, not the entire script.
cd c:\users\admin\
echo desktop\exchangetest.ps1; exit $LastExitCode | powershell.exe -command -
This returned the exact same results as via powershell directly. Just to validate, the nsclient\nrpe users in services.msc do have rights to this script? Also as a slightly side note, there is no escaping or stripping being done. Since you have this configured via nrpe\nsclient and are calling it locally. Also the echo, only echos the name of the file to powershell, not the entire script.
Nagios-Plugins maintainer exclusively, unless you have other C language bugs with open-source nagios projects, then I am happy to help! Please pm or use other communication to alert me to issues as I no longer track the forum.
Re: Missing expression after unary opearator '-'. At line:1
Thanks sreinhardt for the reply and appreciating the valuable time spent by you to verify it in your setup.sreinhardt wrote:If you are seeing this also via command line on the windows machine and not exclusively by nrpe. Then this is an issue with how the command is being executed on cmd. I can say that with some minor testing, I was able to run the check just like below:
cd c:\users\admin\
echo desktop\exchangetest.ps1; exit $LastExitCode | powershell.exe -command -
This returned the exact same results as via powershell directly. Just to validate, the nsclient\nrpe users in services.msc do have rights to this script? Also as a slightly side note, there is no escaping or stripping being done. Since you have this configured via nrpe\nsclient and are calling it locally. Also the echo, only echos the name of the file to powershell, not the entire script.
At the end I am able to crack the issue and it was very difficult to find. Our administartor copied/pasted the command line for NSC.ini file from a MS-Word document (How To doc.) shared by me to them.
In the original MS-Word doc the hypen (at the end of the line after the word command in the beow given command line) has automatically changed to a little bigger hypen during documentation, which also gone to the NSC.ini file and hence the error was coming for this particular command execution. After changing the hypen manually in the NSC.ini, it worked fine without any issues.
Code: Select all
exch_service_alert10=cmd /c echo scripts\ExServiceAlert10.ps1; exit $LastExitCode | powershell.exe -command –
Santosh
-
- -fno-stack-protector
- Posts: 4366
- Joined: Mon Nov 19, 2012 12:10 pm
Re: Missing expression after unary opearator '-'. At line:1
OH NO, that would definitely do it, good old ascii formatting! Glad you figured it out though.Our administartor copied/pasted the command line for NSC.ini file from a MS-Word document
Nagios-Plugins maintainer exclusively, unless you have other C language bugs with open-source nagios projects, then I am happy to help! Please pm or use other communication to alert me to issues as I no longer track the forum.