简体   繁体   English

PowerShell 从具有最新日期的列表中下载文件

[英]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.我是使用 PowerShell 和脚本的新手,但我尝试解决我的问题几天了,即使在研究 stackoverflow 和 Web 时我也找不到解决方案。

I try to write a script to Download a fixed amount of files (.jdb, .exe) from a Website.我尝试编写一个脚本来从网站下载固定数量的文件(.jdb、.exe)。 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文件名的一部分始终相同,例如:-061-IPS_IU_SEP_14RU1.jdb 但完整的文件名是 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.有些来自 1 天前,有些则是 3 天或更多天。 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.但现在我需要使用$filename数组过滤最新的 href。

Acutally I'm stuck. Acutally我被困住了。 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])"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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