3

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!

DashRantic
  • 1,448
  • 4
  • 19
  • 32

5 Answers5

3

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.

latkin
  • 16,402
  • 1
  • 47
  • 62
  • Hi, this doesn't seem to work either: $msBuildExecutable + $buildArgs 2>&1 | Write-BuildLog Doesn't even seem to be running MSBuild? – DashRantic Aug 07 '13 at 17:43
  • More detail--I'm looking to run a specific version of MSBuild, which is why I have it in a variable. But it doesn't appear that I can run it in the way your are proposing, which is why I was using Start-Process in the first place--is there another way I can do this without Start-Process then? – DashRantic Aug 07 '13 at 17:46
  • Also just tried: & $msBuildExecutable $buildArgs 2>&1 | Write-BuildLog still nothing – DashRantic Aug 07 '13 at 17:51
  • If your args are in an array, you need to use "splatting" to pass them to an executable that expects them one-at-a-time. `PS> & $pathToMsBuild @buildArgs` – latkin Aug 07 '13 at 18:01
  • They're not in an array, just a string. – DashRantic Aug 07 '13 at 18:03
  • 2
    However you are constructing the string, instead use the same inputs to construct an array. Or if it's only a couple of arguments, just pass them one-at-a time like `PS> & $msbuildPath $arg1 $arg2 ...` – latkin Aug 07 '13 at 18:07
2

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

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • Thanks! But I want to send the output to my Write-Buildlog cmdlet, which it doesn't look like -RedirectStandardError can do? – DashRantic Aug 07 '13 at 15:22
2

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.

James Woolfenden
  • 6,498
  • 33
  • 53
1

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
T.CK
  • 411
  • 4
  • 6
0

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.

Farrukh Waheed
  • 2,163
  • 2
  • 29
  • 59