How to specify which file I want to download?

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of Vandrey Trindade Vandrey Trindade 1 month, 3 weeks ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #48922
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    Hi,

    I've been trying to create a script to download a SuperDAT file from McAfee but the file name changes time to time.
    Is there a way to do a "get-childitem" like command on http to get only the newest file?

    Source of the files:
    http://download.nai.com/products/licensed/superdat/nai/Brazilian/

    Script created to download, specifying which file I want (won't work when the file name changes):

    Import-Module BitsTransfer
    
    $Source = "http://download.nai.com/products/licensed/superdat/nai/Brazilian/8244xdat.exe"
    $Destination = "D:\Temp"
    
    $Job = Start-BitsTransfer -Source $Source -Destination $Destination -Asynchronous
    
    While( ($Job.JobState.ToString() -eq 'Transferring') -or ($Job.JobState.ToString() -eq 'Connecting') )
    {
       $pct = [int](($Job.BytesTransferred*100) / $Job.BytesTotal)
       Write-Progress -Activity "Copying file..." -CurrentOperation "$pct% complete"
    }
    Complete-BitsTransfer -BitsJob $Job
    #48928
    Profile photo of Don Jones
    Don Jones
    Keymaster

    No, unfortunately not. Web servers don't allow you to enumerate what's there that way, mainly for security reasons.

    When you go to http://download.nai.com/products/licensed/superdat/nai/Brazilian/, what you're getting is an actual HTML file that the server constructs. If you're doing this from a client computer, you could use Invoke-WebRequest to get that HTML file. The response object will parse out hyperlinks for you, meaning you'll have a set of objects that represent each hyperlink. You could then use that as a kind of "file list," and figure out which one you want from there. That won't work on a server, though; the parsing functionality depends on some IE bits that are client-side. So if you need this to run on a server, you'd have to request that HTML file via Invoke-WebRequest, and then probably use a regular expression to parse out the available hyperlinks yourself.

    #48930
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    Thanks for the class and attention 😀

    #48932
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Hi Vandrey,

    I've quickly modified your snippet to parse out and download the latest definition file. I hope it works for you.

    Cheers
    Daniel

    #48969
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    Hi Daniel,

    Thanks!
    I made almost the same thing using this:

    Import-Module BitsTransfer
    
    $Site = "http://download.nai.com/products/licensed/superdat/nai/Brazilian/"
    $HTML = Invoke-WebRequest $Site
    $File = $HTML.Links | Foreach {$_.href } | Where {$_ -like "*.exe"} | Select -Last 1
    
    $Source = "http://download.nai.com/products/licensed/superdat/nai/Brazilian/$File"
    $Destination = "D:\Temp"
    
    $Job = Start-BitsTransfer -Source $Source -Destination $Destination -Asynchronous
    
    While( ($Job.JobState.ToString() -eq 'Transferring') -or ($Job.JobState.ToString() -eq 'Connecting') )
    {
       $pct = [int](($Job.BytesTransferred*100) / $Job.BytesTotal)
       Write-Progress -Activity "Copying file..." -CurrentOperation "$pct% complete"
    }
    Complete-BitsTransfer -BitsJob $Job
    Write-Progress -Activity "Copying file..."
    #49006
    Profile photo of Dan Potter
    Dan Potter
    Participant

    careful with the foreach where it is not necessary.

    $web = Invoke-WebRequest -Uri 'http://download.nai.com/products/licensed/superdat/nai/brazilian/'
    $web.links.href -match 'exe' |select -Last 1

    #49055
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    Dan Potter,

    Thanks for the warning.
    Changed the code to use your lines =]

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.