2024-06-11 18:27:55 +00:00
function Get-PCBatchInvoke {
[ CmdletBinding ( ) ]
param (
[ Parameter ( ) ]
[ string[] ]
$Computers
)
2024-09-23 21:39:12 +00:00
$Tenant = ( Connect-ISM ) [ 1 ]
$Headers = ( Connect-ISM ) [ 0 ]
2024-06-11 18:27:55 +00:00
$OnlineComputers = @ ( )
$OfflineComputers = @ ( )
$output = @ ( )
$itemIndex = 0
foreach ( $comp in $Computers ) {
$itemIndex + +
BatchInvokesProgressBar -item $comp -currentItemIndex $itemIndex -TotalItems $Computers . Count -stage 1
$Connection = Test-Connection -ComputerName $comp -Count 1
if ( $Connection ) {
$OnlineComputers + = $comp
}
else {
$OfflineComputers + = $comp
}
}
BatchInvokesProgressBar -stage 2
2024-09-13 23:32:38 +00:00
$invokeJob = Invoke-Command -ScriptBlock $ { function : Get-PCLocal } -ComputerName $OnlineComputers -SessionOption ( New-PSSessionOption -NoMachineProfile -OpenTimeout 45000 ) -AsJob
2024-07-31 00:26:27 +00:00
$offlineJobs = @ ( )
2024-07-24 23:04:24 +00:00
if ( $OfflineComputers . count -gt 0 ) {
foreach ( $computer in $OfflineComputers ) {
2024-07-31 00:26:27 +00:00
$offlineJobs + = Start-Job -ScriptBlock $ { function : Get-SCCMQueryBlock } -ArgumentList $computer
2024-07-24 23:04:24 +00:00
}
}
2024-08-07 18:33:25 +00:00
BatchInvokesProgressBar -stage 3
2024-07-31 00:26:27 +00:00
$output + = $invokeJob | Receive-Job -Wait -AutoRemoveJob | Select-Object * -ExcludeProperty RunspaceId , PSComputerName , PSShowComputerName , PSSourceJobInstanceId
2024-06-11 18:27:55 +00:00
$itemIndex = 0
$contactedhosts = $output . Hostname
2024-08-07 18:33:25 +00:00
BatchInvokesProgressBar -stage 4
2024-06-11 18:27:55 +00:00
$missedhosts = $OnlineComputers | Where-Object -FilterScript { $_ -notin $contactedhosts }
2024-07-31 00:26:27 +00:00
$missedJobs = @ ( )
2024-07-24 23:04:24 +00:00
if ( $missedhosts . count -gt 0 ) {
2024-09-23 21:39:12 +00:00
$i = 1
2024-07-26 18:41:04 +00:00
foreach ( $computer in $missedhosts ) {
2024-09-23 21:39:12 +00:00
$missedJobs + = Start-Job -ScriptBlock $ { function : Get-SCCMQueryBlock } -ArgumentList $computer , $i
$i + +
2024-06-11 18:27:55 +00:00
}
}
2024-08-13 23:40:16 +00:00
$output + = $offlineJobs | Receive-Job -Wait -AutoRemoveJob | Select-Object * -ExcludeProperty RunspaceId , PSComputerName , PSShowComputerName , PSSourceJobInstanceId
2024-07-31 00:26:27 +00:00
$output + = $missedJobs | Receive-Job -Wait -AutoRemoveJob | Select-Object * -ExcludeProperty RunspaceId , PSComputerName , PSShowComputerName , PSSourceJobInstanceId
2024-08-13 23:40:16 +00:00
BatchInvokesProgressBar -stage 5
#CMDB cycle of missed hosts
$missedhosts = $Computers | Where-Object -FilterScript { $_ -notin $output . Hostname }
if ( $missedhosts . count -gt 0 ) {
foreach ( $computer in $missedhosts ) {
$output + = Get-CMDBFallback $computer
}
}
2024-06-11 18:27:55 +00:00
return $output
}
function BatchInvokesProgressBar {
param (
$item ,
$currentItemIndex ,
$TotalItems ,
$stage
)
switch ( $stage ) {
2024-09-23 21:39:12 +00:00
1 { Write-Progress -Activity " Connecting to computers " -Status " $item ( $currentItemIndex / $TotalItems ) " -PercentComplete ( ( $currentItemIndex / $TotalItems ) * 100 ) -Id 0 }
2 { Write-Progress -Activity " Spinning up jobs " -PercentComplete ( ( 10 / 100 ) * 100 ) -Id 0 }
3 { Write-Progress -Activity " Querying online computers " -PercentComplete ( ( 20 / 100 ) * 100 ) -Id 0 }
4 { Write-Progress -Activity " Querying SCCM for offline computers " -PercentComplete ( ( 75 / 100 ) * 100 ) -Id 0 }
5 { Write-Progress -Activity " Querying CMDB for computers not in SCCM " -PercentComplete ( ( 85 / 100 ) * 100 ) -Id 0 }
2024-06-11 18:27:55 +00:00
Default { }
}
}