Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send keystrokes to a specific window using PowerShell

Is there a way to send keystrokes to a specific window using PowerShell ?
Right now I'm opening an IE window.
Then, by calling the link, a download window is opening.
I'm sending an ALT+TAB to switch in the download window, and LEFT+ENTER to confirm the download.
Is there a way to send this LEFT+ENTER to a specific window without having focus on it?
My code looks like that:

$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}")

I want to send the keystrokes to a specific window to make sure the download window gets the keystrokes even if the focus isn't on it.
I know sending keystrokes isn't the best way to download the file, but I've also tried this:

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

I always get an 403 Error and haven't found a solution yet to solve this problem...

like image 448
npit Avatar asked Jul 02 '26 16:07

npit


1 Answers

I'm not really a fan of SendKeys cause it almost always fail. If I use the provided link, I'm able to download the file with this Powershell code:

$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:\TEMP\5MB.zip"

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

You might also need to add network credentials.

like image 137
user3316995 Avatar answered Jul 04 '26 04:07

user3316995



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!