简体   繁体   中英

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. Is it possible to wait until the file download is complete to continue the script instead of sleeping or implementing an incremental retry 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

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.

FYI (putting this here as it is too long for a normal comment), here are 3 other ways , that you could have done this:

$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)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM