61 lines
2 KiB
PowerShell
61 lines
2 KiB
PowerShell
#Running batches as jobs
|
|
|
|
|
|
function Get-PCBatchJob{
|
|
|
|
[CmdletBinding()]
|
|
param (
|
|
$Computers,
|
|
[switch]$printers
|
|
)
|
|
$TotalItems = $Computers.count
|
|
$ActiveJobs = @()
|
|
$itemIndex = 0
|
|
foreach ($c in $computers){
|
|
|
|
if($printers){
|
|
JobsProgressBar $c $itemIndex $TotalItems 1
|
|
$ActiveJobs += Start-Job -Name $c -ScriptBlock ${function:Get-SHSPrinter} -ArgumentList $c
|
|
}
|
|
else{
|
|
JobsProgressBar $c $itemIndex $TotalItems 1
|
|
$ActiveJobs += Start-Job -Name $c -ScriptBlock ${function:get-pc} -ArgumentList $c
|
|
}
|
|
$itemIndex++
|
|
}
|
|
|
|
$completedJobs = @()
|
|
$itemIndex = 0
|
|
JobsProgressBar 'Waiting on jobs to be' $itemIndex $TotalItems 2
|
|
While($ActiveJobs.Length){
|
|
$job = Wait-Job -Any $ActiveJobs
|
|
$itemIndex++
|
|
JobsProgressBar $job.Name $itemIndex $TotalItems 2
|
|
$completedJobs += $job
|
|
$ActiveJobs = $ActiveJobs | Where-Object { $_.Name -ne $job.Name }
|
|
}
|
|
$itemIndex = 0
|
|
foreach($job in $completedJobs) {
|
|
$itemIndex++
|
|
JobsProgressBar $job.Name $itemIndex $TotalItems 3
|
|
$output = Receive-Job $job | Select-Object * -ExcludeProperty RunspaceId, PSComputerName, PSShowComputerName
|
|
Remove-job $job
|
|
Write-Output $output
|
|
}
|
|
}
|
|
|
|
function JobsProgressBar {
|
|
param (
|
|
$item,
|
|
$currentItemIndex,
|
|
$TotalItems,
|
|
$stage
|
|
)
|
|
switch ($stage) {
|
|
1 { Write-Progress -Activity "Starting Jobs" -Status "$item Job Started ($currentItemIndex/$TotalItems)" -PercentComplete (($currentItemIndex/$TotalItems) * 100)}
|
|
2 { Write-Progress -Activity "Jobs Running" -Status "$item Completed ($currentItemIndex/$TotalItems)" -PercentComplete (($currentItemIndex/$TotalItems) * 100)}
|
|
3 { Write-Progress -Activity "Retrieving Data" -Status "$item Completed ($currentItemIndex/$TotalItems)" -PercentComplete (($currentItemIndex/$TotalItems) * 100)}
|
|
Default {}
|
|
}
|
|
|
|
} |