3 ms·
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.
by mschuetz 10y ago
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.
- emodendroket 10y agoCouldn't you use $() for this?
- vicda 10y agoThis was the hardest thing to figure out. I have a program just to ensure I'm sending the arguments that I think I am.
- 3minus1 10y agoI was frustrated as well by this. There's a program echoargs.exe you can use to find out what you need to escape in your powershell args.
- ygra 10y agoUsually 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 )
- Jayku1 10y agoThey 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 '@