Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Last active May 8, 2024 04:45
Show Gist options
  • 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)
}
@ninmonkey
Copy link

Tip: [void] Isn't required for the add functions: List.Add/AddRange

List.Remove is one that returns a value.
I didn't see an overload that both: returns nothing and accepts type Object

@JustinGrote
Copy link
Author

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 :))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment