[英]Powershell wait until after file download to continue script
$x = Invoke-RestMethod -Method "Get" -uri $url -Headers $get_headers
**Start-Process "chrome.exe" $x.url**
Start-Sleep -s 10
Hello,你好,
I am using the above to download a file via chrome.我正在使用上面的内容通过 chrome 下载文件。 Is it possible to wait until the file download is complete to continue the script instead of sleeping or implementing an incremental retry function.
是否可以等到文件下载完成后继续执行脚本,而不是休眠或实现增量重试 function。
Thank you谢谢
So lets talk about what i see and ways to make it better那么让我们谈谈我所看到的以及如何让它变得更好
$x = Invoke-RestMethod -Method "Get" -uri $url -Headers $get_headers
Start-Process "chrome.exe" $x.url
Start-Sleep -s 10
Well you can save using the Invoke-RestMethod那么你可以使用Invoke-RestMethod保存
Here is a example这是一个例子
Invoke-RestMethod "http://www.peoplelikeus.org/piccies/codpaste/codpaste-teachingpack.pdf" -OutFile "C:\codpaste-teachingpack.pdf"
In your case this might work.在您的情况下,这可能有效。
$x = Invoke-RestMethod $url -Headers $get_headers
Invoke-RestMethod $x.url -OutFile "C:\SomeTypeOfFile.Txt"
The Invoke-RestMethod will wait until it is complete to move on in the code. Invoke-RestMethod将等到它完成后才能在代码中继续。
FYI (putting this here as it is too long for a normal comment), here are 3 other ways , that you could have done this:仅供参考(把它放在这里是因为它对于普通评论来说太长了),还有其他 3 种方法,你可以这样做:
$url = "http://mirror.internode.on.net/pub/test/10meg.test"
$output = "$PSScriptRoot\10meg.test"
$start_time = Get-Date
# 1. Invoke-WebRequest
Invoke-WebRequest -Uri $url -OutFile $output
"Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
# 2. System.Net.WebClient
(New-Object System.Net.WebClient).DownloadFile($url, $output)
"Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
# 3. Start-BitsTransfer
Start-BitsTransfer -Source $url -Destination $output -Asynchronous
"Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.