Reset lastexitcode if robocopy succeeds with powershell

Viewed 1337

I'm using robocopy to copy files during a build step in a Gitlab pipeline. The pipeline tasks are executed in powershell.

The exit status of robocopy results in a failure of my build step. I noticed all statuscodes under 8 are valid. Meaning I should reset the $lastexitcode to $null if the $lastexitcode is < 8

This is what I tried in my powershell script:

robocopy src dest /mir (&{if ($lastexitcode -lt 8) { $global:LASTEXITCODE = $null }})

The problem here is that the (&{ if... part is executed before robocopy.

1 Answers

You must use two statements, separated by ;:

robocopy src dest /mir; if ($lastexitcode -lt 8) { $global:LASTEXITCODE = $null }

In your attempt the (in this case empty) output of the (...) expression was passed as an argument to robocopy and, as you've observed, expressions passed as arguments are (of necessity) evaluated first.

Note: Robocopy's exit codes 0 through 7 are non-error exit codes, hence the -le 8 condition - see https://ss64.com/nt/robocopy-exit.html

Related