76 lines
2.5 KiB
PowerShell
76 lines
2.5 KiB
PowerShell
|
|
#Running batches as jobs
|
||
|
|
|
||
|
|
|
||
|
|
function Get-PCBatchJob{
|
||
|
|
|
||
|
|
[CmdletBinding()]
|
||
|
|
param (
|
||
|
|
$Computers,
|
||
|
|
[switch]$printers
|
||
|
|
)
|
||
|
|
$modulePath = (get-item $PSScriptRoot).Parent.FullName
|
||
|
|
$modulePath = "$modulePath\Get-PC.psd1"
|
||
|
|
$TotalItems = $Computers.count
|
||
|
|
$ActiveJobs = @()
|
||
|
|
$itemIndex = 0
|
||
|
|
foreach ($c in $computers){
|
||
|
|
|
||
|
|
if($printers){
|
||
|
|
JobsProgressBar $c $itemIndex $TotalItems 1
|
||
|
|
$ActiveJobs += Start-Job -Name $c -ArgumentList $c,$modulePath -ScriptBlock{
|
||
|
|
Import-Module $args[1] -WarningAction Ignore
|
||
|
|
get-pc $args[0] -SHSPrinter}
|
||
|
|
}
|
||
|
|
else{
|
||
|
|
JobsProgressBar $c $itemIndex $TotalItems 1
|
||
|
|
$ActiveJobs += Start-Job -Name $c -ArgumentList $c,$modulePath -ScriptBlock{
|
||
|
|
Import-Module $args[1] -WarningAction Ignore
|
||
|
|
get-pc $args[0]}
|
||
|
|
}
|
||
|
|
$itemIndex++
|
||
|
|
}
|
||
|
|
|
||
|
|
$completedJobs = @()
|
||
|
|
$itemIndex = 0
|
||
|
|
JobsProgressBar 'Waiting on jobs to be' $itemIndex $TotalItems 2
|
||
|
|
While(!$jobsDone){
|
||
|
|
$jobsDone = $true
|
||
|
|
foreach ($job in $ActiveJobs){
|
||
|
|
if($job.State -eq 'Running'){
|
||
|
|
$jobsDone = $false
|
||
|
|
}
|
||
|
|
if($job.State -eq 'Completed' -and !$completedJobs.Contains($job)){
|
||
|
|
$itemIndex++
|
||
|
|
JobsProgressBar $job.Name $itemIndex $TotalItems 2
|
||
|
|
$completedJobs += $job
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Start-Sleep -Milliseconds 1
|
||
|
|
}
|
||
|
|
$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 {}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|