none
How to code this powershell? RRS feed

  • Question

  • Referring to following coding, I would like to know on how to add a loop to check on whether download is successful or not, if not, repeat to download with maximum 5 trials.

    $clnt = new-object System.Net.WebClient $url = "https://go.microsoft.com/fwlink/?LinkId=212732" $file = "D:\Folder\msert.exe" $clnt.DownloadFile($url,$file)


    Does anyone have any suggestions?
    Thanks in advance for any suggestions


    Thanks in advance for any suggestions

    Sunday, June 16, 2019 7:44 AM

All replies

  • I've found a site that might help you.

    3 ways to download files with PowerShell.  ... I'd recommend the last way descibed. ;-)

    If yo like to go forward with your code you'd need a loop like foreach, a counter and a check for a condition like if.

    BTW: It would be nice if you could choose a better subject for your questions next time. A subject should provide a hint what the question is about ...  about 95 % of the questions are about Powershell here ... ;-)


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Sunday, June 16, 2019 12:36 PM
    Sunday, June 16, 2019 11:22 AM
  • My script is already working, but need a loop to repeat the download until successful.

    Do you have any suggestions on how to create a loop for checking?
    Thank you very much for any suggestions (^v^)


    Thanks in advance for any suggestions

    Sunday, June 16, 2019 1:05 PM
  • Are you getting a download error? 

    The DownLoadFile method will not return until the download is complete. If you need to initiate the download and do something else while that is running you can use the aysnc method.

    $clnt = new-object System.Net.WebClient
    $url = "https://go.microsoft.com/fwlink/?LinkId=212732"
    $file = "c:\temp\msert.exe"
    $clnt.DownloadFileAsync($url,$file)
    while($clnt.IsBusy){ 
        "Waiting..."
        Start-Sleep 5
    }
    "Download complete."

     
    Sunday, June 16, 2019 2:24 PM
  • Sometime, server is down, so not able to download file, I would like to use loop as shown below

    Do

        Coding ...

    Loop Until if download file size > 3kb or try more than 5 times

    Do you have any suggestions?
    Thank you very much for any suggestions (^v^)


    Thanks in advance for any suggestions

    Sunday, June 16, 2019 2:31 PM
  • DO not use async here.  You can use Try/Catch I a loop.

    $clnt = new-object System.Net.WebClient
    $url = "https://go.microsoft.com/fwlink/?LinkId=212732"
    $file = "D:\Folder\msert.exe"
    
    for($i = 1;$i -le 5;$i++){
        Try{
            $clnt.DownloadFile($url, $file)
        }
        Catch{
            Write-Host $_
            Sleep 5
        }
    }

    On any failure the code will throw an exception and report it then try again.

    To use PowerShell you must learn to use loop and handle exceptions.


    \_(ツ)_/



    • Edited by jrv Sunday, June 16, 2019 2:47 PM
    Sunday, June 16, 2019 2:45 PM
  • If the remote server is up, it may not necessarily crash. https://go.microsoft.com/fwlink/?LinkId=999999 successfully downloads but msert.exe then contains HTML data. Depending on how your server processes the request you may still need to examine the contents or file size in the Try block after the .DownLoadFile() returns.
    Sunday, June 16, 2019 3:42 PM
  • If the remote server is up, it may not necessarily crash. https://go.microsoft.com/fwlink/?LinkId=999999 successfully downloads but msert.exe then contains HTML data. Depending on how your server processes the request you may still need to examine the contents or file size in the Try block after the .DownLoadFile() returns.

    Yes.  You also need to check the results which will contain the web page error.  The method for this is the same but we can use DownloadData.  The bigger issue comes when downloading through a link.

    An easy way to is to just check the file type.  If it is binary then it is the EXE.  If a string then it was the error page.

    Another way is to just open the link page and check for errors.  You can also download the file and check its size.

    There is not clean way to do this through a link.  The OP said the issue was that the server was not available.  That will always cause an exception to be thrown. 


    \_(ツ)_/

    Sunday, June 16, 2019 4:03 PM
  • I would like to know on how to add following checking conditions for handling error

    if download file size > 3kb then PASS else ERROR and retry until reaching 5 times

    Do you have any suggestions?
    Thanks, to everyone very much for any suggestions (^v^)


    Thanks in advance for any suggestions

    Sunday, June 16, 2019 4:23 PM
  • (Get-Item $file).Length -gt 3Kb

    Just test the file.

    Please also understand that you will need to learn PowerShell to understand what to do.  We cannot write a custom script for you on line at a time.  That and learning PowerShell are your responsibility.


    \_(ツ)_/


    • Edited by jrv Sunday, June 16, 2019 4:28 PM
    Sunday, June 16, 2019 4:26 PM
  • I would like to know on where to add this condition into "for loop" and "Try Catch"

    (Get-Item $file).Length -gt 3Kb

    Do you have any suggestions?
    Thanks, to everyone very much for any suggestions (^v^)

    for($i = 1;$i -le 5;$i++){
        Try{
            $clnt.DownloadFile($url, $file)
        }
        Catch{
            Write-Host $_
            Sleep 5
        }

    Thanks in advance for any suggestions

    Sunday, June 16, 2019 4:37 PM
  • You copied the code wrong.   Where would you check the file?  After it is download.

    As above - we will not rite this for you one line at a time.  You must learn PowerShell basics.  Nearly every question you have asked depends on learning basic PowerShell.  If you need someone to do this for you then please contact a consultant.

          Microsoft Virtual     Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/

    Sunday, June 16, 2019 5:10 PM
  • My script is already working, ...

    That doesn't necessarily mean that it's the optimum you can get. ;-) Have you at least read the information on the site I recommended? If I got you right Start-BitsTransfer has everything you want already built in. So don't need to build something yourself.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH-666 Sunday, June 16, 2019 7:31 PM
    Sunday, June 16, 2019 7:29 PM
  • Yes - BITS has all of the retry features and will grab file.  The issue with BITS is that it cannot resolve reference given as a link ID.  You can only use direct URI references to a file.


    \_(ツ)_/

    Sunday, June 16, 2019 7:38 PM
  • If you derefence the link and use the actual URI of the file then BITS works best:

    http://definitionupdates.microsoft.com/download/definitionupdates/safetyscanner/amd64/msert.exe

    This URI will retry on an interval until the file if successful or until you remove the transfer request.

    Try the following to see how it works.

    $url = 'http://definitionupdates.microsoft.com/download/definitionupdates/safetyscanner/amd64/msert.exe'
    $file = 'D:\Folder\msert.exe'
    Start-BitsTransfer -Destination $file -Source $url -RetryInterval 60


    \_(ツ)_/


    • Edited by jrv Sunday, June 16, 2019 7:49 PM
    Sunday, June 16, 2019 7:48 PM
  • Please see following error as shown below.  I would like to know on what wrong it is on checking file size.

    $count = 1
    DO
    {
     $clnt.DownloadFile($url, $file)
     Write-Host $count
     Sleep 5
     $count++
    } Until (Get-Item $file).Length -gt 3Kb or $count -gt 5

    Do you have any suggestions?
    Thanks, to everyone very much for any suggestions (^v^)



    Thanks in advance for any suggestions

    Sunday, June 16, 2019 10:31 PM
  • Please take the time to learn PowerShell and stop guessing.  You can use the help to learn how to use loops and the tutorial will teach you the syntax.

    The code I posted works correctly.  Why would you change it for something that you do not understand at all?


    \_(ツ)_/

    Sunday, June 16, 2019 10:36 PM
  •  help about_do

    \_(ツ)_/

    Sunday, June 16, 2019 10:37 PM