繁体   English   中英

如何在 Windows Powershell 中修复此 Robocopy 错误?

[英]How do I fix this Robocopy error in Windows Powershell?

再会,

在遵循@Trevor Sullivan 针对此问题发布的教程时,我无法使脚本正常工作。 它失败了,理由是它找不到日志文件。 起初我认为这可能是权限问题,所以我明确指定了日志文件的文件路径,但仍然收到相同的错误消息。

如何修复代码以使其运行?

这是代码:

function Copy-WithProgress {
    [CmdletBinding()]
    param (
            [Parameter(Mandatory = $true)]
            [string] $Source
        , [Parameter(Mandatory = $true)]
            [string] $Destination
        , [int] $Gap = 0
        , [int] $ReportGap = 2000
    )
    # Define regular expression that will gather number of bytes copied
    $RegexBytes = '(?<=\s+)\d+(?=\s+)';

    #region Robocopy params
    # MIR = Mirror mode
    # NP  = Don't show progress percentage in log
    # NC  = Don't log file classes (existing, new file, etc.)
    # BYTES = Show file sizes in bytes
    # NJH = Do not display robocopy job header (JH)
    # NJS = Do not display robocopy job summary (JS)
    # TEE = Display log in stdout AND in target log file
    $CommonRobocopyParams = '/MIR /COPYALL /DCOPY:T /SECFIX /NP /NDL /NC /BYTES /NJH /NJS';
    #endregion Robocopy params

    #region Robocopy Staging
    Write-Verbose -Message 'Analyzing robocopy job ...';
    $StagingLogPath = '{0}\Temp\{1} robocopy staging.log' -f $env:windir, (Get-Date -Format 'yyyy-MM-dd hh-mm-ss');

    $StagingArgumentList = '"{0}" "{1}" /LOG:"{2}" /L {3}' -f $Source, $Destination, $StagingLogPath, $CommonRobocopyParams;
    Write-Verbose -Message ('Staging arguments: {0}' -f $StagingArgumentList);
    Start-Process -Wait -FilePath robocopy.exe -ArgumentList $StagingArgumentList -NoNewWindow;
    # Get the total number of files that will be copied
    $StagingContent = Get-Content -Path $StagingLogPath;
    $FileCount = $StagingContent.Count;

    # Get the total number of bytes to be copied
    [RegEx]::Matches(($StagingContent -join "`n"), $RegexBytes) | % { $BytesTotal = 0; } { $BytesTotal += $_.Value; };
    Write-Verbose -Message ('Total bytes to be copied: {0}' -f $BytesTotal);
    #endregion Robocopy Staging

    #region Start Robocopy
    # Begin the robocopy process
    $RobocopyLogPath = '{0}\Temp\{1} robocopy.log' -f $env:windir, (Get-Date -Format 'yyyy-MM-dd hh-mm-ss');
    $ArgumentList = '"{0}" "{1}" /LOG:"{2}" /ipg:{3} {4}' -f $Source, $Destination, $RobocopyLogPath, $Gap, $CommonRobocopyParams;
    Write-Verbose -Message ('Beginning the robocopy process with arguments: {0}' -f $ArgumentList);
    $Robocopy = Start-Process -FilePath robocopy.exe -ArgumentList $ArgumentList -Verbose -PassThru -NoNewWindow;
    Start-Sleep -Milliseconds 100;
    #endregion Start Robocopy

    #region Progress bar loop
    while (!$Robocopy.HasExited) {
        Start-Sleep -Milliseconds $ReportGap;
        $BytesCopied = 0;
        $LogContent = Get-Content -Path $RobocopyLogPath;
        $BytesCopied = [Regex]::Matches($LogContent, $RegexBytes) | ForEach-Object -Process { $BytesCopied += $_.Value; } -End { $BytesCopied; };
        Write-Verbose -Message ('Bytes copied: {0}' -f $BytesCopied);
        Write-Verbose -Message ('Files copied: {0}' -f $LogContent.Count);
        Write-Progress -Activity Robocopy -Status ("Copied {0} files; Copied {1} of {2} bytes" -f $LogContent.Count, $BytesCopied, $BytesTotal) -PercentComplete (($BytesCopied/$BytesTotal)*100);
    }
    #endregion Progress loop

    #region Function output
    [PSCustomObject]@{
        BytesCopied = $BytesCopied;
        FilesCopied = $LogContent.Count;
    };
    #endregion Function output
}

# Call the Copy-WithProgress function
Copy-WithProgress -Source "M:\" -Destination "E:\Homes" -Verbose;

这是错误:

PS C:\Users\Administrator> C:\Users\Administrator\Documents\PowerShell Scripts\Copy_With_Progress.ps1
VERBOSE: Analyzing robocopy job ...
VERBOSE: Staging arguments: "M:\" "E:\Homes" /LOG:"C:\Windows\Temp\2014-09-29 02-11-54 robocopy staging.lo
g" /L /MIR /COPYALL /DCOPY:T /SECFIX /NP /NDL /NC /BYTES /NJH /NJS
Get-Content : Cannot find path 'C:\Windows\Temp\2014-09-29 02-11-54 robocopy staging.log' because it
does not exist.
At C:\Users\Administrator\Documents\PowerShell Scripts\Copy_With_Progress.ps1:33 char:23
+     $StagingContent = Get-Content -Path $StagingLogPath;
+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Windows\Temp...opy staging.log:String) [Get-Content],  
   ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

