How to specify which file I want to download?

Welcome Forums General PowerShell Q&A How to specify which file I want to download?

Viewing 4 reply threads
  • Author
    Posts
    • #48922
      Participant
      Topics: 51
      Replies: 122
      Points: 78
      Rank: Member

      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
      Keymaster
      Topics: 13
      Replies: 4872
      Points: 1,844
      Helping HandTeam Member
      Rank: Community Hero

      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
        Participant
        Topics: 51
        Replies: 122
        Points: 78
        Rank: Member

        Thanks for the class and attention 😀

    • #48932
      Moderator
      Topics: 2
      Replies: 525
      Points: 24
      Team Member
      Rank: Member

      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
        Participant
        Topics: 51
        Replies: 122
        Points: 78
        Rank: Member

        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
      Participant
      Topics: 18
      Replies: 573
      Points: 32
      Rank: Member

      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
      Participant
      Topics: 51
      Replies: 122
      Points: 78
      Rank: Member

      Dan Potter,

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

Viewing 4 reply threads
  • The topic ‘How to specify which file I want to download?’ is closed to new replies.