使用Powershell 2.0调用mage并调用运算符(即&)

发布于 2024-10-03 21:21:27 字数 713 浏览 6 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深已缘浅 2024-10-10 21:21:27

$params 定义为数组很容易,每个数组项一个参数:

# define $params as an array
$params = "-New", "Application"

# and pass this array in, just as you tried before
& "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" $params

同上,通过几个步骤动态构建 $params

$params = @()
...
$params += '-New'
...
$params += 'Application'
...
& "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" $params

这种方式经常被忽视,但它是非常方便,它允许轻松地动态组合本机应用程序的复杂命令行。

PS 我们不必关心参数值中的空格,我们按原样使用参数数组中的值,不需要额外的 "

It is easy with $params defined as an array, one parameter per array item:

# define $params as an array
$params = "-New", "Application"

# and pass this array in, just as you tried before
& "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" $params

Ditto with $params dynamically built in a few steps:

$params = @()
...
$params += '-New'
...
$params += 'Application'
...
& "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" $params

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.

归属感 2024-10-10 21:21:27

Start-Process

有更多方法可以做到这一点。首先是通过 Start-Process

$p = '-h 3 google.com'
start-process tracert -arg $p

弹出新窗口。如果您想在控制台内运行该进程,只需使用 -NoNewWindow 运行它

$p = '-h 3 google.com'
start-process tracert -arg $p -nonew

$params = "-New Application"
start-process "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" -arg $params -nonew

Invoke-Expression

Invoke-Expression 也会有帮助。但这很棘手,因为可执行文件的路径中有空格。这是可行的,因为路径中没有空格:

$p = '-h 3 google.com'
invoke-expression "tracert $p"

但如果有空格,则需要在内部使用 &

$params = "-New Application"
Invoke-Expression "& ""C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe"" $params"

注意 "& ""C:\Program Files ( x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe"" $params" 扩展为:

& "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" -New Application

这就是您想要的。但是,如果其中一个参数中再次有空格,则再次..您需要引用它:

$file1 = 'c:\test path\file1.txt'
$file2 = 'c:\test path\file2.txt'
$params = """$file1"" ""$file2"""
Invoke-Expression "& someexecutable $params"

解析非常棘手:|

Start-Process

There are more ways how to do that. First is via Start-Process:

$p = '-h 3 google.com'
start-process tracert -arg $p

New window pops up. If you would like to run the process inside the console, just run it with -NoNewWindow

$p = '-h 3 google.com'
start-process tracert -arg $p -nonew

$params = "-New Application"
start-process "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" -arg $params -nonew

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:

$p = '-h 3 google.com'
invoke-expression "tracert $p"

But if there is a space, you need to use & inside:

$params = "-New Application"
Invoke-Expression "& ""C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe"" $params"

Note that "& ""C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe"" $params" is expanded to this:

& "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe" -New Application

which is what you wanted. But if there is again a space in one of the parameters, then again.. you need to quote it:

$file1 = 'c:\test path\file1.txt'
$file2 = 'c:\test path\file2.txt'
$params = """$file1"" ""$file2"""
Invoke-Expression "& someexecutable $params"

The parsing is quite tricky :|

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文