Robocopy in TFS Build PowerShell 步骤报错但没有报错

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

我的 powershell 脚本运行时没有在日志文件中报告错误,但 TFS 2015 构建步骤报告错误。我需要执行特殊回拨吗?

这是一种新风格的构建,而不是基于 XAML 的构建。

脚本没什么特别的,它调用了robocopy,成功发生。

这是脚本:

[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{}

我可以看到所有 robocopy 输出,如果我取消静音并在 TFS 构建日志中使用 /DEBUG 和捕获的 none。

奇怪的是,当我强制错误时,catch 执行并且步骤报告成功。

报告的错误信息是:

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

TL;DR 检查调用中使用的退出代码,或使用 exit 离开 脚本。


RoboCopy 使用一套退出代码,包括:

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)

因为脚本没有 exit 语句,所以 $LastExitCode 的值为 1,这对 Robocopy 有意义,但导致 TFS 认为脚本失败。

使用 exit 抑制了 Robocopy 退出代码,因此 TFS 认为该脚本已经起作用。但是,这意味着任何 Robocopy 信息都被隐藏了。

根据 this article.

,将最后一行更改为 exit ($LastExitCode -band 24) 正确解决了问题