Re:PowerShell is open sourced and is available on Linux
by mschuetz on Thursday August 18, 2016 @12:36PM
(#12313998)
Yeah, I wanted to switch to PowerShell but then I had to call an exe with variable arguments from the PowerShell script. That was a quick way back to cmd.
[ Parent
| on HN ]
Re:PowerShell is open sourced and is available on Linux
by ygra on Thursday August 18, 2016 @02:20PM
(#12315043)
Usually everything works as expected, e.g.
someprogram.exe arg1 arg2 arg3 $foo (Get-Item bar).Length
Variables get expanded in the argument list, arguments get quoted automatically based on their content. The only gotchas I can think of are: & "command with spaces.exe" args
It's a bit unexpected that the & operator is needed.Arguments sometimes get quoted unexpectedly, or need (single) quotes depending on PowerShell syntax rules to avoid evaluating something.
The worst (but sadly common) thing people can do when faced with problems here, though, is heading for Invoke-Expression, sometimes in combination with all sorts of wrappers, each of them making the problem worse, not better (it cannot get better that way).
The most robust way I tend to use when necessary is to just prepare a single array with arguments and splat that when calling the program:
program.exe @array
No surprises with PowerShell's parsing in command mode because we prepare the array in expression mode. Things are generally more predictable. Automatic quoting of the arguments still happens, though.(Side note: My own little echoargs replacement is actually a batch file :-)
@echo off
echo.argv[0] = %~0
set cnt=1
:loop
echo.argv[%cnt%] = %~1
set /a cnt+=1
shift
if not [%1]==[] goto loop
)
Re:PowerShell is open sourced and is available on Linux
by emodendroket on Thursday August 18, 2016 @12:55PM
(#12314173)
Couldn't you use $() for this?
Re:PowerShell is open sourced and is available on Linux
by Jayku1 on Thursday August 18, 2016 @06:18PM
(#12316724)
They fixed this in later versions with the literal: --%
But you can also always use start-process like:
start-process app @'
your arguments and parameters go here
'@