2024-06-11 18:27:55 +00:00
#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
2024-07-26 18:41:04 +00:00
$ActiveJobs + = Start-Job -Name $c -ScriptBlock $ { function : Get-SHSPrinter } -ArgumentList $c
2024-06-11 18:27:55 +00:00
}
else {
JobsProgressBar $c $itemIndex $TotalItems 1
2024-07-26 18:41:04 +00:00
$ActiveJobs + = Start-Job -Name $c -ScriptBlock $ { function : get-pc } -ArgumentList $c
2024-06-11 18:27:55 +00:00
}
$itemIndex + +
}
$completedJobs = @ ( )
$itemIndex = 0
JobsProgressBar 'Waiting on jobs to be' $itemIndex $TotalItems 2
2024-07-26 18:41:04 +00:00
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 }
2024-06-11 18:27:55 +00:00
}
$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 { }
}
}