0

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 !

xashcorex
  • 134
  • 1
  • 3
  • 11
  • Why not just use start-process? e.g. $robocopy = start-process robocopy -argumentlist -passthru. That will return a process object with HasExited and ExitCode properties you can check to see if it's finished and what the exit code was. – mjolinor Feb 04 '14 at 15:59
  • here is how I lastexitcode from start-process - http://stackoverflow.com/questions/20645326/safest-way-to-run-bat-file-from-powershell-script/20645421#20645421 – Knuckle-Dragger Feb 04 '14 at 16:12
  • Hi, First off thanks for your help ! I have tried to use start-process as Knuckle-Dragger showed but the exit code returned is always 1... Even if the copy works. I must be missing something here. – xashcorex Feb 04 '14 at 16:34
  • Nope, it worked perfectly! **ExitCode 0 = No Errors, No Files Copied** So ExitCode 1 is actually a good thing. **ExitCode 1 = No Errors, New Files Copied**. ExitCode > 1 = There were errors, or at least unexpected results. – TheMadTechnician Feb 04 '14 at 17:03

1 Answers1

0

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
Knuckle-Dragger
  • 6,644
  • 4
  • 26
  • 41