Custom RoboCopy Progress Bar in PowerShell

后端 未结 7 1050
夕颜
夕颜 2020-11-28 03:51

I\'m interested in a PowerShell script that copies a large amount of files from a server daily and I\'m interested in implementing a in-console progress bar like

7条回答
  •  被撕碎了的回忆
    2020-11-28 04:39

    I ended up using this based on Amrinder's suggested answer:

    robocopy.exe $Source $Destination $PatternArg $MirrorArg /NDL /NJH /NJS | ForEach-Object -Process {
        $data = $_.Split([char]9);
        if (($data.Count -gt 4) -and ("$($data[4])" -ne ""))
        {
            $file = "$($data[4])"
            Write-Progress "Percentage $($data[0])" -Activity "Robocopy" -CurrentOperation "$($file)" -ErrorAction SilentlyContinue; 
        }
        else
        {
            Write-Progress "Percentage $($data[0])" -Activity "Robocopy" -CurrentOperation "$($file)"
        }
    }
    # Robocopy has a bitmask set of exit codes, so only complain about failures:
    [int] $exitCode = $global:LastExitCode;
    [int] $someCopyErrors = $exitCode -band 8;
    [int] $seriousError = $exitCode -band 16;
    if (($someCopyErrors -ne 0) -or ($seriousError -ne 0))
    {
        Write-Error "ERROR: robocopy failed with a non-successful exit code: $exitCode"
        exit 1
    }
    

    Fyi, Bill

提交回复
热议问题