使用 powershell 在 IE 上保存文件绕过下载弹出窗口

using powershell Save file on IE bypassing Download Popup

我正在尝试使用 powershell 从网站下载 IE 版本 21H 上的文件。

当我使用 powershell 单击“下载”按钮时,它要求我下载弹出窗口 window,并提供以下信息。

您想打开或保存来自 www.XYZ.com 的 XYZ.log 下面是我使用的代码

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible=$true
$ie.Navigate("www.xyz.com/ab/sder23445sdfrty")  #please note this is random URL I provided
$link=$ie.Document.getElementsByTagName("Button") | where-object {$_.outerhtml -like "*download*"} 
$link.click()

您可以先激活 IE window 并使用 AppActivate 将其置于最前面,然后使用 SendKeys 发送击键 Ctrl+S 保存文件。

示例代码如下,您可以将url和元素选择器更改为您自己的:

[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible=$true
$ie.Navigate("https://www.example.com/download.html") #change it to your own url
while($ie.ReadyState -ne 4 -or $ie.Busy) {Start-Sleep -m 100}
$link=$ie.Document.getElementById("btnDowload") #change it to your own selector
$link.click()

Sleep 5
$ieProc = Get-Process | ? { $_.MainWindowHandle -eq $ie.HWND }
[Microsoft.VisualBasic.Interaction]::AppActivate($ieProc.Id)
[System.Windows.Forms.SendKeys]::Sendwait("%{s}");