質問

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 !

役に立ちましたか?

解決

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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top