VB脚本+运行CMD并使用SendKeys显示网络地址

VB脚本+运行CMD并使用SendKeys显示网络地址

我使用以下 VB 脚本(示例),在我的 WIN XP 上打开 CMD 窗口并运行 ipconfig

CMD窗口已打开

但由于某种原因,WshShell.SendKeys 没有在 CMD 窗口上发送 ipconfig

请指教 WshShell.SendKeys 无法发送“ipconfig”的原因可能是什么?

Option Explicit

Dim WshShell

set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.run("cmd.exe")
WScript.Sleep 5000
WshShell.SendKeys "ipconfig" 
WshShell.SendKeys("{Enter}")
WScript.Sleep 5000

答案1

SendKeys 方法不太可靠。有时它不知道当前焦点在哪里。如果您在cmd-window 中启动脚本,它就可以工作。但是,键不会发送到新打开的 shell(4ht 行),而是发送到启动 vbs 脚本的当前 cmd 窗口。

答案2

请尝试以下代码。它对我有用。

Dim o, i, strErr
Set o = createobject("WScript.Shell")
strErr = o.Run ("cmd /k ipconfig.exe", 1 , False)
WScript.Echo strErr
'strErr return code, indicating the 
Set o = nothing

‘参考:https://www.codeproject.com/Tips/507798/Differences-between-Run-and-Exec-VBScript

相关内容