作为计划作业的一部分,我需要从许多服务器并行下载一些 web 内容,但我找不到并行/异步运行下载的正确方法。如何才能做到这一点?
没有任何并行性,我可以这样做,但速度很慢:
$web = [System.Net.WebClient]::new()
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# $srvList is a list of servers of viariable length
$allData = ""
foreach ($srv in $srvList) {
$url = "https:\\$srv\MyWebPage"
$data = $web.DownloadString($url)
$allData += $data
}
但是如何通过“$web.DownloadStringAsync”并行执行此操作?我找到了这个片段,但我没有看到如何获取每个调用的结果以及如何连接它:
$job = Register-ObjectEvent -InputObject $web -EventName DownloadStringCompleted -Action {
Write-Host 'Download completed'
write-host $EventArgs.Result
}
$web.DownloadString($url)
有人知道如何以一种简短而聪明的方式解决这个问题吗?