powershell、sendkey() 和 appactive() 奇怪 [英] powershell, sendkey() and appactive() strangness

查看:37
本文介绍了powershell、sendkey() 和 appactive() 奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录 powershell 脚本.作为脚本的一部分,它启动一个应用程序,然后等待 4 秒钟等待计算机跟上,然后发送一些按键.

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.

似乎 AppActivate() 似乎并没有真正起作用.有人对此有任何意见吗?

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

推荐答案

您需要确保 App 是前台窗口.PowerShell Community Extensions 有一个 Set-ForegroundWindow cmdlet 可以帮助解决这个问题:

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}')

这是使用 PowerShell V2 的 Add-Type cmdlet.另请注意,SendKeys 在 .NET 中可用.

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

这篇关于powershell、sendkey() 和 appactive() 奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