在发送密钥之前确保 window 焦点
Ensure window focus before sending keys
我正在使用 VBScript SendKeys
访问 Cisco 客户端,因为它会提示输入用户名和密码。但是,我在集中命令提示符时遇到了问题。
我在每个 SendKeys
命令之前添加了 AppActivate
,但是正常的计算机使用经常会在这些命令之间的时间中断焦点。
如何在发送键之前确保命令提示符有焦点?
Dim host, username, password, pathToClient
host = "host"
username = "username"
password = "password"
pathToClient = "C:\Program Files {(}x86{)}\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe"
Set ws = WScript.CreateObject("WScript.Shell")
ws.run("TASKKILL.exe /F /IM vpnui.exe"), 0, false
ws.run("cmd.exe"), 2, false
WScript.Sleep 300
ws.AppActivate("Command Prompt")
ws.SendKeys """" & pathToClient & """ connect " & host & "~"
WScript.Sleep 1000
ws.AppActivate("Command Prompt")
ws.SendKeys(username & "~")
WScript.Sleep 50
ws.AppActivate("Command Prompt")
ws.SendKeys(password & "~")
ws.run("TASKKILL.exe /F /IM cmd.exe"), 0, false
使用 VBScript 则不能。 AppActivate
是 VBScript 中唯一可用的将焦点放在特定应用程序上的方法,它不能确保焦点停留在那里。这就是 SendKeys
无论如何都不应该用于自动化的确切原因。试试 AutoIt 之类的东西。
或者设置 Auto Connect on Start 可能是一个选项,因为您显然正在使用 AnyConnect?
Auto Connect on Start—AnyConnect, when started, automatically establishes a VPN connection with the secure gateway specified by the AnyConnect profile, or to the last gateway to which the client connected.
我用 AutoHotKey 解决了我的问题。这是我的代码:
host := "my.host.domain"
username := "myUsername"
password := "myPassword"
pathToClient = "C:\...\vpncli.exe"
DetectHiddenWindows, on
Process, Close, vpnui.exe
run, cmd.exe,, Hide
Sleep 100
ControlSend,, %pathToClient% connect %host%{Enter}, ahk_exe cmd.exe
Sleep 1000
ControlSend,, %username%{Enter}, ahk_exe cmd.exe
Sleep 50
ControlSend,, %password%{Enter}, ahk_exe cmd.exe
;Process, Close, cmd.exe
我正在使用 VBScript SendKeys
访问 Cisco 客户端,因为它会提示输入用户名和密码。但是,我在集中命令提示符时遇到了问题。
我在每个 SendKeys
命令之前添加了 AppActivate
,但是正常的计算机使用经常会在这些命令之间的时间中断焦点。
如何在发送键之前确保命令提示符有焦点?
Dim host, username, password, pathToClient
host = "host"
username = "username"
password = "password"
pathToClient = "C:\Program Files {(}x86{)}\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe"
Set ws = WScript.CreateObject("WScript.Shell")
ws.run("TASKKILL.exe /F /IM vpnui.exe"), 0, false
ws.run("cmd.exe"), 2, false
WScript.Sleep 300
ws.AppActivate("Command Prompt")
ws.SendKeys """" & pathToClient & """ connect " & host & "~"
WScript.Sleep 1000
ws.AppActivate("Command Prompt")
ws.SendKeys(username & "~")
WScript.Sleep 50
ws.AppActivate("Command Prompt")
ws.SendKeys(password & "~")
ws.run("TASKKILL.exe /F /IM cmd.exe"), 0, false
使用 VBScript 则不能。 AppActivate
是 VBScript 中唯一可用的将焦点放在特定应用程序上的方法,它不能确保焦点停留在那里。这就是 SendKeys
无论如何都不应该用于自动化的确切原因。试试 AutoIt 之类的东西。
或者设置 Auto Connect on Start 可能是一个选项,因为您显然正在使用 AnyConnect?
Auto Connect on Start—AnyConnect, when started, automatically establishes a VPN connection with the secure gateway specified by the AnyConnect profile, or to the last gateway to which the client connected.
我用 AutoHotKey 解决了我的问题。这是我的代码:
host := "my.host.domain"
username := "myUsername"
password := "myPassword"
pathToClient = "C:\...\vpncli.exe"
DetectHiddenWindows, on
Process, Close, vpnui.exe
run, cmd.exe,, Hide
Sleep 100
ControlSend,, %pathToClient% connect %host%{Enter}, ahk_exe cmd.exe
Sleep 1000
ControlSend,, %username%{Enter}, ahk_exe cmd.exe
Sleep 50
ControlSend,, %password%{Enter}, ahk_exe cmd.exe
;Process, Close, cmd.exe