Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Copy-WithProgress {
  2.     [CmdletBinding()]
  3.     param (
  4.             [Parameter(Mandatory = $true)]
  5.             [string] $Source
  6.         , [Parameter(Mandatory = $true)]
  7.             [string] $Destination
  8.         , [int] $Gap = 0
  9.         , [int] $ReportGap = 2000
  10.     )
  11.     # Define regular expression that will gather number of bytes copied
  12.     $RegexBytes = '(?<=\s+)\d+(?=\s+)';
  13.  
  14.     #region Robocopy params
  15.     # MIR = Mirror mode
  16.     # NP  = Don't show progress percentage in log
  17.     # NC  = Don't log file classes (existing, new file, etc.)
  18.     # BYTES = Show file sizes in bytes
  19.     # NJH = Do not display robocopy job header (JH)
  20.     # NJS = Do not display robocopy job summary (JS)
  21.     # TEE = Display log in stdout AND in target log file
  22.     $CommonRobocopyParams = '/MIR /SEC /NP /NDL /NC /BYTES /NJH /NJS';
  23.     #endregion Robocopy params
  24.  
  25.     #region Robocopy Staging
  26.     Write-Verbose -Message 'Analyzing robocopy job ...';
  27.     $StagingLogPath = '{0}\temp\{1} robocopy staging.log' -f $env:windir, (Get-Date -Format 'yyyy-MM-dd HH-mm-ss');
  28.  
  29.     $StagingArgumentList = '"{0}" "{1}" /LOG:"{2}" /L {3}' -f $Source, $Destination, $StagingLogPath, $CommonRobocopyParams;
  30.     Write-Verbose -Message ('Staging arguments: {0}' -f $StagingArgumentList);
  31.     Start-Process -Wait -FilePath robocopy.exe -ArgumentList $StagingArgumentList -NoNewWindow;
  32.     # Get the total number of files that will be copied
  33.     $StagingContent = Get-Content -Path $StagingLogPath;
  34.     $TotalFileCount = $StagingContent.Count - 1;
  35.  
  36.     # Get the total number of bytes to be copied
  37.     [RegEx]::Matches(($StagingContent -join "`n"), $RegexBytes) | % { $BytesTotal = 0; } { $BytesTotal += $_.Value; };
  38.     Write-Verbose -Message ('Total bytes to be copied: {0}' -f $BytesTotal);
  39.     #endregion Robocopy Staging
  40.  
  41.     #region Start Robocopy
  42.     # Begin the robocopy process
  43.     $RobocopyLogPath = '{0}\temp\{1} robocopy.log' -f $env:windir, (Get-Date -Format 'yyyy-MM-dd HH-mm-ss');
  44.     $ArgumentList = '"{0}" "{1}" /LOG:"{2}" /ipg:{3} {4}' -f $Source, $Destination, $RobocopyLogPath, $Gap, $CommonRobocopyParams;
  45.     Write-Verbose -Message ('Beginning the robocopy process with arguments: {0}' -f $ArgumentList);
  46.     $Robocopy = Start-Process -FilePath robocopy.exe -ArgumentList $ArgumentList -Verbose -PassThru -NoNewWindow;
  47.     Start-Sleep -Milliseconds 100;
  48.     #endregion Start Robocopy
  49.  
  50.     #region Progress bar loop
  51.     while (!$Robocopy.HasExited) {
  52.         Start-Sleep -Milliseconds $ReportGap;
  53.         $BytesCopied = 0;
  54.         $LogContent = Get-Content -Path $RobocopyLogPath;
  55.         $BytesCopied = [Regex]::Matches($LogContent, $RegexBytes) | ForEach-Object -Process { $BytesCopied += $_.Value; } -End { $BytesCopied; };
  56.         $CopiedFileCount = $LogContent.Count - 1;
  57.         Write-Verbose -Message ('Bytes copied: {0}' -f $BytesCopied);
  58.         Write-Verbose -Message ('Files copied: {0}' -f $LogContent.Count);
  59.         $Percentage = 0;
  60.         if ($BytesCopied -gt 0) {
  61.            $Percentage = (($BytesCopied/$BytesTotal)*100)
  62.         }
  63.         Write-Progress -Activity Robocopy -Status ("Copied {0} of {1} files; Copied {2} of {3} bytes" -f $CopiedFileCount, $TotalFileCount, $BytesCopied, $BytesTotal) -PercentComplete $Percentage
  64.     }
  65.     #endregion Progress loop
  66.  
  67.     #region Function output
  68.     [PSCustomObject]@{
  69.         BytesCopied = $BytesCopied;
  70.         FilesCopied = $CopiedFileCount;
  71.     };
  72.     #endregion Function output
  73. }
  74.  
  75. $SourceToCopy = "I:"
  76. $DestinationToCopy = "O:"
  77.  
  78. Copy-WithProgress -Source "I:" -Destination "O:" -Verbose;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement