0
$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

Alex
  • 5
  • 1
  • 2
  • 3
  • have you tried using Start-Process `"chrome.exe" $x.url -wait`; Also why not just do another `Invoke-Restmethod` and save the file? – ArcSet Feb 24 '20 at 15:44
  • unfortunately -wait did not work. Invoke-RestMethod -outfile seems to be working quite well. Thanks for the response. – Alex Feb 24 '20 at 20:36

2 Answers2

3

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.

ArcSet
  • 6,518
  • 1
  • 20
  • 34
  • Thank you for your help. Invoke-RestMethod $x.url -OutFile "C:\SomeTypeOfFile.Txt" is working as intended. – Alex Feb 24 '20 at 20:37
2

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)"
postanote
  • 15,138
  • 2
  • 14
  • 25