Home > Mobile >  missing expression after unary operator '-'
missing expression after unary operator '-'

Time:04-30

I wrote a PowerShell script in PowerShell v5.0, and I'm able to execute that script from a batch file with no issue. However when I execute the batch file in Windows 2008 with PowerShell v1.0, I'm getting an error 'missing expression after unary operator '-'.

Can someone enlighten me what is causing the error?

This is the code in my batch file

@ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%SAP_Shell_Script.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'";

CodePudding user response:

This isn't guaranteed to solve your problem, but may lead you to a solution:

First - if even possible - it's worth considering updating your very outdated v1.0 PowerShell installation (which came with Windows Server 2008).

Since you've already ruled out that it isn't the script's content that is the problem, the implication is that PowerShell's CLI syntax must have changed in later versions:

  • It sounds like at least one of the parameter names you're using isn't recognized by powershell.exe in v1, causing it and all subsequent arguments to be interpreted as a PowerShell command (and it is the - prefix - then interpreted as an operator - that causes the error message).

Troubleshoot as follows:

  • Start with powershell.exe -? in order to display what parameters are supported in your version.

  • Start with a minimal command, then try to add parameters one by one.

    • The following minimal command assumes that you've used Set-ExecutionPolicy to allow at least the current session (process) to execute scripts; in later PowerShell versions, you'd do that as follow - I don't know if it works in v1.0:

      Set-ExecutionPolicy -Force -Scope Process -ExecutionPolicy Bypass
      

Minimal command:

PowerShell "& '%PowerShellScriptPath%'"

Note that, as with your original command (which works in v5), the assumption is that the value of variable %PowerShellScriptPath% doesn't itself contain ' characters.

  • Related