tags:

views:

284

answers:

2

I have a logon powershell script. As part of the script, it launches an app, then waits 4 seconds to wait for the computer to catch up, then sends some keystrokes.

$deviceID = "123xyz"
invoke-item ("C:\myapp")
...
$myshell = New-Object -com "Wscript.Shell"
start-sleep -s 4
$myshell.AppActivate("myapp");$myshell.sendkeys("1");$myshell.sendkeys("{TAB}");$myshell.sendkeys("$deviceID");$myshell.sendkeys("{ENTER}")

It works great, unless the user clicks anywhere during the logon. If they do, the app never gets the key strokes and the app flashes in the taskbar.

Seems like AppActivate() doesnt seem to really work. Does anyone have any input on this?

A: 

Sounds like the focus is being taken away from the active app and cannot be returned, possibly because the target of the clicks is modal (splash screen, etc). Is there anyway to send the data stream to the app via pipes or standard input?

GrayWizardx
this is other programs loading, but it works fine without a mouse click (to the desktop).AFAIK sendkeys() is the only why the send input without using 3rd party powerpaks etc.
falkaholic
Did you write the app your calling yourself? If so you have many options, if not there are still some. Depending on the type of app it may or may not accept commandline params, allow for redirecting of console input (StdIn, StdOut) and our support direct DDE.
GrayWizardx
+1  A: 

You need to make sure the App is the foreground window. The PowerShell Community Extensions has a Set-ForegroundWindow cmdlet that can help with this:

Add-Type -AssemblyName System.Windows.Forms
$proc = Get-Process notepad
Set-ForegroundWindow $proc.MainWindowHandle
Start-Sleep -Seconds 2 # Just so you can see the window before it closes :-)
[Windows.Forms.SendKeys]::SendWait('%{F4}')

This is using PowerShell V2's Add-Type cmdlet. Also note that SendKeys is available in .NET.

Keith Hill