BatchInvokes now uses jobs for a 4x speedup!

This commit is contained in:
Zachary Gorman 2024-07-24 16:04:24 -07:00
parent e17f7e6ba3
commit f432187f92

View file

@ -11,7 +11,6 @@ function Get-PCBatchInvoke {
try {$win32_networkadapterconfiguration = Get-CimInstance -Class win32_networkadapterconfiguration} catch {$win32_networkadapterconfiguration = $null} #| MAC Address, try {$win32_networkadapterconfiguration = Get-CimInstance -Class win32_networkadapterconfiguration} catch {$win32_networkadapterconfiguration = $null} #| MAC Address,
try {$win32_LogicalDisk = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" } catch {$win32_LogicalDisk = $null} #| Diskspace, try {$win32_LogicalDisk = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" } catch {$win32_LogicalDisk = $null} #| Diskspace,
try {$win32_SystemEnclosure = Get-CimInstance -ClassName Win32_SystemEnclosure} catch {$win32_SystemEnclosure = $null} #| Asset Tag try {$win32_SystemEnclosure = Get-CimInstance -ClassName Win32_SystemEnclosure} catch {$win32_SystemEnclosure = $null} #| Asset Tag
try {$memoryAvailable = (Get-Counter '\Memory\Available Bytes').CounterSamples.CookedValue} catch {$memoryAvailable = $null}
Write-Progress -Activity "Retrieving data from online computers" -Status $Env:COMPUTERNAME -PercentComplete 20 Write-Progress -Activity "Retrieving data from online computers" -Status $Env:COMPUTERNAME -PercentComplete 20
try {$bitlocker = manage-bde -status C:} catch {$bitlocker = $null} # | Bitlocker Status try {$bitlocker = manage-bde -status C:} catch {$bitlocker = $null} # | Bitlocker Status
try {$PCInfo = get-computerinfo} catch {$PCInfo = $null} try {$PCInfo = get-computerinfo} catch {$PCInfo = $null}
@ -352,32 +351,28 @@ function Get-PCBatchInvoke {
} }
} }
BatchInvokesProgressBar -stage 2 BatchInvokesProgressBar -stage 2
$output += Invoke-Command -ScriptBlock $sblock -ComputerName $OnlineComputers -SessionOption (New-PSSessionOption -NoMachineProfile -OpenTimeout 45000) | Select-Object * -ExcludeProperty RunspaceId, PSComputerName, PSShowComputerName $jobs = Invoke-Command -ScriptBlock $sblock -ComputerName $OnlineComputers -SessionOption (New-PSSessionOption -NoMachineProfile -OpenTimeout 45000) -AsJob
$itemIndex = 0 if($OfflineComputers.count -gt 0){
$contactedhosts = $output.Hostname foreach($computer in $OfflineComputers){
$missedhosts = $OnlineComputers | Where-Object -FilterScript {$_ -notin $contactedhosts} $jobs += Start-Job -ScriptBlock {
$OfflineComputers += $missedhosts $offlineCompData = Get-SCCMQuery -comp $computer -NumberofComputers $OfflineComputers.count -PCID $itemIndex
foreach($computer in $output){ if($null -eq $offlineCompData.'Last Reboot'){
$itemIndex++ $offlineCompData = Get-CMDBFallback -comp $computer
BatchInvokesProgressBar -item $computer.Hostname -currentItemIndex $itemIndex -TotalItems $output.Count -stage 3 }
$cmdbData = Get-CMDBData -comp $computer.Hostname -asset $computer.'Asset Tag' Write-Output $offlineCompData
$computer.'CMDB Location' = Get-CMDBLocation -cmdb $cmdbData }
if($cmdbData.values.ConfigurationItem._SHSDELAsset -eq 'True'){
$delInfo = Get-CMDBDELInfo $cmdbData
$computer | Add-Member -MemberType NoteProperty -Name 'DEL Owner' -Value $delInfo.Owner
$computer | Add-Member -MemberType NoteProperty -Name 'DEL Vendor PC' -Value $delInfo.Vendor
$computer | Add-Member -MemberType NoteProperty -Name 'DEL Description' -Value $delInfo.Description
} }
} }
if($OfflineComputers.count -gt 0){ BatchInvokesProgressBar -stage 3
BatchInvokesProgressBar -stage 4 $output += $jobs | Receive-Job -Wait | Select-Object * -ExcludeProperty RunspaceId, PSComputerName, PSShowComputerName
$itemIndex = 0 $itemIndex = 0
$contactedhosts = $output.Hostname
BatchInvokesProgressBar -stage 4
$missedhosts = $OnlineComputers | Where-Object -FilterScript {$_ -notin $contactedhosts}
if($missedhosts.count -gt 0){
foreach($computer in $OfflineComputers){ foreach($computer in $OfflineComputers){
$itemIndex++
$offlineCompData = Get-SCCMQuery -comp $computer -NumberofComputers $OfflineComputers.count -PCID $itemIndex $offlineCompData = Get-SCCMQuery -comp $computer -NumberofComputers $OfflineComputers.count -PCID $itemIndex
if($null -eq $offlineCompData.'Last Reboot'){ if($null -eq $offlineCompData.'Last Reboot'){
$offlineCompData = Get-CMDBFallback -comp $computer $offlineCompData = Get-CMDBFallback -comp $computer
} }
$output += $offlineCompData $output += $offlineCompData
@ -423,9 +418,9 @@ function BatchInvokesProgressBar {
) )
switch ($stage) { switch ($stage) {
1 { Write-Progress -Activity "Connecting to computers" -Status "$item ($currentItemIndex/$TotalItems)" -PercentComplete (($currentItemIndex/$TotalItems) * 100)} 1 { Write-Progress -Activity "Connecting to computers" -Status "$item ($currentItemIndex/$TotalItems)" -PercentComplete (($currentItemIndex/$TotalItems) * 100)}
2 { Write-Progress -Activity "Retrieving data from online computers" -PercentComplete ((50/100) * 100)} 2 { Write-Progress -Activity "Spinning up jobs" -PercentComplete ((50/100) * 100)}
3 { Write-Progress -Activity "Grabbing CMDB Data" -Status "$item ($currentItemIndex/$TotalItems)" -PercentComplete (($currentItemIndex/$TotalItems) * 100)} 3 { Write-Progress -Activity "Awaiting Jobs" -PercentComplete ((55/100)*100)}
4 { Write-Progress -Activity "Querying SCCM for offline computers" -PercentComplete ((75/100) * 100)} 4 { Write-Progress -Activity "Querying SCCM for missed computers" -PercentComplete ((75/100) * 100)}
Default {} Default {}
} }