Question

I'm trying to do something simple.

I want to create a robocopy job that would launch the copy and check the $lastexitcode.

$Source = "c:\SourceFolder"
$Destination "\\someserver\Destinationfolder"

$ScriptBlock = [scriptblock]::Create("robocopy $Source $Destination /MOVE /E /R:5 /E /W:5 /A-:SH; if ( $lastexitcode -gt 8) { LOGMESSAGE ERROR } else { LOGMESSAGE SUCCESS }")

start-job -scriptblock $ScriptBlock

LOGMESSAGE is a function i've written that will log the event and send a mail in case of error.

Somehow, the $lastexitcode value is always returned to 16 - Even when the copy works. Also, The function is not executed (it works outside of the scriptblock)

Does anybody have a clue on what I am doing wrong ?

Any help would be appreciated.

Thanks !

Was it helpful?

Solution

See your comment, guessing you've found success. Here is the answer for future readers in case the link I've quoted goes down. Basically we are assigning the start-process command to a variable and throwing the -passthru tag so we can poll for the .exitcode later.

$A = Start-Process -FilePath .\my-app\my-fle.bat -Wait -passthru
$A.ExitCode
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top