简体   繁体   English

Powershell 等到文件下载后继续脚本

[英]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.

相关问题 强制等待/暂停,直到虚拟应用启动器实例关闭以继续Powershell脚本 - Force wait/pause until Virtual app launcher instance closes to continue Powershell script 调用powershell脚本后,批处理文件不会继续 - Batch file wont continue after call up a powershell script PowerShell 中是否有等待参数允许脚本等待命令完成 - is there a wait parameter in PowerShell to allow the script to wait until a command is completed 如何强制Powershell脚本等待重写文件,直到另一个进程完成读取它为止? - How can I force a Powershell script to wait overwriting a file until another process is finished reading it? Powershell - 退出应用程序后继续执行脚本 - Powershell - Continue a script after exiting an application 继续事件订阅,脚本执行后powershell - Continue event subscription,after script execution powershell 即使发生错误,Powershell脚本也不会继续 - Powershell script will not continue even after error Powershell脚本可在域中的所有计算机上运行-等待计算机联机 - Powershell script to run on all computers in domain - wait until computer is online 脚本内部的SQL错误失败后,是否停止Powershell脚本继续运行? - Stopping Powershell script to continue after an SQL error inside of the script fails? Powershell - 重启并继续脚本 - Powershell - Reboot and Continue Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM