使用Powershell 2.0调用mage并调用运算符(即&)
我想使用 Powershell 2.0 通过 Microsoft 的清单生成和编辑工具(mage)来编写应用程序清单的创建脚本。具体来说,我希望能够将动态指定的参数值传递给 mage 命令(例如从 xml 或其他源读取)。
虽然我可以使用调用表达式来完成此操作,但我更愿意避免它被视为不太安全的选项(即容易受到“powershell 注入攻击”)。
这是我所知道的。
此操作成功,并显示消息“application.exe.manifest已成功创建”:
& "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" -New Application
此操作失败,并显示消息“第一个参数必须是以下之一:-New、-Update、- Sign”(这是一个 mage,而不是 powershell,错误消息):
$params = "-New Application"
& "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" $params
如何将 $params 值传递给 mage 命令,以便 mage 成功解释它?
I would like to use Powershell 2.0 to script the creation of an application manifest using Microsoft's Manifest Generation and Editing tool (mage). Specifically, I would like to be able to pass dynamically specified parameter values to the mage command (e.g. read from xml or some other source).
Although I can accomplish this using invoke-expression, I would prefer to avoid it is regarded as a less secure option (i.e. vulnerable to "powershell injection attacks").
Here is what I know.
This succeeds with the message "application.exe.manifest successfully created":
& "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" -New Application
This does not succeed with the message "The first argument must be one of the following: -New, -Update, -Sign" (which is a mage, not powershell, error message):
$params = "-New Application"
& "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" $params
How can I pass the $params value to the mage command so it is successfully interpreted by mage?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
$params
定义为数组很容易,每个数组项一个参数:同上,通过几个步骤动态构建
$params
:这种方式经常被忽视,但它是非常方便,它允许轻松地动态组合本机应用程序的复杂命令行。
PS 我们不必关心参数值中的空格,我们按原样使用参数数组中的值,不需要额外的
"
。It is easy with
$params
defined as an array, one parameter per array item:Ditto with
$params
dynamically built in a few steps:This way is often overlooked but it is very handy, it allows easy dynamic composition of complex command lines for native applications.
P.S. And we do not have to care of spaces in parameter values, we use values in the parameter array as they are, no extra
"
needed.Start-Process
有更多方法可以做到这一点。首先是通过
Start-Process
:弹出新窗口。如果您想在控制台内运行该进程,只需使用
-NoNewWindow
运行它Invoke-Expression
Invoke-Expression
也会有帮助。但这很棘手,因为可执行文件的路径中有空格。这是可行的,因为路径中没有空格:但如果有空格,则需要在内部使用
&
:注意
"& ""C:\Program Files ( x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe"" $params"
扩展为:这就是您想要的。但是,如果其中一个参数中再次有空格,则再次..您需要引用它:
解析非常棘手:|
Start-Process
There are more ways how to do that. First is via
Start-Process
:New window pops up. If you would like to run the process inside the console, just run it with
-NoNewWindow
Invoke-Expression
Invoke-Expression
could help as well. But it is tricky, because you have spaces in path to your executable. This works, because there is no space in the path:But if there is a space, you need to use
&
inside:Note that
"& ""C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe"" $params"
is expanded to this:which is what you wanted. But if there is again a space in one of the parameters, then again.. you need to quote it:
The parsing is quite tricky :|