Psake and robocopy failing
Asked Answered
C

1

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?

Cooks answered 14/5, 2013 at 1:41 Comment(1)
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.Proselytize
W
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
Watering answered 14/5, 2013 at 13:21 Comment(3)
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.Cooks
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?Chimborazo
@Chimborazo I wouldn't know, because I wasn't involved in making that decision, and I won't speculate about the reasons.Watering

© 2022 - 2024 — McMap. All rights reserved.