2

When i am using batch file for invoking msbuild.exe ,logging functionality works fine.

But when it is written in powershell ,it doesn't log anything.

Following is the powershell script i am using . Any idea how to solve this?

# Script to invoke build
. ./vsvars32.ps1

Remove-Item "ViewBuild1.log"

$MsbuildBinPath="C:\Windows\Microsoft.NET\Framework\v4.0.30319"


$errorLogFileName="ViewBuild1Errors.log"
$buildLogFileName="ViewBuild1.log"

$MSBuildLogger="/flp1:Append;LogFile=ViewBuild1.log;Verbosity=Normal; /flp2:LogFile=ViewBuild1Errors.log;Verbosity=Normal;errorsonly"

$MSBuildFile="Build.Targets"

Write-Host --------------------------------------------
Write-Host Prepare for the build
Write-Host --------------------------------------------
&"$MsbuildBinPath\Msbuild.exe" $MSBuildFile "/t:Prepare"  "$MSBuildLogger"
if ($LastExitCode -ne 0) {
Write-Host "It failed, send a mail"
} 
#$LastExitCode

&"$MsbuildBinPath\Msbuild.exe" $MSBuildFile "/t:BuildAll"  "$MSBuildLogger"

Viewbuild1.log has no contents at all after executing though i have used append option in filelogger.

Samselvaprabu
  • 16,830
  • 32
  • 144
  • 230

2 Answers2

2

All of $MSBuildLogger is passed as a single argument to MSBuild e.g.:

PS> $MSBuildLogger="/flp1:Append;LogFile=ViewBuild1.log;Verbosity=Normal; /flp2:LogFile=ViewBuild1Errors.log;Verbosity=
Normal;errorsonly"
PS> echoargs $MSBuildLogger
Arg 0 is </flp1:Append;LogFile=ViewBuild1.log;Verbosity=Normal; /flp2:LogFile=ViewBuild1Errors.log;Verbosity=Normal;erro
rsonly>

Try using two variables instead:

PS> $MSBuildLogger1="/flp1:Append;LogFile=ViewBuild1.log;Verbosity=Normal;"
PS> $MSBuildLogger2="/flp2:LogFile=ViewBuild1Errors.log;Verbosity=Normal;errorsonly"
PS> echoargs $MSBuildLogger1 $MSBuildLogger2
Arg 0 is </flp1:Append;LogFile=ViewBuild1.log;Verbosity=Normal;>
Arg 1 is </flp2:LogFile=ViewBuild1Errors.log;Verbosity=Normal;errorsonly>

FYI, echoargs.exe is a utility from the PowerShell Community Extensions.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
1

Concat the entire MSBuild command as a powershell var and then use Invoke-Command to execute it. I do that and it works great!

$console_logging_switch = " /clp:Summary"";""Verbosity=$consoleLogLevel"
$file_logging_switch = " /flp:Summary"";""Append"";""LogFile=build.$config.log"";""verbosity=normal"
$file_errlog_switch = " /flp1:Append"";""LogFile=build.$config.err"";""errorsonly"
$file_wrnlog_switch = " /flp2:Append"";""LogFile=build.$config.wrn"";""warningsonly"
$logging_switch = $console_logging_switch + $file_logging_switch + $file_errlog_switch + $file_wrnlog_switch
$targets_switch = " /t:$targets"
$outdir_switch = " /p:OutDir=""$outdir"""
$config_switch = " /p:Configuration=""$config"""

if($skipCodeAnalysis){ $config_switch = " $config_switch /p:RunCodeAnalysis=false " }
if($buildReferences){ $config_switch = " $config_switch /p:BuildProjectReferences=false " }
if($filter -ne $null){ $config_switch = " $config_switch /p:ProjectFilter=$filter " }
$options = $outdir_switch + $config_switch + $logging_switch + $targets_switch

$cmd = "msbuild $project $options"
Invoke-Expression $cmd
Nick Nieslanik
  • 4,388
  • 23
  • 21