Missing expression after unary opearator '-'. At line:1 char:2

I am facing some peculiar issue in PowerShell 2.0. 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:
D:>echo .\ExServiceAlert10.ps1 ; exit $LastExitCode | powershell.exe -command -

From nagios NSclient++ execution via check_nrpe:
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 to solve this for past 1 week with no success.

Please let me know if any further information required here.
Thank you in advance.


Since .ps1 is not going as an attachment, i’m directly entering the script content below.

#First, findout if Exchange Management Shell is loaded:
$snapins = Get-PSSnapin |select name
$found = $false
$founDName = $snapins[$snapincount].name
if ($founDName -eq “Microsoft.Exchange.Management.PowerShell.E2010”)
#Exchange Shell already loaded
$found = $True
while ($snapincount -lt $snapins.Count)

if ($found -ne $True)
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

Create variables

$status = 0
$desc = “”

Get Service Health

Test-ServiceHealth | ForEach-Object {
$main = “n&quot; + &quot;Role: &quot; + $_.Role + &quot;n” + "Status: " + $.RequiredServicesRunning + “n" if ($_.RequiredServicesRunning -eq "True"){ $array = $_.ServicesRunning $runningsvcs = "Services running:" foreach ($svc in $array){ $runningsvcs = $runningsvcs + " " + $svc } $desc += $main +$runningsvcs + "n”
$status = 1
$array = $
$notrunning = “Services Not running”
foreach ($svc in $array){
$notrunning = $notrunning + " " + $svc
$desc += $main + $notrunning

if ($status -eq 1){
echo “Critical - Exchange Services Alert $desc”
exit 2
echo “OK - Exchange Services Alert $desc”
exit 0

Scripts must be attached at TXT.

I don’t think the error is in your script. It’s how you’re calling PowerShell.exe. Above, you used its -command parameter, followed by a dash. What was the purpose of that? Powershell is interpreting it as an operator.

Thank you Don!

Next time on, I will attach the script as .txt file and apologized for the mistake.

This script is used by nagios monitoring tool agent NSClient++. It has a configuration file NSC.ini, which says to add the PS scripts like the way I have given (extra dash at the end). 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 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.

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.

Yeah, don’t know what to tell you - I’m not familiar with that system. If the script works standalone, then the problem is how your system is running it. I know the -command - isn’t good syntax; it’s sending the command “-” to PowerShell, which it will see as an operator. Which is where the error is coming from. I don’t think the error is referring to a line in your script; I think it’s referring to the -command. Normally, if you’d run a script, you’d do it with -FilePath, not with -Command. But if the script runs standalone without error, then the problem isn’t there. You have to look elsewhere, and I suspect it’s how your system is calling the shell.

Thank you Don for the reply and appreciating the valuable time spent by you to read my guinness record breaking post.

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.

exch_service_alert10=cmd /c echo scripts\ExServiceAlert10.ps1; exit $LastExitCode | powershell.exe -command –