首页 > 解决方案 > PowerShell 在 html 站点下载最新文件

问题描述

我试图让 PowerShell 从http://download.affixa.com/MSI/versions.html下载最新的 msi 文件,但我永远找不到该文件。不过,我可以让它从不同的站点下载文件。当我尝试从该站点获取信息时,只有当我在他们的页面上看不到任何 Google 时才会显示 Google 信息。此外,如果我还没有文件(与脚本在同一目录中),我只想下载该文件,因为我什至无法让它工作,所以我还没有想出来。

$site = (Invoke-WebRequest -URI "http://download.affixa.com/MSI").Links | Where-Object {$_.href -like ‘*msi*’} | Select-Object href
Invoke-WebRequest -URI $site -Outfile "$PSScriptRoot\Affixa.msi"

标签: powershell

解决方案


正如李提到的,您需要使用浏览器/解析器,因为页面内容是在页面加载时加载的。

这对我有用(据我所知,IE 在 Windows 10 上仍然可用)。

# reset '$lastLink'
$lastLink = $null
while($null -eq $lastLink) {
    # set the url we want to parse
    $url = 'http://download.affixa.com/MSI/versions.html'
    # create an Internet Explorer object
    $ie = New-Object -com internetexplorer.application;
    # hide the Internet Explorer Application
    $ie.visible = $false;
    # navigate to the url specified
    $ie.navigate($url);
    # wait for the application beeing ready to use
    while($ie.Busy -eq $true) {
        Start-Sleep -Seconds 1
    }
    Start-Sleep -Seconds 2
    # select the last link of the page which matches the pattern
    $lastLink = $ie.Document.getElementsByTagName('a') | Select-Object -Last 1 | Select-Object -ExpandProperty href
    # close the Internet Explorer application
    $ie.quit()
}
# download the msi
Invoke-WebRequest -Uri $lastLink -OutFile "$PSScriptRoot\Affixa.msi"

推荐阅读