Using if statements in Powershell for search Robocopy Results
 

Hi,

We currently have a script that Purges a folder structure apart from 3 files and a folder.

 $Result = Robocopy "source" "target" *.* /NP /V /E /R:1 /W:1 /PURGE /XF File1 File2 File3 /XD Folder 

this works as expected, However when then checking if the robocopy was successful

 if ($result -gt 7){"fail"} 

The result is always a failure. I believe it has something to do with the exclusion of files and folders from a purge but I am unsure.
 
Without having the exlusion the if statement works for a purge and without (useful when running jobs and so cannot see the lastexitcode)
 
Does anyone know why this behaviour occurs and if there is anyway of getting round it
 

Thanks
 
tom
 


 

April 2nd, 2012 4:44pm

Don't assign the results of the Robocopy command to a variable.  Instead, check the value of $lastexitcode immediately after calling Robocopy:

if ($lastexitcode -gt 7){"fail"} 

Free Windows Admin Tool Kit Click here and download it now
April 2nd, 2012 4:57pm

Hi Bigteddy,

Thanks for the fast reply. Usually I would use the lastexitcode feature but unfortuntely the robocopy script runs as a job (multiple instances run at the same time). And so i cannot see the last exit code. The above scripting has been working until usually the exlusion option in robocopy. Can you think of any other way of getting around it?

Thanks

Tom

April 2nd, 2012 5:02pm

I'm not sure what you mean by:

"The above scripting has been working until usually the exlusion option in robocopy."

The only solution I can think of is to include the code inside your job.  In other words, within the job, check the $lastexitcode (each instance will have its own $lastexitcode).  Then return a value from the job using Write-Output, or some suchlike action.

Free Windows Admin Tool Kit Click here and download it now
April 2nd, 2012 5:10pm

Sorry what I meant was that if I use line

 $Result = Robocopy "source" "target" *.* /NP /V /E /R:1 /W:1 /PURGE

without the exclusion options and then use

if ($result -gt 7){"fail"}

The if statement can tell just from the $result if it fails or not without needing to do anything with the lastexitcode. when i add the exclusion option it always fails.

Tom

April 3rd, 2012 8:15am

Sorry what I meant was that if I use line

 $Result = Robocopy "source" "target" *.* /NP /V /E /R:1 /W:1 /PURGE

without the exclusion options and then use

if ($result -gt 7){"fail"}

The if statement can tell just from the $result if it fails or not without needing to do anything with the lastexitcode. when i add the exclusion option it always fails.

Tom

$result is text.  It is the output in text and not an exit code.  If you inspect $result you will see that it is areport on the outcome of the copy.  It will never equal 7 br greater than 7 or match/compare to any number.

A sync with purge will always do nothing after the first launch because there is nothiong to sync.

The problem with RoboCopy is that it does not set an exitcode.  It will set the error level if you launch it with CMD.

You can parse the captured text for the status of the copy.

Free Windows Admin Tool Kit Click here and download it now
April 3rd, 2012 9:39am

Thanks for the reply jrv,

I understand that the result will be text, but the if statement checking against a number does work, try it.

if you do a simple robocopy (copy one file) and try the if statement, it passes. Then if you change permission on the target file to deny and retry robocopy (to get an error) then try the if statement it picks up the failure? we have used this methods for several years. Does this work for you?

Tom

April 3rd, 2012 10:48am

Hi,

You can test this yourself without using robocopy; e.g.:

PS C:\> "This is a test string" -gt 7
True
PS C:\> "This is a test string" -eq 0
False
PS C:\> "0" -eq 0
True

The reason for these results is that PowerShell is coercing the values automatically to sensible types so the comparison won't throw an error. In other words, I think your technique is working completely by accident. If you want to check robocopy's exit code, use the $LASTEXITCODE variable.

Bill

Free Windows Admin Tool Kit Click here and download it now
April 3rd, 2012 1:55pm

Starting robocopy with Start-Process, you can assign this to a variable to get a numeric Error Code using something like this:

$RobocopyResult = Start-Process -FilePath robocopy -ArgumentList $RobocopyParams -Wait -PassThru

$RobocopyResult.ExitCode

Note that -PassThru is important, otherwise $RobocopyResult won't have any value.

September 2nd, 2015 8:30pm

Note that RoboCopy does not return zero on success most of the time.  "0" (zero means NO files were copied.'

001   1       One or more files were copied successfully (that is, new files have arrived).

http://ss64.com/nt/robocopy-exit.html

Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2015 8:52pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics