一个测试用的VBS脚本样例时间:2021-10-30 08:15:25Const KENTER="{ENTER}" Const KALTF4="%{F4}" Const KUP="{UP}" Const KDOWN="{DOWN}" Const KLEFT="{LEFT}" Const KRIGHT="{RIGHT}" Const ONE_SECOND=1000 Dim WaitCount Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.Run "FreeView" WScript.Sleep ONE_SECOND*10 'WshShell.SendKeys("rootroot") 'WScript.Sleep 1000 While True While WshShell.AppActivate("虚拟桌面")=False WScript.Sleep ONE_SECOND/10 Wend SendKey(KENTER) 'MsgBox "登录开始。" While WshShell.AppActivate("桌面列表")=False WScript.Sleep ONE_SECOND/10 Wend 'MsgBox "进入桌面列表。" WScript.Sleep ONE_SECOND SendKey(KDOWN) SendKey(KDOWN) WScript.Sleep ONE_SECOND/10 SendKey(KDOWN) WScript.Sleep ONE_SECOND/10 SendKey(KDOWN) WScript.Sleep ONE_SECOND/10 SendKey(KDOWN) WScript.Sleep ONE_SECOND/10 SendKey(KDOWN) 'MsgBox "开始连接桌面。" WScript.Sleep ONE_SECOND/10 SendKey(KENTER) WaitCount = 0 Do WScript.Sleep ONE_SECOND/2 WaitCount += 1 If WaitCount > 600 Then '超过300s没有连接上,再次发送Enter键 SendKey(KENTER) WaitCount = 0 End If Loop While Not IsRdpStart WScript.Sleep ONE_SECOND*10 WshShell.Run "taskkill /F /IM rdp.exe /T",0,True WScript.Sleep ONE_SECOND SendKey(KUP) SendKey(KUP) While WshShell.AppActivate("桌面列表")=False WScript.Sleep ONE_SECOND/10 Wend 'MsgBox "关闭1" WScript.Sleep ONE_SECOND*2 SendKey(KALTF4) WScript.Sleep ONE_SECOND 'MsgBox "关闭2" Wend Sub SendKey(Key) WshShell.SendKeys Key End Sub Function IsRdpStart() Dim Pos,s '生成批处理文件 Set fso = WScript.CreateObject("Scripting.FileSystemObject") Set BatFile = fso.CreateTextFile("task.bat",True) BatFile.WriteLine("tasklist /FI ""IMAGENAME eq rdp.exe"" /FO CSV /NH > test.dat") BatFile.Close Set Exec = WScript.CreateObject("WScript.Shell") '0,不显示执行窗口。1,等待命令执行完成 Exec.Run "task.bat",0,1 '读取批处理程序执行结果 s="" Set ts = fso.OpenTextFile("test.dat") s = ts.ReadAll ts.Close() '删除临时文件 Set ts = fso.GetFile("test.dat") ts.Delete Set BatFile = fso.GetFile("task.bat") BatFile.Delete '检查结果 'MsgBox "" & s & "" Pos = InStr(s,"rdp.exe") If Pos < 1 Then IsRdpStart = False Else IsRdpStart = True End If End Function Function IsRdpStart2() Dim Pos,WshShell, oExec, input Set WshShell = WScript.CreateObject("WScript.Shell") Set oExec = WshShell.Exec("tasklist /FI ""IMAGENAME eq rdp.exe"" /FO CSV /NH") input = "" Do While True If Not oExec.StdOut.AtEndOfStream Then input = input & oExec.StdOut.ReadLine Else Exit Do End If WScript.Sleep 100 Loop '检查结果 Pos = InStr(input,"rdp.exe") If Pos < 1 Then IsRdpStart2 = False Else IsRdpStart2 = True End If End Function