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

2 Answers 2

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.

3
  • what the good alternative for WshShell.SendKeys ?
    – yael
    Jun 30, 2013 at 11:18
  • start your script in cmd. However, if you start to work in other application it will get the focus and as a consequence the commands ipconfig and enter will be executed in it. f.e. If you work in Word suddenly "you will" write ipconfig. Do you really need to use the sendkeys? Wouldn't it be better to start ipconfig as a command and direct the output to the file and eventually analyze this file?
    – tatus2
    Jun 30, 2013 at 14:33
  • if you really want to send string to application then you can use smth like: strCmd = "shell.exe"; Set WshShell = CreateObject("WScript.Shell");Set WshShellExec = WshShell.Exec(strCmd); WshShellExec.StdIn.WriteLine("ipconfig"); WshShellExec.StdIn.WriteLine("")
    – tatus2
    Jun 30, 2013 at 14:49
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.