1

I am currently trying to get a command that I know works in CLI to run through a PowerShell Ise script but it will not allow me to run it as it is picking up a part of the command as a parameter.

I tried to run the following command, but it brings up an error:

Start-Process ssh -o KexAlgorithms=diffie-hellman-group14-sha1 Username@IP
Start-Process : Parameter cannot be processed because the parameter name 'o' is ambiguous. Possible matches include: -OutVariable -OutBuffer.
At line:1 char:30
+ Start-Process ssh -o KexAlgorithms=diffie-hellman-group14- ...
+                              ~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameter,Microsoft.PowerShell.Commands.StartProcessCommand

What is supposed to happen is the cli should pop up and allow me to access a switch which as i have stated earlier works when I manually input it into Cli.

mklement0
  • 382,024
  • 64
  • 607
  • 775

1 Answers1

1

tl;dr

As Mathias notes, you must quote the list of arguments to pass through to ssh:

# Note the '...' around the entire list of pass-through arguments.
# Use "..." if you need string interpolation.
# Use *embedded* "..." quoting for arguments that contain spaces, for instance.
Start-Process ssh '-o KexAlgorithms=diffie-hellman-group14-sha1 Username@IP'

There are two problems with your approach:

  • Because -o is unquoted, Start-Process interprets it as one of its parameter names.

    • Thus, pass-through arguments that happen to look like PowerShell parameters, must be quoted, e.g. - if passed individually (which isn't advisable; see next point) - '-o'.

    • As for the error you saw:

      • PowerShell's "elastic syntax" allows you to use parameter-name prefixes for brevity, so you don't have to type / tab-complete the full parameter name.

      • However, such a prefix must be unambiguous; if it isn't, PowerShell complains and lists the candidate parameter names, such as -OutVariable and -OutBuffer for -o in this case. Thus, prefix -outv would have avoided the ambiguity, for instance. Also note that some parameters have unambiguous short aliases, such as -ov for -OutVariable.

  • More fundamentally, trying to pass multiple pass-through arguments individually to Start-Process causes a syntax error:

    • It is best to specify all pass-through arguments as a single, quoted string, using embedded double-quoting, if necessary.

      • This answer explains the syntax problem in detail, but the short of it is that Start-Process expects pass-through arguments via a single argument, passed to its -ArgumentList (-Args) parameter, which is the 2nd positional parameter.
      • This answer explains why - even though you can specify the pass-through arguments individually as the elements of an array (,-separated), a long-standing bug makes the single-string approach preferable.
mklement0
  • 382,024
  • 64
  • 607
  • 775