使用 PowerShell 将击键发送到特定 window

Send keystrokes to a specific window using PowerShell

有没有办法使用 PowerShell 将击键发送到特定 window?
现在我正在打开一个 IE window.
然后,通过调用 link,下载 window 正在打开。
我正在发送 ALT+TAB 以切换下载 window,并发送 LEFT+ENTER 以确认下载。
有没有办法将此 LEFT+ENTER 发送到特定的 window 而无需关注它?
我的代码看起来像这样:

$ie=new-object -com internetexplorer.application
$ie.navigate2($url)
$ie.visible=$true
while($ie.busy) {Start-Sleep 1}
start-sleep -seconds 1
[System.Windows.Forms.SendKeys]::SendWait("%{TAB}")
start-sleep -seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{LEFT}")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")

我想将击键发送到特定 window 以确保下载 window 获得击键,即使焦点不在它上面。
我知道发送击键不是下载文件的最佳方式,但我也试过这个:

$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.Networkcredential($user, $password)
$webclient.DownloadFile($urlWeb,$path)

我总是收到 403 错误并且还没有找到解决这个问题的方法...

我不是 SendKeys 的忠实粉丝,因为它几乎总是失败。如果我使用提供的 link,我可以使用此 Powershell 代码下载文件:

$userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1"
$target = "http://download.thinkbroadband.com/5MB.zip"
$folder = "C:\TEMPMB.zip"

$web = New-Object System.Net.WebClient
$web.Headers.Add("user-agent", $userAgent)
$web.DownloadFile($target, $folder)

您可能还需要添加网络凭据。