From f432187f9257e6b685ca5ae61e61f2007e313805 Mon Sep 17 00:00:00 2001 From: Zachary Gorman Date: Wed, 24 Jul 2024 16:04:24 -0700 Subject: [PATCH] BatchInvokes now uses jobs for a 4x speedup! --- Private/BatchInvokes.ps1 | 45 ++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/Private/BatchInvokes.ps1 b/Private/BatchInvokes.ps1 index adcdfa5..8656731 100644 --- a/Private/BatchInvokes.ps1 +++ b/Private/BatchInvokes.ps1 @@ -11,7 +11,6 @@ function Get-PCBatchInvoke { 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_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 try {$bitlocker = manage-bde -status C:} catch {$bitlocker = $null} # | Bitlocker Status try {$PCInfo = get-computerinfo} catch {$PCInfo = $null} @@ -352,32 +351,28 @@ function Get-PCBatchInvoke { } } BatchInvokesProgressBar -stage 2 - $output += Invoke-Command -ScriptBlock $sblock -ComputerName $OnlineComputers -SessionOption (New-PSSessionOption -NoMachineProfile -OpenTimeout 45000) | Select-Object * -ExcludeProperty RunspaceId, PSComputerName, PSShowComputerName - $itemIndex = 0 - $contactedhosts = $output.Hostname - $missedhosts = $OnlineComputers | Where-Object -FilterScript {$_ -notin $contactedhosts} - $OfflineComputers += $missedhosts - foreach($computer in $output){ - $itemIndex++ - BatchInvokesProgressBar -item $computer.Hostname -currentItemIndex $itemIndex -TotalItems $output.Count -stage 3 - $cmdbData = Get-CMDBData -comp $computer.Hostname -asset $computer.'Asset Tag' - $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 + $jobs = Invoke-Command -ScriptBlock $sblock -ComputerName $OnlineComputers -SessionOption (New-PSSessionOption -NoMachineProfile -OpenTimeout 45000) -AsJob + if($OfflineComputers.count -gt 0){ + foreach($computer in $OfflineComputers){ + $jobs += Start-Job -ScriptBlock { + $offlineCompData = Get-SCCMQuery -comp $computer -NumberofComputers $OfflineComputers.count -PCID $itemIndex + if($null -eq $offlineCompData.'Last Reboot'){ + $offlineCompData = Get-CMDBFallback -comp $computer + } + Write-Output $offlineCompData + } } } - if($OfflineComputers.count -gt 0){ - BatchInvokesProgressBar -stage 4 - $itemIndex = 0 + BatchInvokesProgressBar -stage 3 + $output += $jobs | Receive-Job -Wait | Select-Object * -ExcludeProperty RunspaceId, PSComputerName, PSShowComputerName + $itemIndex = 0 + $contactedhosts = $output.Hostname + BatchInvokesProgressBar -stage 4 + $missedhosts = $OnlineComputers | Where-Object -FilterScript {$_ -notin $contactedhosts} + if($missedhosts.count -gt 0){ foreach($computer in $OfflineComputers){ - $itemIndex++ $offlineCompData = Get-SCCMQuery -comp $computer -NumberofComputers $OfflineComputers.count -PCID $itemIndex - if($null -eq $offlineCompData.'Last Reboot'){ - $offlineCompData = Get-CMDBFallback -comp $computer } $output += $offlineCompData @@ -423,9 +418,9 @@ function BatchInvokesProgressBar { ) switch ($stage) { 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)} - 3 { Write-Progress -Activity "Grabbing CMDB Data" -Status "$item ($currentItemIndex/$TotalItems)" -PercentComplete (($currentItemIndex/$TotalItems) * 100)} - 4 { Write-Progress -Activity "Querying SCCM for offline computers" -PercentComplete ((75/100) * 100)} + 2 { Write-Progress -Activity "Spinning up jobs" -PercentComplete ((50/100) * 100)} + 3 { Write-Progress -Activity "Awaiting Jobs" -PercentComplete ((55/100)*100)} + 4 { Write-Progress -Activity "Querying SCCM for missed computers" -PercentComplete ((75/100) * 100)} Default {} }