使用 Powershell 异步下载多个大文件

use*_*942 4 powershell

我需要下载四个大型操作系统安装介质。如果我等待每个下载完成然后再进行下一个下载,这将需要很长时间。在下载之前,我想检查媒体是否已经存在。

该解决方案可能是哈希表、测试路径和调用网络请求的组合,但我无法破解它。

所以在伪代码中:

Check if file1 exists
if true then download and check file2
if false check file 2
check if file 2 exists...
Run Code Online (Sandbox Code Playgroud)

因此,请检查文件是否存在,如果不存在,请开始下载所有丢失的文件。

我对PS不是很有经验,所以非常感谢您的帮助,非常感谢!研究答案很有趣,但我觉得我在这里缺少一个关键字......

Mik*_*Twc 7

使用 WebClient 类进行异步下载有一种相当简单的方法,尽管它可能在旧版本的 PS 上不可用。请参阅下面的示例

$files = @(
 @{url = "https://github.com/Microsoft/TypeScript/archive/master.zip"; path = "C:\temp\TS.master.zip"}
 @{url = "https://github.com/Microsoft/calculator/archive/master.zip"; path = "C:\temp\calc.master.zip"}
 @{url="https://github.com/Microsoft/vscode/archive/master.zip"; path = "C:\temp\Vscode.master.zip"}
)

$workers = foreach ($f in $files) { 

$wc = New-Object System.Net.WebClient

Write-Output $wc.DownloadFileTaskAsync($f.url, $f.path)

}

# wait until all files are downloaded
# $workers.Result

# or just check the status and then do something else
$workers | select IsCompleted, Status
Run Code Online (Sandbox Code Playgroud)