简体   繁体   English

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

[英]PowerShell: Start-Process with wait option taking long time to return

I have the following script:我有以下脚本:

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

The above successfully builds myProj.以上成功构建了myProj。 However, it takes a really long time to return.但是,返回需要很长时间。 When the above line is reached, I see the msbuild windows for about 2 minutes.到达上述行时,我看到 msbuild windows 大约 2 分钟。 Then, it closes.然后,它关闭。 After that, it takes another 8 minutes for the process to complete.之后,该过程还需要 8 分钟才能完成。 If I just run the above in a cmd window, it takes about 2 minutes for it to complete.如果我只是在 cmd window 中运行上面的代码,大约需要 2 分钟才能完成。 I tried starting the process cmd.exe and passing msbuild as a parameter, but got the same result.我尝试启动进程 cmd.exe 并将 msbuild 作为参数传递,但得到了相同的结果。 I also tried Invoke-Expression and also got the same results.我也尝试了 Invoke-Expression 并得到了相同的结果。

Does anyone have a clue what can be causing this delay?有谁知道是什么导致了这种延迟?

Thanks in advance!提前致谢!

I am using PowerShell v4.0 Start-Process for running MSBuild on Win2012R2.我正在使用 PowerShell v4.0 Start-Process 在 Win2012R2 上运行 MSBuild。 Usually build time of my solution is 1 min, but on build server it is 16 min.我的解决方案的构建时间通常为 1 分钟,但在构建服务器上为 16 分钟。 It takes MSBuild 15 min to close all nodes and finally exit (16 min to print "Finished!!!"). MSBuild 需要 15 分钟才能关闭所有节点并最终退出(打印“完成!!!”需要 16 分钟)。

PowerShell code: PowerShell 代码:

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

MSBuild args: MSBuild 参数:

$commandLine = "/nologo /p:Configuration=Release;Platform=x64 /maxcpucount:2"
 + " "+ $dir + "MySolution.sln"

After adding /nodeReuse:false to MSBuild command line the build time is back to normal (1min)./nodeReuse:false添加到 MSBuild 命令行后,构建时间恢复正常(1 分钟)。

Here is the working code:这是工作代码:

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

I had same issue using devenv.exe to compile like this我在使用devenv.exe编译时遇到了同样的问题

Start-Process $devenv $args -Wait

That would take forever.那将需要永远。 I reformatted the code to this我将代码重新格式化为

$proc = Start-Process $devenv $args -PassThru 
$proc.WaitForExit()

And it is flying.它正在飞行。 In my case it is compilation of multiple solutions (40+) in the loop, and in perf-mon I don't see many active MSBuild/Devenv processes.就我而言,它是循环中多个解决方案(40+)的编译,而在 perf-mon 中,我没有看到许多活动的 MSBuild/Devenv 进程。 There are few of each but all "terminated".每个都很少,但都“终止”了。 And memory is OK.而且内存没问题。 So, good option.所以,不错的选择。

Why are you using Start-Process to run MSBUILD?为什么要使用 Start-Process 来运行 MSBUILD? I run it directly within PowerShell eg:我直接在 PowerShell 中运行它,例如:

C:\PS> msbuild myproj.vcxproj /t:clean`,build /p:configuration=DEBUG`;platform=win32`;OutDir=. > $tempfile

Just be sure to escape the characters that PowerShell would normally interpret like , and ;只要确保转义 PowerShell 通常会解释的字符,例如,; . .

I ran into this problem today.我今天遇到了这个问题。 What I found is that msbuild waits for VBCSCompiler.exe (located in the visual studio installation folder) to close.我发现 msbuild 等待 VBCSCompiler.exe(位于 visual studio 安装文件夹中)关闭。 There is a config to this exe with a keepalive setting which defaults to 600 seconds.此 exe 有一个配置,其中的保活设置默认为 600 秒。 Setting this to 1 second will bypass this problem.将此设置为 1 秒将绕过此问题。

Just observed the script and found that the first Line of the script seems to have a missing Single Quote.刚刚观察脚本,发现脚本的第一行好像少了一个单引号。 Please try again with the missing quote and report if that helps?请重试丢失的报价并报告是否有帮助?

On Windows 7 the process returns immediately after completion of the build, but on Windows 8 I have the same problem.在 Windows 7 上,该过程在构建完成后立即返回,但在 Windows 8 上我遇到了同样的问题。 If possible, I'll try to test with some other environments as well.如果可能,我也会尝试在其他一些环境中进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM