繁体   English   中英

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

[英]PowerShell Downloading Files from a List with the latest Date

我是使用 PowerShell 和脚本的新手,但我尝试解决我的问题几天了,即使在研究 stackoverflow 和 Web 时我也找不到解决方案。

我尝试编写一个脚本来从网站下载固定数量的文件(.jdb、.exe)。 文件名的一部分始终相同,例如:-061-IPS_IU_SEP_14RU1.jdb 但完整的文件名是 20201120-061-IPS_IU_SEP_14RU1.jdb

第一部分是创建文件的日期。 到目前为止,我能够使用以下代码下载所有文件:

$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{}

当前的问题是某些文件不是每天更新的。 有些来自 1 天前,有些则是 3 天或更多天。 我只需要最新更新的文件。

好吧,因为下载文件不是我的问题,所以我尝试了类似的方法

$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

列出页面上的所有文件。 但现在我需要使用$filename数组过滤最新的 href。

Acutally我被困住了。 希望您能够帮助我。

你好

我已修改您的代码段以提取文件名的日期部分并将其转换为日期时间数组。对日期时间数组进行排序以获取最近的更新。

$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