Created
March 9, 2018 02:39
-
-
Save TLaborde/3a7d2591b5554c83d6821b93eb560789 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$global:queue = [System.Collections.Queue]::Synchronized( (New-Object System.Collections.Queue) ) | |
foreach($item in $jobInput) | |
{ | |
$global:queue.Enqueue($item) | |
} | |
$global:resultCode = [hashtable]::Synchronized(@{}) | |
function RunJobFromQueue { | |
if( $global:queue.Count -gt 0) { | |
$j = Start-Job -ScriptBlock { | |
param($folder,$queue,$resultCode) | |
$VerbosePreference = "continue" | |
$null = Start-Transcript -Path "C:\logs\dqueued\$($folder.foldername).$(get-date -f 'yyyyMMdd').log" -Append | |
Write-Verbose "Copy from source to new FS with robocopy. Params:" | |
$RobocopyParams ="$($folder.folderfullname) \\path\$($folder.foldername) /mir /b /copyall /secfix /timfix /mt:16 /dcopy:dat /r:3 /w:3 /unilog+:C:\logs\dirfs-box\$($folder.foldername).$(get-date -f 'yyyyMMdd').log" | |
Write-Verbose $RobocopyParams | |
$RobocopyResult = Start-Process -FilePath robocopy -ArgumentList $RobocopyParams -Wait -PassThru -WindowStyle Hidden | |
Write-Verbose "Result Code of robocopy: $($RobocopyResult.ExitCode)" | |
"Result Code of robocopy for $($folder.foldername): $($RobocopyResult.ExitCode)" | |
$resultCode[$folder.foldername] = $RobocopyResult.ExitCode | |
$null = Stop-Transcript | |
$VerbosePreference = "silentlycontinue" | |
} -ArgumentList @(,$global:queue.Dequeue(),$global:queue, $global:resultCode) | |
Register-ObjectEvent -InputObject $j -EventName StateChanged -Action { | |
$VerbosePreference = "silentlycontinue" | |
RunJobFromQueue | |
$results = Receive-Job -Job $eventsubscriber.sourceobject | |
Remove-Job -Job $eventsubscriber.sourceobject | |
Unregister-Event $eventsubscriber.SourceIdentifier | |
Remove-Job -Name $eventsubscriber.SourceIdentifier | |
$VerbosePreference = "silentlycontinue" | |
Write-Host $results | |
Write-Host "Remaining Jobs: $($global:queue.Count)" | |
} | Out-Null | |
} | |
} | |
# Start up to the max number of concurrent jobs | |
# Each job will take care of running the rest | |
for( $i = 0; $i -lt $maxConcurrentJobs; $i++ ) { | |
RunJobFromQueue | |
} | |
for( $i = 0; $i -lt $maxConcurrentJobs; $i++ ) { | |
RunJobFromQueue | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment