首页 > 解决方案 > powershell WebClient 为 zip 文件解析链接或正则表达式

问题描述

我有一个几乎可以工作的脚本,但我需要在 URL 上添加解析或通配符,因为 URL 将更改为我每个月都不知道的字符。我必须使用 New-Object System.Net.WebClient 因为调用 Web 请求被阻止。所以,我在想是否有人知道如何使用我知道的字符下载链接并剥离链接的其余部分。

以下链接示例

Full-CSV-data-file-Jan19-ZIP-3633K-53821.zip
Full-CSV-data-file-Dec18-ZIP-3427K.zip
Full-CSV-data-file-Nov18-ZIP-3543K-21860.zip

所以在上面的链接中,我知道最新的文件将包含 Jan19,它是一个 zip 文件。我正在使用的脚本是

$currentMonthNo = get-date -format "MM"
$currentMonthName = (get-date((get-date).addmonths(-2)) -format MMM)
$currentYearNo = get-date –format yy
$url = "http://www.website.com/Full-CSV-data-file-$currentMonthName$currentYearNo-ZIP-3633K-53821.zip"
$output = "C:\Folder\Full-CSV-data-file-Jan19-ZIP-3633K-53821.zip"
$start_time = Get-Date
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)
#OR
(New-Object System.Net.WebClient).DownloadFile($url, $output)

Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
Write-Output $url
Start-Sleep -s 6

标签: regexpowershellparsing

解决方案


推荐阅读