VERBOSE: Total bytes to be copied: 0
VERBOSE: Beginning the robocopy process with arguments: "M:\" "E:\Homes" /LOG:"C:\Windows\Temp\2014-09-29
02-11-55 robocopy.log" /ipg:0 /MIR /COPYALL /DCOPY:T /SECFIX /NP /NDL /NC /BYTES /NJH /NJS
Get-Content : Cannot find path 'C:\Windows\Temp\2014-09-29 02-11-55 robocopy.log' because it does not
exist.
At C:\Users\Administrator\Documents\PowerShell Scripts\Copy_With_Progress.ps1:54 char:23
+         $LogContent = Get-Content -Path $RobocopyLogPath;
+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Windows\Temp...55 robocopy.log:String) [Get-Content],  
   ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Exception calling "Matches" with "2" argument(s): "Value cannot be null.
Parameter name: input"
At C:\Users\Administrator\Documents\PowerShell Scripts\Copy_With_Progress.ps1:55 char:9
+         $BytesCopied = [Regex]::Matches($LogContent, $RegexBytes) | ForEach-Obje ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException

VERBOSE: Bytes copied: 0
VERBOSE: Files copied: 0
Attempted to divide by zero.
At C:\Users\Administrator\Documents\PowerShell Scripts\Copy_With_Progress.ps1:58 char:9
+         Write-Progress -Activity Robocopy -Status ("Copied {0} files; Copied {1} ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo          : NotSpecified: (:) [], RuntimeException
   + FullyQualifiedErrorId : RuntimeException


                                        BytesCopied                                          FilesCopied
                                        -----------                                          -----------
                                                  0                                                    0



PS C:\Users\Administrator> C:\Users\Administrator\Documents\PowerShell Scripts\Copy_With_Progress.ps1
VERBOSE: Analyzing robocopy job ...
VERBOSE: Staging arguments: "M:\" "E:\Homes" /LOG:"E:\Logs\2014-09-29 02-16-10 r
obocopy staging.log" /L /MIR /COPYALL /DCOPY:T /SECFIX /NP /NDL /NC /BYTES /NJH
/NJS
Get-Content : Cannot find path 'E:\Logs\2014-09-29 02-16-10 robocopy
staging.log' because it does not exist.
At C:\Users\Administrator\Documents\PowerShell
Scripts\Copy_With_Progress.ps1:33 char:23
+     $StagingContent = Get-Content -Path $StagingLogPath;
+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo          : ObjectNotFound: (E:\Logs\2014-09...opy staging.l
  og:String) [Get-Content], ItemNotFoundException
   + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCo
  ntentCommand

VERBOSE: Total bytes to be copied: 0
VERBOSE: Beginning the robocopy process with arguments: "M:\" "E:\Homes" /LOG:"E
:\Logs\2014-09-29 02-16-11 robocopy.log" /ipg:0 /MIR /COPYALL /DCOPY:T /SECFIX /
NP /NDL /NC /BYTES /NJH /NJS
Get-Content : Cannot find path 'E:\Logs\2014-09-29 02-16-11 robocopy.log'
because it does not exist.
At C:\Users\Administrator\Documents\PowerShell
Scripts\Copy_With_Progress.ps1:54 char:23
+         $LogContent = Get-Content -Path $RobocopyLogPath;
+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo          : ObjectNotFound: (E:\Logs\2014-09-29 02-16-11 rob
  ocopy.log:String) [Get-Content], ItemNotFoundException
   + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCo
  ntentCommand

Exception calling "Matches" with "2" argument(s): "Value cannot be null.
Parameter name: input"
At C:\Users\Administrator\Documents\PowerShell
Scripts\Copy_With_Progress.ps1:55 char:9
+         $BytesCopied = [Regex]::Matches($LogContent, $RegexBytes) |
ForEach-Obje ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
   + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
   + FullyQualifiedErrorId : ArgumentNullException

VERBOSE: Bytes copied: 0
VERBOSE: Files copied: 0
Attempted to divide by zero.
At C:\Users\Administrator\Documents\PowerShell
Scripts\Copy_With_Progress.ps1:58 char:9
+         Write-Progress -Activity Robocopy -Status ("Copied {0} files; Copied
{1} ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException


                            BytesCopied                             FilesCopied
                            -----------                             -----------
                                      0                                       0



PS C:\Users\Administrator>

看看你的代码,就像有人用反斜杠转义了一样。 替换所有的\\\\\\并替换\\'' 看看这是否不能解决您的问题。

隔离 Get-Content 行并使用文字路径进行测试。 如果仍然失败,请尝试添加 -Force 开关。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM