Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robocopy in TFS Build PowerShell Step Reports Failure But Has No Error

My powershell script runs without error reported in the log file, but the TFS 2015 build step reports an error. Do I need to perform a special call back?

This is a new style build, not a XAML based one.

The script is nothing special, it calls robocopy, which occurs successfully.

Here's the script:

[CmdletBinding()]
param (
  [string]$localWorkspace,
  [string]$destination 
)
begin{}
process
{
  try
  {
    ## Script
    $ServiceDirs = Get-ChildItem $localWorkspace -Recurse | ?{ $_.PSIsContainer -eq $True -and $_.Name -match "^Service$" } | % { $_.FullName }

    $serviceCollection = @{}
    foreach ($Service in $ServiceDirs)
    {
      $ServiceName = Get-ChildItem $Service | ?{$_.Name -match ".*\.csproj$" } | % { $_.BaseName }

      $binpath = ($service + "\bin\release")
      $serviceCollection.Add($serviceName , $binpath)
    }

    $serviceCollection.GetEnumerator() | % { 
      Write-Verbose "Processing service: $($_.key)" 

      $currentDestination = ($destination + "\" + $_.key)
      $output = robocopy $_.value $currentDestination /MIR /NFL /NDL /NJH /NJS /nc /ns /np
    }
  }
  catch
  {
    write-host "Caught an exception:" 
    write-host "Exception Type: $($_.Exception.GetType().FullName)" 
    write-host "Exception Message: $($_.Exception.Message)" 
  }
}
end{}

I can see all the robocopy output, if I unsilence it and use /DEBUG and none of the catch in the TFS build log.

Strangely, when I force an error, the catch executes and the step reports success.

The error message reported is:

Task PowerShell failed. This caused the job to fail. Look at the logs for the task for more details.

like image 867
NikolaiDante Avatar asked Sep 01 '15 21:09

NikolaiDante


1 Answers

TL;DR Check the exit codes used in the calls, or use exit to leave the script.


RoboCopy uses a suite of exit codes including:

0×00 0 No errors occurred, and no copying was done. The source and destination directory trees are completely synchronized.

0×01 1 One or more files were copied successfully (that is, new files have arrived).

(Full List Here)

Because the script didn't have an exit statement the value of $LastExitCode was 1 which makes sense for Robocopy but causes TFS to believe the script to fail.

Using exit supressed the Robocopy exit code so TFS believed the script to have worked. However, this meant that any Robocopy information was being suppressed.

Changing the final line to exit ($LastExitCode -band 24) solved the issue properly, as per this article.

like image 190
NikolaiDante Avatar answered Sep 20 '22 10:09

NikolaiDante