How to specify which file I want to download?

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

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.

Thanks for the class and attention :smiley:

Hi Vandrey,

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

https://gist.github.com/Dan1el42/57855af4fe33485e63672a3c41644ad0

Cheers
Daniel

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..."

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

Dan Potter,

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