PowerShell - 启动进程和命令行开关

发布于 2024-07-14 22:52:46 字数 524 浏览 8 评论 0原文

我可以很好地运行:

$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 技术交流群。

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

发布评论

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

评论(6

北方。的韩爷 2024-07-21 22:52:47

你需要将你的参数分成单独的参数

$msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe"
$arguments = "/v:q /nologo"
start-process $msbuild $arguments 

you are going to want to separate your arguments into separate parameter

$msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe"
$arguments = "/v:q /nologo"
start-process $msbuild $arguments 
憧憬巴黎街头的黎明 2024-07-21 22:52:47

使用显式参数,它将是:

$msbuild = 'C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe'
start-Process -FilePath $msbuild -ArgumentList '/v:q','/nologo'

编辑:引号。

Using explicit parameters, it would be:

$msbuild = 'C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe'
start-Process -FilePath $msbuild -ArgumentList '/v:q','/nologo'

EDIT: quotes.

沩ん囻菔务 2024-07-21 22:52:47

警告

如果您从 Powershell 创建的 cmd.exe 窗口运行 PowerShell,第二个实例将不再等待作业完成。

cmd>  PowerShell
PS> Start-Process cmd.exe -Wait 

现在,从新的 cmd 窗口中再次运行 PowerShell,并在其中启动第二个 cmd 窗口:
cmd2> PowerShell PowerShell

PS> Start-Process cmd.exe -Wait
PS>   

的第二个实例不再接受 -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.

cmd>  PowerShell
PS> Start-Process cmd.exe -Wait 

Now from the new cmd window, run PowerShell again and within it start a 2nd cmd window:
cmd2> PowerShell

PS> Start-Process cmd.exe -Wait
PS>   

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

狼亦尘 2024-07-21 22:52:47

我发现使用 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

梦里泪两行 2024-07-21 22:52:47

除非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).

谷夏 2024-07-21 22:52:47

如果您希望它等待,请在启动进程语句中添加 -wait 标志。

# Start EXE as a detected process
function StartExe {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true, Position=0)]
        [string]$ProcessName,

        [Parameter(Mandatory=$false, Position=1, ValueFromRemainingArguments=$true)]
        [string[]]$Arguments
    )

    # Command line as Multiple Arguments:
    #    PS> StartExe tclsh myscript.tcl arg1 arg2 arg3
    if ($Arguments) {
        $argumentString = $Arguments -join ' '
        Start-Process -FilePath $ProcessName -ArgumentList $argumentString -NoNewWindow
    }
    
    # Command line as a Single Argument:
    #    PS> StartExe "tclsh myscript.tcl arg1 arg2 arg3"
    else {
        Start-Process -FilePath 'cmd.exe' -ArgumentList "/c $ProcessName" -NoNewWindow
    }
}

If you want it to wait add -wait flag to start-processs statements.

# Start EXE as a detected process
function StartExe {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true, Position=0)]
        [string]$ProcessName,

        [Parameter(Mandatory=$false, Position=1, ValueFromRemainingArguments=$true)]
        [string[]]$Arguments
    )

    # Command line as Multiple Arguments:
    #    PS> StartExe tclsh myscript.tcl arg1 arg2 arg3
    if ($Arguments) {
        $argumentString = $Arguments -join ' '
        Start-Process -FilePath $ProcessName -ArgumentList $argumentString -NoNewWindow
    }
    
    # Command line as a Single Argument:
    #    PS> StartExe "tclsh myscript.tcl arg1 arg2 arg3"
    else {
        Start-Process -FilePath 'cmd.exe' -ArgumentList "/c $ProcessName" -NoNewWindow
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文