【问题标题】:RoboCopy "Access Denied" Does not return failureRoboCopy“拒绝访问”不返回失败
【发布时间】:2018-10-09 18:40:15
【问题描述】:

我有一个 powershell 脚本,可以使用 robocopy 将文件从一台计算机复制到另一台 UNC。最近,它使用一个新的用户帐户运行,并在目标上获得了拒绝访问。 重试限制设置为 10,因此此时失败。 问题是命令的返回并没有显示失败,退出码是0,所以没有捕捉到失败。 完整日志如下。您可以在顶部看到使用的选项:(仅更改了服务器名称) ( 最后一行来自 PS 命令

Write-Host "Robocopy.exe 使用代码 $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

即使在这一点上,我唯一的预感是特定错误“访问目标目录”有些奇怪。当我尝试简单地删除文件权限的测试时,我收到错误

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

并且 robocopy 正确返回退出代码 8,FAILED 计数 = 1。

TIA

JS

【问题讨论】:

  • 但是 robocopy 没有失败;它完成了(它继续过去的“拒绝访问”消息)。 robocopy documentation 没有列出您想要的特定退出代码。您需要写出日志文件并在之后检查以确定您的情况是否发生。
  • 如果 robocopy 遇到不同的错误,通常它会失败。 E:G:
  • 如果 robocopy 遇到不同的错误,通常它会失败。 E:G:从 c:\temp\target 删除所有继承的权限。此命令:robocopy.exe C:\temp\Source C:\temp\target /e /purge /r:2 /w: write-host "exit code : $lastexitcode" 返回:Older 3 Fiel.txt 2018/10 /10 10:46:00 错误 5 (0x00000005) 复制文件 C:\temp\Source\Fiel.txt 访问被拒绝。错误:超过重试限制。退出代码:8
  • 重点是robocopy的行为就是这样;你将无法改变它。如果它没有捕获您想要的错误,我建议创建一个日志文件(或捕获标准输出,如答案所述)并以这种方式检查错误。

标签: powershell robocopy


【解决方案1】:

robocopy 是一个 cmd 可执行文件,并且不会向 powershell 返回 $false,如果这是您正在寻找的。一个很好的例子是 ping 命令总是返回 0 的退出代码。您需要使用 powershell 命令。 copy-item 是一个好的开始。

如果您必须使用 robocopy,那么您可以将其放入一个变量中,并在字符串中检查“访问被拒绝”等响应。 不知道你的代码是什么,或者你在复制什么......但是一个想法......

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

【讨论】:

  • 不,它返回一个退出代码,可以通过内置的 $lastExitCode 在 powershell 中访问。 :
  • 您可以在 $lastexitcode 上运行 if 检查,但如果它退出是相同的,无论它是否失败,那么您将不得不做一个输出解析技巧。 CMD.exe 只是输出文本。当涉及到 powershell 错误检查时,可能会导致很多此类问题。
【解决方案2】:

我看到这个并意识到它是从 2018 年开始的 - 但我觉得我需要回答。这可能会得到零票 - 但这里是:

  1. 首先,您应该(如果您还没有)下载 Linux/Unix 命令的 Windows 版本,例如“grep”。 Grep 非常强大,让生活变得轻松。

注意:因为 Windows 现在包含 GREP 函数 - 您可能必须将 unix 命令从“grep”重命名为“fgrep”或“ugrep” - 至少我必须这样做。

  1. 您可能需要使用(或应该使用)“/log:”和“/tee”选项。 /log 选项显然允许您创建日志文件,但 /tee 允许您在屏幕上查看日志文件。两者都是不错的选择。

robocopy 完成后 - 您可以使用 grep。

好的 - robocopy 已经完成。然后你可以输入:

C:>fgrep -i "错误"

这应该会为您提供日志文件中的每一行,其中包含由空格包围的单词 ERROR。还有一个选项可以列出消息所在的行,如下所示:

C:>fgrep -in "错误"

这是一个例子:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    相关资源
    最近更新 更多