Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sending keystrokes by send-keys

Status
Not open for further replies.

Kazendar

Programmer
Oct 23, 2008
18
LB
Does PowerShell v.1 has an ability to use [red]send-keys[/red] as keystrokes to with Windows programs ? like in VBS
 
Thanks, i need it to run some Windows programs, like ArcView from ESRI. It is easer for me to create
a script as macro with sendKeys
 
I used VBS in the past, but not anymore.
Today i am trying to write scripts in PowerShell v1 if possible .
 
Thanks crobin, i checked the thread on Egghead Cafe, and find some strange letters, so i did not try it, maybe the script for [red]CTP2[/red] !!!

I found the script from MicroSoft very usefull, and write the next script. I hope some enhancement

<code>
# test_notePad.ps1

function wait {
param([int]$stop = 3)
Start-Sleep -seconds $stop
}

[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
& "$env:WinDir\Notepad.exe"
$a = Get-Process | Where-Object {$_.Name -eq "Notepad"}
wait
[Microsoft.VisualBasic.Interaction]::AppActivate($a.ID)
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
wait
[System.Windows.Forms.SendKeys]::SendWait("{F5}{ENTER}")
[System.Windows.Forms.SendKeys]::SendWait("~Name:{TAB}$env:UserName~")
[System.Windows.Forms.SendKeys]::SendWait("ABCDEFGHIJKLM~")
[System.Windows.Forms.SendKeys]::SendWait("ABCDEFGHIJKLM{BACKSPACE}~")
[System.Windows.Forms.SendKeys]::SendWait("^{HOME}{RIGHT}{RIGHT}{RIGHT}{RIGHT}{RIGHT}~{DELETE}")
wait
[System.Windows.Forms.SendKeys]::SendWait("%F")
wait
[System.Windows.Forms.SendKeys]::SendWait("X")
wait
[System.Windows.Forms.SendKeys]::SendWait("N")
exit
</code>
 
The odd characters equate to dashes and quote marks. I'm guessing there was a character set mismatch between the original poster and Web site.

There are 2 ways to send key strokes. One way is by going through the .Net Framework as in the example from Microsoft. The other way is by calling Windows Script Host as a COM object as in the example on Egghead Cafe.

In the Egghead Cafe post, the outline is this:
1. Create a new Windows Script Host COM object (new-object)
2. Start the desired application (.Run)
3. Make sure the application started (.AppActivate)
4. Send keystrokes to the application

The original poster had a problem with step 3 because he didn't have the application's Window Title correct.

Note, as listed in one of the comments on the Egghead Cafe thread, you may want to check out "Windows Automation Snapin for PowerShell", So this
Code:
$a = Get-Process | Where-Object {$_.Name -eq "Notepad"}
wait
[Microsoft.VisualBasic.Interaction]::AppActivate($a.ID)
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
wait
[System.Windows.Forms.SendKeys]::SendWait("{F5}{ENTER}")
becomes
Code:
Select-Window notepad | Send-Keys "{F5}{ENTER}"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top