Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. However, it takes a really long time to return. When the above line is reached, I see the msbuild windows for about 2 minutes. Then, it closes. After that, it takes another 8 minutes for the process to complete. If I just run the above in a cmd window, it takes about 2 minutes for it to complete. I tried starting the process cmd.exe and passing msbuild as a parameter, but got the same result. I also tried Invoke-Expression and also got the same results.

Does anyone have a clue what can be causing this delay ?

Thanks in advance!

like image 243
b3bel Avatar asked Nov 11 '13 11:11

b3bel


People also ask

How do you wait 5 seconds in PowerShell?

Using the PowerShell Start Sleep cmdlet You can also write Start-Sleep 5 to let the script sleep for 5 seconds. But for the readability of your script is it better to define the -s or -seconds parameter. The only other parameter besides seconds is milliseconds.

Does Start process wait?

When using the Wait parameter, Start-Process waits for the process tree (the process and all its descendants) to exit before returning control. This is different than the behavior of the Wait-Process cmdlet, which only waits for the specified processes to exit.

Why is my PowerShell script so slow?

When PowerShell starts to become slow at startup, an update of the . NET framework might be the cause. To speed up again, use ngen.exe on PowerShell's assemblies. It generate native images for an assembly and its dependencies and install them in the Native Images Cache.


1 Answers

I am using PowerShell v4.0 Start-Process for running MSBuild on Win2012R2. Usually build time of my solution is 1 min, but on build server it is 16 min. It takes MSBuild 15 min to close all nodes and finally exit (16 min to print "Finished!!!").

PowerShell code:

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

MSBuild args:

$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).

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
like image 165
Sergei Zinovyev Avatar answered Sep 22 '22 03:09

Sergei Zinovyev