Alex
Alex

Reputation: 151

Download a file form dynamic URL

I'm facing a problem when trying to download a file with a PowerShell script:

The issue is that the file's name changes dynamically, so I'm not sure about how to define the URL path.

For example, I'd like to download a file from "http://definitions.symantec.com/defs/jdb/FILE_NAME", but the file's name (e.g., "2wer123.jdb") changes every day.

Following, you can see the script I'm currently working on. However, I don't know how to implement a solution for tackling the dynamic name.

$source = "http://definitions.symantec.com/defs/jdb/*.jdb";
$dest = "F:\Program Files (x86)\xXXXXXx\xXXXXXx\xXXXx\xXXx\content\incoming\virus_def.jdb";
$secPasswd = ConvertTo-SecureString "adfasfdasfl" -AsPlainText -Force
$myCreds = New-Object System.Management.Automation.PSCredential -ArgumentList "sadfas.sdafasf.sfdasdfaf.sfs.sdfs\Administrator", $secPasswd
$Test = Invoke-WebRequest -Uri $source -OutFile $dest -Proxy 'http:\\sdfs.sdfs.sadfsa:8080' -ProxyCredential $mycreds

Upvotes: 0

Views: 1780

Answers (2)

Matt
Matt

Reputation: 46730

In order to get that link you need to parse the current definition link from this page like Andrey Marchuk suggests

$path = "https://www.symantec.com/security_response/definitions/download/detail.jsp?gid=sep"
$definitionPath = (Invoke-WebRequest $path).Links |
    Where-Object{$_.InnerText -like "*.jdb" -and $_.InnerText -notlike "*core*"} | 
    Select-Object -ExpandProperty href

What this does it take all the hyperlinks on the page and return the one(??) that has a link that ends in "jdb" but does not contain "core" (both are listed). Now $definitionPath contains the path you are looking for and can now download.

I believe that you would need PowerShell 3.0 for this to work.

Upvotes: 1

Andrey Marchuk
Andrey Marchuk

Reputation: 13483

Your script doesn't work because you can't use wildcard over HTTP. What you can do is to find a page that has the link to the latest file, like: https://www.symantec.com/security_response/definitions/download/detail.jsp?gid=sep

From there on I can think of 2 ways of getting the filename:

  1. Download the page and parse is to get the link
  2. On that page, there's FTP link to the same file. You could download your file over FTP, or at least list the files and get the file name.

Upvotes: 1

Related Questions