'RoboCopy "Access Denied" Does not return failure

I have a powershell script to copy files from one computer to another UNC, using robocopy. Recently, it was run with a new user account, and got an Access Denied on the target. The retry limit was set to 10, so it failed out at that point. The problem is that the returns of the command do not show that there was a failure, and the exit code was 0, so the failure was not caught. Full logs below. You can see the options used at the top : (only server names changed) ( The last line is from the PS command

Write-Host "Robocopy.exe completed with code $lastexitcode")

[18:54:04][Step 1/1]   Started : Monday, October 8, 2018 6:54:04 PM
[18:54:04][Step 1/1]    Source : C:\Source
[18:54:04][Step 1/1]      Dest : \\Server\Target
[18:54:04][Step 1/1] 
[18:54:04][Step 1/1]     Files : *.*
[18:54:04][Step 1/1]        
[18:54:04][Step 1/1]   Options : *.* /S /E /DCOPY:DA /COPY:DAT /PURGE /NP /R:2 /W:30 
[18:54:04][Step 1/1] 
[18:54:04][Step 1/1] ------------------------------------------------------------------------------
[18:54:04][Step 1/1] 
[18:54:06][Step 1/1]                     438    C:\Source
[18:54:06][Step 1/1] 2018/10/08 18:54:06 ERROR 5 (0x00000005) Accessing Destination Directory \\Server\Target
[18:54:06][Step 1/1] Access is denied.
[18:54:06][Step 1/1] 
[18:54:38][Step 1/1] Waiting 30 seconds... Retrying...
[18:54:38][Step 1/1] 2018/10/08 18:54:38 ERROR 5 (0x00000005) Accessing Destination Directory \\Server\Target
[18:54:38][Step 1/1] Access is denied.
[18:54:38][Step 1/1] 
[18:55:10][Step 1/1] Waiting 30 seconds... Retrying...
[18:55:10][Step 1/1] 2018/10/08 18:55:10 ERROR 5 (0x00000005) Accessing Destination Directory \\Server\Target
[18:55:10][Step 1/1] Access is denied.
[18:55:10][Step 1/1] 
[18:55:10][Step 1/1] 
[18:55:10][Step 1/1] ERROR: RETRY LIMIT EXCEEDED.
[18:55:10][Step 1/1] 
[18:55:10][Step 1/1] 
[18:55:10][Step 1/1] ------------------------------------------------------------------------------
[18:55:10][Step 1/1] 
[18:55:10][Step 1/1]                Total    Copied   Skipped  Mismatch    FAILED    Extras
[18:55:10][Step 1/1]     Dirs :         1         0         1         0         0         0
[18:55:10][Step 1/1]    Files :         0         0         0         0         0         0
[18:55:10][Step 1/1]    Bytes :         0         0         0         0         0         0
[18:55:10][Step 1/1]    Times :   0:01:06   0:00:00                       0:01:00   0:00:06
[18:55:10][Step 1/1]    Ended : Monday, October 8, 2018 6:55:10 PM
[18:55:10][Step 1/1] 
[18:55:10][Step 1/1] Robocopy.exe completed with code 0

My only hunch even at this point is that there is something strange about the specific error "Accessing Destination Directory". When I try a test simply removing file permissions, I get the error

14:08:15 ERROR 5 (0x00000005) Copying File

and robocopy correctly returns exit code 8, with FAILED count = 1.

TIA

JS



Solution 1:[1]

robocopy is a cmd executable, and will not return a $false to powershell, if that's what you're looking for. a good example is the ping command always returns an exit code of 0. You'll need to utilize a powershell command. copy-item is a good start.

If you must MUST use robocopy, then you could put it into a variable, and have the response checked for "access denied" etc.. in the string. not sure what your code is, or what you're copying... but an idea...

$temp = robocopy *.* /s... etc...
if ($temp -match "Access is denied"){"Robocopy failed due to a denial of access"}

Solution 2:[2]

I saw this AND realize it is from 2018 - but I felt I needed to answer. This will probably get zero votes - but here goes:

  1. First, you should (if you haven't already) download the Windows version of the Linux/Unix commands like "grep". Grep is very powerful and makes life easy.

NOTE : Because Windows now contains a GREP function - you will probably have to rename the unix command from "grep" to "fgrep" or maybe "ugrep" - at least I had to do so.

  1. You probably need to use (or should use) the "/log:<logfile>" and "/tee" options. The /log option obviously lets you make a log file but the /tee allows you to see, on your screen, the log file. Both are nice options to use.

Once robocopy has finished - you can then use grep.

Ok - so robocopy has finished. Then you can just type:

C:>fgrep -i " error "

This should then give you every line in the log file which has the word ERROR surrounded by spaces. There is also an option to list which line the message is on like so:

C:>fgrep -in " error "

And here is an example:

D:>fgrep -in " error " robocopy.log

17:2022/02/26 15:23:22 ERROR 32 (0x00000020) Copying NTFS Security to Destination File C:\DumpStack.log.tmp
21:2022/02/26 15:23:22 ERROR 32 (0x00000020) Copying NTFS Security to Destination File C:\hiberfil.sys
25:2022/02/26 15:23:22 ERROR 32 (0x00000020) Copying NTFS Security to Destination File C:\pagefile.sys
29:2022/02/26 15:23:22 ERROR 2 (0x00000002) Copying NTFS Security to Destination File C:\RAMDisk.img
33:2022/02/26 15:23:22 ERROR 32 (0x00000020) Copying NTFS Security to Destination File C:\swapfile.sys
39:2022/02/26 15:23:24 ERROR 5 (0x00000005) Copying NTFS Security to Destination File C:\System Volume Information\1{3808876b-c176-4e48-b7ae-04046e6cc752}
43:2022/02/26 15:23:24 ERROR 2 (0x00000002) Copying NTFS Security to Destination File C:\System Volume Information\IndexerVolumeGuid
47:2022/02/26 15:23:24 ERROR 2 (0x00000002) Copying NTFS Security to Destination File C:\System Volume Information\ISwift3.dat
51:2022/02/26 15:23:24 ERROR 32 (0x00000020) Copying NTFS Security to Destination File C:\System Volume Information\klBackupDepository.dat
55:2022/02/26 15:23:24 ERROR 2 (0x00000002) Copying NTFS Security to Destination File C:\System Volume Information\kliddb.dat
59:2022/02/26 15:23:24 ERROR 2 (0x00000002) Copying NTFS Security to Destination File C:\System Volume Information\klmeta.dat
63:2022/02/26 15:23:24 ERROR 2 (0x00000002) Copying NTFS Security to Destination File C:\System Volume Information\klobjdb.dat
67:2022/02/26 15:23:24 ERROR 2 (0x00000002) Copying NTFS Security to Destination File C:\System Volume Information\MountPointManagerRemoteDatabase
72:2022/02/26 15:23:24 ERROR 2 (0x00000002) Copying NTFS Security to Destination File C:\System Volume Information\WPSettings.dat

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Robert Cotterman
Solution 2 Mark Manning