0

I use the following VB script ( example ) , to open CMD window and run ipconfig on my WIN XP

CMD window was opened

but from some reason WshShell.SendKeys not send the ipconfig on CMD window

please advice what chuld be the reasons that WshShell.SendKeys not send the "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
yael
  • 2,563

2 Answers2

1

The SendKeys method is not very reliable. Sometimes it doesn't know where the current focus is. If you start your script in cmd-window it works. However the keys are not send to newly opened shell (4ht line) but to current the cmd-window where the vbs-script was started.

tatus2
  • 139
0

Please try the following code. It worked for me.

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

'Ref: https://www.codeproject.com/Tips/507798/Differences-between-Run-and-Exec-VBScript

user35042
  • 2,761
  • 12
  • 39
  • 62