简体   繁体   中英

PowerShell Downloading Files from a List with the latest Date

I'm new to using PowerShell and Scripting Overall but i tried to solve my Problem for a few Days now and even with researching stackoverflow and the Web i can't find a solution.

I try to write a script to Download a fixed amount of files (.jdb, .exe) from a Website. One Part of the filename is Always the same ex: -061-IPS_IU_SEP_14RU1.jdb But the full filename is 20201120-061-IPS_IU_SEP_14RU1.jdb

The first part is the date where the files have been created. So far i was able to download all the files using following Code:

$filename = @(
"-061-IPS_IU_SEP_14_0.exe",
"-061-IPS_IU_SEP_14_0.jdb",
"-061-IPS_IU_SEP_14_0_MP2.exe",
"-061-IPS_IU_SEP_14_0_MP2.jdb",
"-061-IPS_IU_SEP_14RU1.exe",
"-061-IPS_IU_SEP_14RU1.jdb",
"-061-IPS_IU_SEP_14.2_RU1.exe",
"-061-IPS_IU_SEP_14.2_RU1.jdb",
"-061-IPS_IU_SEP_14.2_RU2.exe",
"-061-IPS_IU_SEP_14.2_RU2.jdb"
)
# Zielverzeichnis
$output = "C:\IPS14\" 
$url = "http://definitions.symantec.com/defs/ips/"
$Date = Get-Date -format yyyyMMdd ((Get-Date).AddDays(0))

$fullurl = ("$url"  + $Date +  $filename[0])
    
        

for ($i=0; $i -lt $filename.Length; $i++){
    $fullurl = ("$url"  + ($Date-1) +  $filename[$i])
    Try{
   
    Start-BitsTransfer -Source $fullurl -Destination $Output
    Write-Host ("$url"  + $Date +  $filename[$i] + " Downloading")
}
Catch{}

The current Problem is that some Files are not updated daily. Some are from 1 Day ago others are 3 or more Days old. I only need the latest updated files.

Well because Downloading the files weren't my Problem i tried something like

$IPSindex = 'https://definitions.symantec.com/defs/download/symantec_enterprise/ips/index.html'
(Invoke-WebRequest –Uri $IPSindex).Links   | Sort-Object href -Unique | Format-List innerText, href

to list all files on the Page. But now i need to filter the latest href using the $filename Array.

Acutally I'm stuck. Hope you can help me.

Greetings

I have modified your snippet to extract the date part of the filename and converting it into a datetime array.Sorting the datetime array to fetch recent update.

$IPSindex = 'https://definitions.symantec.com/defs/download/symantec_enterprise/ips/index.html'
$links = ((Invoke-WebRequest –Uri $IPSindex).Links).href
$dates =@()
foreach ($link in $links){
if($link -like "*IPS*"){
$dates += $link.split("/")[-1].split("-")[0]
}
}
$dates = $dates |Get-Unique | foreach {[datetime]::ParseExact($_,"yyyyMMdd",$null)} | Sort-Object -Descending

Write-Output "The latest available definition date: $($dates[0])"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM