Last active
August 19, 2024 19:17
-
-
Save JustinGrote/2f129822efec0ca15afca726c6382ab6 to your computer and use it in GitHub Desktop.
Get All Packages from the PowerShell Gallery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace System.Management.Automation | |
using namespace System.Collections.Generic | |
[List[Job2]]$jobs = @() | |
[List[Object]]$Result = @() | |
$baseUri = 'https://www.powershellgallery.com/api/v2/Packages?$skip=' | |
$maxPackages = 309000 | |
for ($i = 0; $i -lt $maxPackages; $i += 100) { | |
$job = Start-ThreadJob { | |
Invoke-RestMethod -Uri ($args[0] + $args[1]) | |
} -ArgumentList $baseUri, $i | |
[void]$jobs.Add($job) | |
if ($jobs.Count -gt 30) { | |
$jobs | Wait-Job -Any | Out-Null | |
} | |
foreach ($completedJob in ($jobs | Where-Object State -Match 'Completed|Failed')) { | |
$completedJob | Receive-Job -Wait -AutoRemoveJob | ForEach-Object { | |
[void]$Result.Add($_) | |
} | |
Write-Verbose "Job: $i Result: $($Result.Count)" | |
[void]$jobs.Remove($completedJob) | |
} | |
} | |
Write-Verbose 'Waiting for final Job Completion for 10 seconds' | |
$jobs | Wait-Job -Timeout 30 | Out-Null | |
foreach ($completedJob in ($jobs | Where-Object State -Match 'Completed|Failed')) { | |
$completedJob | Receive-Job -Wait -AutoRemoveJob | ForEach-Object { | |
[void]$Result.Add($_) | |
} | |
Write-Verbose "Final Job: $i Final Result: $($Result.Count)" | |
[void]$jobs.Remove($completedJob) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's probably a holdover from using Arraylist, hashset, or another type temporarily. It doesn't hurt anything to have it there regardless (other than a little confusion :))