Question

I'm creating some new build scripts for a project using PowerShell, and would like to capture the output of MSBuild when I call it and save that to a text file. I've tried a couple different methods of doing so with no luck so far--here's what I last tried (Write-Buildlog just handles writing off the output to the log):

Start-Process $msBuildExecutable $buildArgs -Wait | Write-Buildlog

No output at all is captured, though MSBuild runs fine. Any tips would be greatly appreciated as I've done a bit of searching and have found nothing useful so far, which is surprising :)

Thanks!

Was it helpful?

Solution

If you want to pipe output from msbuild to a logging or processing cmdlet, you should not be starting it with Start-Process, just execute it normally.

PS> msbuild.exe $flag1 $flag2 $thingToBuild | Write-Buildlog

You might need to also redirect stderr to capture more of the output. In that case you would need to add 2>&1

PS> msbuild.exe $flag1 $flag2 $thingToBuild  2>&1 | Write-Buildlog

Start-Process will start your process outside of any powershell environment or hosting, so obtaining output and sending to cmdlets becomes much more difficult. If you want to process executable output in powershell, then it's best to simply stay within a powershell environment the whole time.

OTHER TIPS

In Start-Process CmdLet you've got a -RedirectStandardOutput parameter ; have you test it ?

Start-Process -FilePath "C:\Windows\system32\ping.exe" -ArgumentList "MyMachine" -RedirectStandardOutput "c:\temp\p.txt" -NoNewWindow

You can also redirect errors with -RedirectStandardError

All you need is:

& msbuild.exe .\yourproj.sln |Out-Host

or even: & msbuild.exe .\yourproj.sln |Out-File c:\log.txt

If writing to a file is what you want.

You can do whatever you want with the output if you run it like this:

$MSBuild = [string](MSBuild.exe -argument1 -argument2 -andSoOn)

Say you want to have the output go to a file.

$MSBuild = [string](MSBuild.exe -argument1 -argument2 -andSoOn) | Out-File C:\text.txt

Or write it like so...

$MSBuild = [string](MSBuild.exe -argument1 -argument2 -andSoOn) 2>&1 >> C:\text.txt

Note: 2 ">>" means to append and 1 ">" to overwrite previously added lines.

Or if you want just to get a just one word like "true" from a bunch of output.

$MSBuild = [string](MSBuild.exe -argument1 -argument2 -andSoOn)
if($MSBuid -match "true") 
{
    Write-Host "Whatever you want to say about what's true"
}

If you want to see it all in the console, you could do this.

$MSBuild = [string](MSBuild.exe -argument1 -argument2 -andSoOn) | Out-Host

Or...

$MSBuild = [string](MSBuild.exe -argument1 -argument2 -andSoOn)
Write-Host $MSBuild

I think, you should read about the Logging options in MsBuild command line: http://msdn.microsoft.com/en-us/library/ms164311.aspx

Msbuild /Logger gives you good options to log output. You can use this argument in your powershell script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top