我尝试使用 URL 下载 Adobe Acrobat 安装程序,但脚本始终下载几乎为空的 exe 文件(大小约为 4 KB),并且我的系统不断告诉我该文件已损坏且无法读取。
我想要正确下载安装程序,以便我可以运行它,并以这种方式执行 Adobe Acrobat 的完整安装。我尝试过不同的方法,例如 Invoke-WebRequest、使用curl、使用起始位传输,但没有任何效果。我已经验证了我使用的 URL 是合法的,因为我将其放入网络浏览器中,并且它下载了 Adobe Acrobat 安装程序。这是我当前正在使用的 PowerShell 脚本:
$adobeInstallerUrl = "https://get.adobe.com/reader/download? os=Windows+10&name=Reader+2024.003.20112+English+Windows%2864Bit%29&lang=en&nativeOs=Windows+10&accepted=cr&declined=mss&preInstalled=&site=landing"
$installerPath = "C:\Users\User\Downloads\Reader_en_install.exe"
function Download-AdobeInstaller {
Write-Host "Downloading Adobe Acrobat Installer from $adobeInstallerUrl ..."
try {
Invoke-WebRequest -Uri $adobeInstallerUrl -OutFile $installerPath
Write-Host "Download complete. Installer saved to $installerPath"
}
catch {
Write-Host "Failed to download Adobe Acrobat Installer. Error: $_"
exit 1
}
}
function Install-AdobeAcrobat {
Write-Host "Running Adobe Acrobat installer..."
try {
Start-Process -FilePath $installerPath -ArgumentList "/sAll /rs /rps /msi EULA_ACCEPT=YES" -Wait
Write-Host "Adobe Acrobat installation complete."
}
catch {
Write-Host "Failed to install Adobe Acrobat. Error: $_"
exit 1
}
}
function Verify-FileSize {
$file = Get-Item $installerPath
if ($file.Length -lt 1000000) { # Assuming the file should be larger than 1MB
Write-Host "The downloaded file is too small and might be corrupt."
exit 1
}
}
Download-AdobeInstaller
Verify-FileSize
Install-AdobeAcrobat
mclayton在评论中提供了关键指示:
Invoke-WebRequest
下载 HTML 页面,而不是感兴趣的 二进制(可执行文件)。
在网络浏览器中打开此 URL,HTML 中嵌入的 JavaScript 会触发下载交互式。
使用
Invoke-WebRequest
/ Invoke-RestMethod
,你得到的只是 HTML 源代码,这对你没有帮助。
事实上
Invoke-WebRequest
和 Invoke-RestMethod
导致目标 URL 上的服务器做出响应 就像响应来自成熟的交互式 Web 浏览器 的请求一样,这是 有问题的 ,并且归因于 这些 cmdlet 发送的用户代理字符串(user-agent
标头字段)。
-UserAgent
字符串提供不同的用户代理字符串并使用 'curl'
来修改服务器的响应 - 正如广泛使用的 curl
实用程序所做的那样(默认情况下附加版本信息,例如 'curl/8.6.0'
) - 您将得到一个适合编程处理的响应。当使用
-UserAgent curl
作为用户代理字符串时,目标 URL 输出 JSON,Invoke-RestMethod
自动解析为 objects([pscustomobject]
实例),您可以从中提取实际的下载 URL访问相关属性:
downloadURL
属性(下面使用),以及 downloadURLA
、downloadURLB
和 fallbackDownloadURL
属性;我不清楚这些其他属性的用途,只是猜测 fallbackDownloadURL
可能会根据调用者的主机平台选择适当的二进制文件。# Get download info from the original URL, as an object parsed from JSON.
# Note the use of Invoke-*RestMethod*.
$downloadInfo =
Invoke-RestMethod -UserAgent curl 'https://rdc.adobe.io/reader/downloadUrl?name=Reader%202024.003.20112%20English%20Windows(64Bit)&nativeOs=Windows%2010&os=Windows%2010&site=landing&lang=en&accepted=cr&declined=mss&mcvisId=23676568118047080952195819047851974486&country=US&api_key=dc-get-adobereader-cdn'
# Download the binary (executable) based on the download info.
# Note: Invoke-RestMethod would work too.
Invoke-WebRequest -OutFile installer.exe $downloadInfo.downloadURL