Missing expression after unary opearator '-'. At line:1 char
Posted: Mon Aug 05, 2013 11:22 pm
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
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