Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
$cs = @"
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
public class Test
private static HttpClient client = new HttpClient() { Timeout = TimeSpan.FromMinutes(5) };
public static async Task DownloadASync(string[] uriStrings, string directory)
await Task.WhenAll(uriStrings.Select(x => new Uri(x))
.Select(async x =>
var file = Path.Combine(directory, x.Segments.Last());
using (var res = await client.GetAsync(x, HttpCompletionOption.ResponseHeadersRead))
using (var fileStream = new FileStream(file, FileMode.OpenOrCreate))
var s = await res.Content.ReadAsStreamAsync();
await s.CopyToAsync(fileStream);
Add-Type -TypeDefinition $cs
$uris = (Invoke-WebRequest -Uri |
Where {$_.EndsWith(".pdf")} |
Sort-Object -Unique
[System.Threading.Tasks.Task]::WaitsAll([Test]::DownloadASync($uris, $pwd))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment