简体   繁体   中英

Call PowerShell Script From Batch File With All Parameters

This question and this blog post address how to pass particular parameters into a PowerShell script from a batch file.

How can one pass all parameters into the PowerShell script? Basically I want to splat all the parameters so that the batch file passes all arguments through transparently.

Edit for more context:

I'm currently using a line like:

PowerShell.exe -Command "& '%~dpn0.ps1' '%1' '%2'"

This works, but creates a redundancy between the files such that, if I update the PowerShell script to take different arguments, I have to update the batch script as well. What would be nice is if I could do something like:

PowerShell.exe -Command "& '%~dpn0.ps1' '%*'"

Try the following:

powershell -File "%~dpn0.ps1" %*
  • In batch files, %* represents all arguments passed. [1]

  • -File is the parameter to use to invoke scripts via PowerShell's CLI.

  • All remaining arguments are then passed through as as-is (whereas -Command would subject them to another round of interpretation by PowerShell [2] ).

[1] Note that for cmd.exe (batch files) to recognize an argument with embedded whitespace as a single argument, you must enclose it in "..." .
For instance, if you wanted to pass arguments a and bc to batch file file.cmd , you'd have to call it as
file a "bc" .
To pass embedded " instances, \\ -escape them; eg, "\\"bc\\"" makes PowerShell see "bc" , including the double quotes.
If you respect these rules, %* - without quoting - properly passes the array of arguments through.
Do not use "%*" - it won't work as expected.

[2] In effect, -Command causes all following arguments to be joined by a single space each, and the resulting string is then interpreted as a PowerShell command - that is, after the arguments are parsed by the rules of cmd.exe (batch files), they are subject to another round of parsing, by PowerShell.
Unfortunately, PowerShell has always worked this way, but the behavior is obscure, and is likely to cause even more confusion in the Unix world, now that PowerShell has gone cross-platform - see this GitHub issue .

I wanted to do something similar, but a bit more complicated recently, and the answers here and information on a few sites helped me a lot. In my case the parameter to be passed from the cmd was a variable number of paths, often with spaces, potentially with $ and & - and the parameter was to be splatted, ie it had to have commas between strings, or the script would interpret it as a different argument. No matter what I did, I would break something. So in the end I had concatenated all the individual entries into a single variable, introduced escapes for $ and &, and passed it to the intermediate script:

set a="%~1"
if [%1]==[] goto end
:loop
shift
set bbbb="%~1"
if [%1]==[] goto execute
set a=%a% %b%
goto loop
:execute
set aaaa=%aaaa:&=`&%
set aaaa=%aaaa:$=`$%
powershell -file (fullpath)\intermediatescript.ps1 %a%
:end
exit

which contains just a single line, invoking the main script:

& '(fullpath)\mainscript.ps1' -Path:$args

(I'm sharing this in case someone looks for a solution to the problem similar to mine or if wiser people spot a problem with my approach that I failed to encounter so far.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM