Last active
December 19, 2022 19:36
-
-
Save tankerkiller125/f504a9b1df0241fc5f3141a28be81bf5 to your computer and use it in GitHub Desktop.
PowerShell Queue Task
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
$ScriptBlock = { | |
param($data) | |
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; | |
# Transfer the Mailbox | |
Search-Mailbox $data.SourceUser -TargetMailbox $data.TargetUser -TargetFolder "Archives" | |
Write-Host "Completed:" $data | |
} | |
$Transfers = Import-Csv transfers.csv | |
Invoke-Queue -InputObject $Transfers -ThrottleLimit 3 -Scriptblock $ScriptBlock -ShowProgress | |
While (Get-Job -State "Running") { Start-Sleep 5 } | |
Get-Job | Receive-Job | |
Remove-Job * |
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
function Invoke-Queue { | |
[CmdletBinding()] | |
[OutputType([System.Management.Automation.Job[]])] | |
param( | |
[Parameter(Mandatory)] | |
[ValidateNotNullOrEmpty()] | |
[System.Object[]] | |
$InputObject | |
, | |
[Parameter(Mandatory)] | |
[ValidateNotNullOrEmpty()] | |
[scriptblock] | |
$Scriptblock | |
, | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[System.Object[]] | |
$ArgumentList | |
, | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[int] | |
$ThrottleLimit = 32 | |
, | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[int] | |
$Delay = 2 | |
, | |
[switch] | |
$Wait | |
, | |
[switch] | |
$ShowProgress | |
, | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
$ProgressActivityMessage = 'Processed input objects' | |
) | |
Begin { | |
Write-Verbose ('[{0}] Initializing variables' -f $MyInvocation.MyCommand) | |
$Jobs = @() | |
$InputIndex = 0 | |
if ($Wait) { | |
Write-Verbose ('[{0}] Setting <ThrottleLimit> to 1 because <Wait> was specified.' -f $MyInvocation.MyCommand) | |
$ThrottleLimit = 1 | |
} | |
Write-Verbose ('[{0}] Processing {1} objects' -f $MyInvocation.MyCommand, $InputObject.Count) | |
} | |
Process { | |
while ($InputIndex -lt $InputObject.Count) { | |
Write-Debug ('[{0}] InputIndex={1} JobCount={2} RunningJobCount={3}' -f $MyInvocation.MyCommand, $InputIndex, $Jobs.Count, ($Jobs | Get-Job | Where-Object {$_.State -ieq 'Running'}).Count) | |
if ($Jobs.Count -lt $ThrottleLimit -or ($Jobs | Get-Job | Where-Object {$_.State -ieq 'Running'}).Count -lt $ThrottleLimit) { | |
Write-Verbose ('[{0}] New job for parameter index {1}' -f $MyInvocation.MyCommand, $InputIndex) | |
$Job = Start-Job -ScriptBlock $Scriptblock -Name $InputObject[$InputIndex].ToString() -ArgumentList (@($InputObject[$InputIndex]) + $ArgumentList) | |
$Jobs += $Job | |
Write-Progress -Activity $ProgressActivityMessage -Status "Invoked $($InputObject[$InputIndex].ToString())" -PercentComplete ($Jobs.Count / $InputObject.Count * 100) | |
if ($Wait) { | |
Write-Output ('[{0}] Receiving output for job <{1}>' -f $MyInvocation.MyCommand, $Job.Name) | |
$Job | Receive-Job -Wait | |
} | |
++$InputIndex | |
} | |
else { | |
Write-Debug ('[{0}] Waiting for a job to finish' -f $MyInvocation.MyCommand) | |
Start-Sleep -Seconds $Delay | |
} | |
} | |
Write-Verbose ('[{0}] All input objects are being processed' -f $MyInvocation.MyCommand) | |
Write-Progress -Activity $ProgressActivityMessage -Completed | |
$Jobs | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment