简体   繁体   English

从 PowerShell 脚本调用 Windows 可执行文件,传递所有 arguments

[英]Call Windows executable from PowerShell script, passing all arguments

I've looked all over, and I can't find the answer.我翻遍了,找不到答案。 If this is a duplicate, please point me to the question, but most things I've seen have answers that go around in circles and talk bout all sorts of complicated things with no direct answer.如果这是重复的,请指出我的问题,但我看到的大多数事情都有答案,go 绕圈子,谈论各种复杂的事情,没有直接的答案。

My question is simple.我的问题很简单。 I have the following Windows batch file foo.bat :我有以下 Windows 批处理文件foo.bat

@ECHO OFF
bar.exe %*

If I call foo.bat foobar 123 from the command line, it invokes bar.exe foobar 123 .如果我从命令行调用 foo.bat foobar foo.bat foobar 123 ,它会调用bar.exe foobar 123 This works with no command-line arguments.这适用于没有命令行 arguments 的情况。 This works with multiple command-line arguments.这适用于多个命令行 arguments。

What is the equivalent PowerShell script that does basically the same thing: invoke another executable, passing all CLI parameters the user provided?什么是等效的 PowerShell 脚本,它基本上做同样的事情:调用另一个可执行文件,传递用户提供的所有 CLI 参数?

I wouldn't expect this would be difficult, but I sure can't find any straightforward answer.我不认为这会很困难,但我肯定找不到任何直截了当的答案。

The PowerShell equivalent of your foo.bat file is: foo.bat 文件的foo.bat等效文件是:

bar.exe @args

PowerShell exposes all (unbound) positional arguments [1] as an array stored in the automatic $args variable . PowerShell 将所有(未绑定的)位置 arguments [1]公开为存储在自动$args变量中的数组。

By prefixing $args with @ instead of $ , argument splatting is employed, which means that the elements of the $args array are passed as individual arguments to bar.exe通过在$args前面加上@而不是$ ,使用了参数飞溅,这意味着$args数组的元素作为单独的 arguments 传递bar.exe

  • Note: This isn't strictly necessary when calling external programs (such as bar.exe ) - $args would work there too - but is more versatile in that it can also pass named arguments correctly through to other PowerShell commands, which typically have declared parameters that can be bound by name (eg, -Path C:\temp to bind value C:\temp to declared parameter -Path )注意:在调用外部程序(例如bar.exe )时,这不是绝对必要的 - $args也可以在那里工作 - 但更通用,因为它还可以正确地将命名的 arguments 传递给其他PowerShell命令,这些命令通常已声明可以按名称绑定的参数(例如, -Path C:\temp绑定值C:\temp到声明的参数-Path

As for working with $args in general:至于一般使用$args

  • $args.Count tells you how many (unbound) positional arguments were passed, $args.Count告诉您通过了多少(未绑定)位置 arguments,
  • $args[0] returns the first such argument, $args[1] the second, and so on. $args[0]返回第一个这样的参数, $args[1]返回第二个,以此类推。

However, it is usually preferable to formally declare parameters in PowerShell scripts and functions, which can then also be bound by name (eg, -FooParam foo instead of just foo ).但是,通常最好在 PowerShell 脚本和函数中正式声明参数,然后它们也可以通过名称绑定(例如, -FooParam foo而不是仅仅foo )。 See this answer for more information.有关更多信息,请参阅此答案


[1] If your script doesn't formally declare any parameters (via a param(...) block - see about_Scripts and the linked answer ), all arguments are by definition unbound (ie not mapped to a declared parameter) and positional (not prefixed by a target parameter name ). [1] 如果您的脚本没有正式声明任何参数(通过param(...)块 - 请参阅about_Scripts链接的答案),则所有arguments 根据定义都是未绑定的(即未映射到声明的参数)和位置(不以目标参数名称为前缀)。 However, if your script does declare parameters, $args only contains those arguments, if any, that were passed in addition to those binding to declared parameters.但是,如果您的脚本确实声明了参数,则$args仅包含那些 arguments(如果有的话),除了绑定到声明参数的那些之外,它们还被传递。 If your script is an advanced script (or function) , $args isn't supported at all, because passing unbound arguments is then categorically prevented.如果您的脚本是高级脚本(或函数) ,则根本不支持$args ,因为传递未绑定的 arguments 会被断然阻止。 See this answer for more information.有关更多信息,请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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