5

Am in process of automating the daily download of a zip file from secure site. Script is ready which uses the internet explorer to login and go to the required location and then click on download button, script works as expected till here.

Upon clicking download button, it prompts to click on save button. Have tried with send keys with below

$wshell = New-Object -ComObject WScript.Shell    
$id = (gps iex* | where {$_.MainWindowTitle -match "Title"}).id    
$wshell.AppActivate($id)    
start-sleep 1    
$wshell.SendKeys("%{n}")    
Start-Sleep 1  

want to send keys (Alt+n + TAB + ENTER), tried by changing few things but end up with the same result.

Jos8483
  • 53
  • 1
  • 1
  • 5
  • 2
    As a tip : using SendKeys etc is probably the worst way to automate website related stuff. The cleanest way to do it is to inspect the webrequests with a tool like fiddler, do a login etc so you can use the regular Invoke-Webrequest cmdlets to do this. – bluuf Mar 05 '19 at 09:59

1 Answers1

7

To emulate send keys you want to use System.Windows.Forms.SendKeys class.

The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses () have special meanings to SendKeys. To specify one of these characters, enclose it within braces ({})

In your case, according to documentation, code sample should look like:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("%n{TAB}{ENTER}")

Where:

  • % stands for ALT button;
  • n stands for n button;
  • {TAB} stands for TAB button;
  • {ENTER} stands for ENTER button.

Please follow the documentation page to can see complete list of available options here.

Kirill Pashkov
  • 3,118
  • 1
  • 15
  • 20