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

寵の児 提交于 2019-12-12 11:02:03

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/32340852/robocopy-in-tfs-build-powershell-step-reports-failure-but-has-no-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!