如何从PowerShell运行*.exe文件

4

我在C:\Folder下有一个文件夹,里面有input.xml、output.xml和licensegenerator.exe。 Licensegenerator.exe会获取我们放在input.xml中的变量,并使用output.xml文件创建我们的一个程序的临时许可证。通常我们通过命令行来完成这项操作,步骤如下:

LicenseGenerator.exe "C:\Folder\input.xml" "C:\Folder\output.xml"

我试图编写一个PowerShell脚本来完成完全相同的事情,但我遇到了困难... 下面是我的脚本:

$inputtest = "C:\Folder\Input.xml"
$outputtest = "C:\Folder\Output.xml"
$licensegen = "C:\Folder\LicenseGenerator.exe"

Invoke-Command $licensegen "$inputtest" "$outputtest"

当我运行它时,会出现以下错误:
Invoke-Command : 找不到可以接受参数'C:\Folder\Output.xml'的位置参数。
At line:5 char:1
+ Invoke-Command $licengegen "$inputtest" "$outputtest"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand
我也尝试了使用 Invoke-Expression 运行,但得到完全相同的错误(只是在开头说了“Invoke-Expression”)。有人知道我在这里做错了什么吗?

& $licensegen "$inputtest" "$outputtest" 搞定了。谢谢大家! - Christopher Cass
这个问题已经在这里详细讨论过了,并且有一些非常好的、不寻常的答案:https://dev59.com/qXI-5IYBdhLWcg3wy7sd - Binarus
3个回答

7
您正在寻找“调用运算符(&)”:

您正在寻找调用运算符(&

& $licensegen "$inputtest" "$outputtest"

Invoke-Command 主要用于在其他主机和/或其他用户上下文中运行脚本块。


4

Start-Process非常好用,因为您可以使用runas、重定向输出、隐藏子进程窗口等功能。

Start-Process -FilePath $licensegen -Argumentlist $inputtest,$outputtest


2

& "[路径] 命令" [参数]

只需将 Invoke-Command 替换为 &


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接