1

Here is the command that works in command prompt.

C:\Temp\Agent.exe CustomerId={9c0-4ab1-123-102423a} ActivationId={9c0-4ab1-123-102423a} WebServiceUri=https://Agent/

Here is the error. (I have tried invoke-command and arguments but I think the { is causing issues.

Error: Agent.exe: The command parameter was already specified.

dcaz
  • 847
  • 6
  • 15
  • 1
    To complement Bill_Stewart's helpful answer: PowerShell has _more_ [metacharacters](https://en.wikipedia.org/wiki/Metacharacter) (characters with special meaning) than other shells, notably including `@ { } , ; \``. For these characters to be used _verbatim_, they must be individually `\``-escaped or enclosed in a quoted string. See [this answer](https://stackoverflow.com/a/66302956/45375) for more information. – mklement0 Jun 29 '21 at 11:56

2 Answers2

6

You are certainly not required to use Start-Process (although it may "work," with some limitations, in some scenarios). The simplest and most straightforward answer is to quote the arguments:

C:\Temp\Agent.exe 'CustomerId={9c0-4ab1-123-102423a}' 'ActivationId={9c0-4ab1-123-102423a}' 'WebServiceUri=https://Agent/'

If the executable you want to run is in a path that contains spaces (or the executable filename itself contains spaces), quote the command and use the & (call/invocation) operator; e.g.:

& 'C:\Temp Dir\Agent.exe' 'CustomerId={9c0-4ab1-123-102423a}' 'ActivationId={9c0-4ab1-123-102423a}' 'WebServiceUri=https://Agent/'

Remarks:

  • If you need string interpolation (i.e., automatic expansion of $variable names inside strings), then use " instead of ' as your quote character. Use ' instead of " (as in the examples above) to prevent string interpolation.

  • Parameter quoting in this case is required because the { and } symbols have special meaning in PowerShell.

Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
-1

The proper way to run external programs is to use Start-Process. It gives you a couple of additional options like a separate ArgumentList parameter, running-as another user, or redirecting outputs:

Start-Process -FilePath 'C:\Temp\Agent.exe' -ArgumentList @(
  # Arguments are space-separated when run. You could also just use one big string.
  'CustomerId={9c0-4ab1-123-102423a}',
  'ActivationId={9c0-4ab1-123-102423a}',
  'WebServiceUri=https://Agent/'
)
Cpt.Whale
  • 4,784
  • 1
  • 10
  • 16
  • 3
    I would re-word from "the proper way" to "one way." (`Start-Process` has it's place but it's not the _only_ proper way to invoke executables.) – Bill_Stewart Jun 28 '21 at 21:17
  • @Bill_Stewart it's not the only way, but I stand by it as the "proper" way for users of any level for many reasons. Looks like you beat me by a minute to answer though, so +1 :). – Cpt.Whale Jun 28 '21 at 21:56
  • 4
    To synchronously execute console applications or batch files and capture their output, call them _directly_ (`c:\path\to\some.exe ...` or `& $exePath ...`), do _not_ use `Start-Process` - see [this answer](https://stackoverflow.com/a/51334633/45375). – mklement0 Jun 28 '21 at 21:59
  • 3
    To elaborate: Typically, external executables are _console_ applications, and for them `Start-Process` is definitely _not_ the proper way to invoke them (except in usual circumstances - if you need to run in a _new_ console window (including with elevation) or if you need to run with a different user identity). Recommending `Start-Process` as _the_ tool for calling external programs is very problematic. – mklement0 Jun 28 '21 at 22:08
  • 1
    To readers reaching this page: Although this answer is _accepted_ it is not _recommended_. – Bill_Stewart Jun 29 '21 at 14:28