PowerShell:带有等待选项的启动进程需要很长时间才能返回

b3b*_*bel 5 msbuild powershell

我有以下脚本:

$mArgs = @('myProj.vcxproj', '/t:Clean,Build' ,('/p:configuration=DEBUG'+';platform=win32;OutDir=./'))
Start-Process msbuild.exe -ArgumentList $mArgs -RedirectStandardOutput $tempFile -wait
Run Code Online (Sandbox Code Playgroud)

以上成功构建了myProj。然而,返回需要很长时间。当到达上述行时,我看到 msbuild 窗口大约 2 分钟。然后,它关闭。之后,该过程还需要 8 分钟才能完成。如果我只是在 cmd 窗口中运行上述内容,则需要大约 2 分钟才能完成。我尝试启动进程 cmd.exe 并将 msbuild 作为参数传递,但得到了相同的结果。我也尝试了 Invoke-Expression,也得到了相同的结果。

有没有人知道是什么导致了这种延迟?

提前致谢!

Ser*_*yev 5

我正在使用 PowerShell v4.0 Start-Process 在 Win2012R2 上运行 MSBuild。我的解决方案的构建时间通常为 1 分钟,但在构建服务器上为 16 分钟。MSBuild 需要 15 分钟才能关闭所有节点并最终退出(打印“完成!!!”需要 16 分钟)。

PowerShell 代码:

$process = Start-Process -FilePath $fileName -ArgumentList $arguments
 -WorkingDirectory $workingDir -NoNewWindow -PassThru -Wait
Write-Host "Finished!!!"
$exitCode = $process.ExitCode
Run Code Online (Sandbox Code Playgroud)

MSBuild 参数:

$commandLine = "/nologo /p:Configuration=Release;Platform=x64 /maxcpucount:2"
 + " "+ $dir + "MySolution.sln"
Run Code Online (Sandbox Code Playgroud)

将/nodeReuse:false添加到 MSBuild 命令行后,构建时间恢复正常(1 分钟)。

这是工作代码:

function PsStartProcess([string]$fileName, [array]$arguments, [string]$workingDir)
{
    if (!$workingDir)
    {
        $workingDir = [System.IO.Directory]::GetCurrentDirectory()
    }

    $process = Start-Process -FilePath $fileName -ArgumentList $arguments -WorkingDirectory $workingDir -NoNewWindow -PassThru -Wait
    Write-Host "Finished!!!"
    $exitCode = $process.ExitCode
    $process.Close()

    return $exitCode
}

$exeFileName = "c:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe"
$commandLine = "/nologo /p:Configuration=Release;Platform=x64 /maxcpucount:2 /nodeReuse:false" + " "+ $dir + "MySolution.sln"
PsStartProcess $exeFileName $commandLine $binDir
Run Code Online (Sandbox Code Playgroud)


Sav*_*ngh 0

刚刚观察了脚本,发现脚本的第一行似乎少了一个单引号。请重试缺少的报价并报告是否有帮助?