8

Robocopy will exit with a code above 0 and still possibly not be a failure. PSake detects anything above 0 as a failure and fails the build. This is fine, but how come this still fails:

task Deploy {    
        robocopy $source $dest /NP /S /XO /NFL /NDL /NJH /NJS | Out-Default

    if ($lastexitcode -eq 3)
    {
       Write-Host "Got Here"
       $lastexitcode = 0       
    }

    Write-Host "Deploy local complete"
    Write-Host $lastexitcode
}

TaskTearDown {  
    if ($LastExitCode -ne 0) {
        write-host "Build failed"
        exit 1
    }
}

I can verify that the Deploy if statement is hit and the Write-Host outputs 0, correctly, yet the TaskTearDown still detects the last exit code as 3! How do I fix this?

Bill
  • 5,263
  • 6
  • 35
  • 50
chum of chance
  • 6,200
  • 10
  • 46
  • 74
  • 2
    I would not assign `$lastexitcode`, it's an automatic variable. Assignment perhaps creates a local variable and `TaskTearDown` still sees the original. In any case try to use a different approach without `$lastexitcode = 0`. – Roman Kuzmin May 14 '13 at 04:48

1 Answers1

10

robocopy exit codes below 8 are non-error status codes. Only exit codes of 8 and above indicate an error. See here.

The reason why your teardown task still reports an exit code of 3 is probably because the automatic variable $LastExitCode is a global variable, whereas your deploy task creates an additional local variable $lastexitcode that masks the global variable in the scope of the task. As suggested in this answer to a similar question, use the $global: prefix:

$global:LastExitCode = $null
Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Ah it is a scoping issue! Thanks! Still very new to posh. PSake actually looks for 0 and will throw on null ... so $global:LastExitCode = 0 is what I need. – chum of chance May 14 '13 at 13:36
  • Error code 4 (Mismatch) means that a filename exists in the source as a file, but already exists in the destination as a directory. I would have called this an error; why is it not considered one? – cowlinator Apr 10 '18 at 21:23
  • @cowlinator I wouldn't know, because I wasn't involved in making that decision, and I won't speculate about the reasons. – Ansgar Wiechers Apr 10 '18 at 23:04