PowerShell - 启动进程和命令行开关
我可以很好地运行:
$msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe"
start-process $msbuild -wait
但是当我运行此代码(如下)时,我收到错误:
$msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe /v:q /nologo"
start-process $msbuild -wait
有没有办法可以使用启动进程将参数传递给 MSBuild? 我愿意不使用启动进程,我使用它的唯一原因是我需要将“命令”作为变量。
当我有
C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe /v:q /nologo
单独一行,Powershell 中如何处理?
我应该使用某种 eval() 函数来代替吗?
I can run this fine:
$msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe"
start-process $msbuild -wait
But when I run this code (below) I get an error:
$msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe /v:q /nologo"
start-process $msbuild -wait
Is there a way I can pass parameters to MSBuild using start-process? I'm open to not using start-process, the only reason I used it was I needed to have the "command" as a variable.
When I have
C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe /v:q /nologo
on a line by itself, how does that get handled in Powershell?
Should I be using some kind of eval() kind of function instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你需要将你的参数分成单独的参数
you are going to want to separate your arguments into separate parameter
使用显式参数,它将是:
编辑:引号。
Using explicit parameters, it would be:
EDIT: quotes.
警告
如果您从 Powershell 创建的 cmd.exe 窗口运行 PowerShell,第二个实例将不再等待作业完成。
现在,从新的 cmd 窗口中再次运行 PowerShell,并在其中启动第二个 cmd 窗口:
cmd2> PowerShell PowerShell
的第二个实例不再接受 -Wait 请求,所有后台进程/作业都返回“已完成”状态,即使它们仍在运行!
当我的 C# Explorer 程序用于打开 cmd.exe 窗口并且从该窗口运行 PS 时,我发现了这一点,它也会忽略 -Wait 请求。
看来任何作为 cmd.exe 的“win32 作业”的 PowerShell 都无法满足等待请求。
我在 Windows 7/x64 上使用 PowerShell 3.0 版遇到了这个问题
Warning
If you run PowerShell from a cmd.exe window created by Powershell, the 2nd instance no longer waits for jobs to complete.
Now from the new cmd window, run PowerShell again and within it start a 2nd cmd window:
cmd2> PowerShell
The 2nd instance of PowerShell no longer honors the -Wait request and ALL background process/jobs return 'Completed' status even thou they are still running !
I discovered this when my C# Explorer program is used to open a cmd.exe window and PS is run from that window, it also ignores the -Wait request.
It appears that any PowerShell which is a 'win32 job' of cmd.exe fails to honor the wait request.
I ran into this with PowerShell version 3.0 on windows 7/x64
我发现使用 cmd 作为替代方案效果很好,特别是当您需要通过管道传输被调用应用程序的输出时(特别是当它没有内置日志记录时,与 msbuild 不同)
cmd /C "$msbuild $args ”>> $输出文件
I've found using cmd works well as an alternative, especially when you need to pipe the output from the called application (espeically when it doesn't have built in logging, unlike msbuild)
cmd /C "$msbuild $args" >> $outputfile
除非OP使用PowerShell社区扩展,它确实提供了一个Start-Process cmdlet以及许多其他命令。 如果是这种情况,那么 Glennular 的解决方案会很有效,因为它与 pscx\start-process 的位置参数匹配: -path (位置 1) -arguments (位置 2)。
Unless the OP is using PowerShell Community Extensions which does provide a Start-Process cmdlet along with a bunch of others. If this the case then Glennular's solution works a treat since it matches the positional parameters of pscx\start-process : -path (position 1) -arguments (positon 2).
如果您希望它等待,请在启动进程语句中添加 -wait 标志。
If you want it to wait add -wait flag to start-processs statements.