Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Windows Powershell中修复此Robocopy错误?_Windows_Powershell_Windows Server 2012 R2 - Fatal编程技术网

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

如何在Windows Powershell中修复此Robocopy错误?,windows,powershell,windows-server-2012-r2,Windows,Powershell,Windows Server 2012 R2,你好, 在跟随@Trevor Sullivan发布的教程以回应时,我无法让脚本正常工作。它失败了,因为它找不到日志文件。起初我认为这可能是权限问题,所以我明确指定了日志文件的文件路径,但仍然收到相同的错误消息 如何修复代码以使其运行 代码如下: function Copy-WithProgress { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [string]

你好,

在跟随@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内容行并使用文本路径进行测试。如果仍然失败,请尝试添加-Force开关。

您需要发布代码和错误,而不是链接。谢谢,我以前这么做过,我因为发布太多代码而被责骂。这就是为什么我张贴了粘贴箱。但是我会编辑并发布它们,谢谢。为什么你有两个反斜杠?这可能是你的问题。在文件路径中去掉它们,它可能会开始工作。还有,到处都是
\'
是怎么回事?请原谅,我没有看到你指的\\。