我想使用VBScript将地址注入“远程桌面连接”提示符。有人告诉我,我必须使用Windows Automation API来做这件事,但在阅读完文档之后,我找不到任何可以用于VBScript的东西。我该怎么办?
PS:Helen和this thread指出,目前不支持VBScript访问UI Automation API。
答案 0 :(得分:8)
这里实际上并不需要GUI自动化。要指定要连接的计算机,只需使用/v
命令行参数启动mstsc
,例如:
CreateObject("WScript.Shell").Run "mstsc /v:computername"
或者,如果您的.rdp file包含计算机名称和连接设置,则可以使用mstsc
启动此文件:
CreateObject("WScript.Shell").Run "mstsc E:\ComputerName.rdp"
如果需要,您可以动态生成.rdp文件,如下所示:
Dim oFSO, oShell, strFileName, strComputerName
strComputerName = "computername"
strFileName = "E:\ComputerName.rdp"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oStream = oFSO.CreateTextFile(strFileName, True)
oStream.WriteLine "full address:s:" + strComputerName
' TODO: Write other settings
oStream.Close
Set oShell = CreateObject("WScript.Shell")
oShell.Run "mstsc """ + strFileName + """"
回复评论:
然而,我想要实现的不是RDP的任务,而是实际的注入本身(这可以推广到不同应用的不同窗口)。
Windows脚本宿主提供了用于GUI自动化的AppActivate
和SendKeys
方法,但它们并非万无一失。
我建议使用GUI自动化工具,例如AutoIt(免费)。在AutoIt脚本中,您可以使用ControlSetText
函数更改输入字段中的文本,例如:
Run("notepad.exe")
WinWait("[CLASS:Notepad]")
ControlSetText("[CLASS:Notepad]", "", "Edit1", "Hello, world!")
您还可以将AutoIt的AU3Recorder用于record用户操作,这样您就不必手动编写脚本。