Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Last active December 12, 2023 17:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinGrote/2f129822efec0ca15afca726c6382ab6 to your computer and use it in GitHub Desktop.
Save JustinGrote/2f129822efec0ca15afca726c6382ab6 to your computer and use it in GitHub Desktop.
Get All Packages from the PowerShell Gallery
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