0

I have created a VBScript and have no idea why it won't echo out the results. I know the cmd command works on its own if I ended the script there as I set the parameter to 1 and seen the output.

I have also tried storing it as strObjPing but that doesn't work either

'' Sets up shell ''
Set objShell = CreateObject("Wscript.Shell")

'' Gets hostname ''
Dim strComputerName
strComputerName = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

'' Runs ping in cmd. Stores output. ''
Dim ObjPing
ObjPing = objShell.Run "ping " & strComputerName, 1, True

'' Execute program and read the output into a variable line by line ''
Dim strFromProc
Do
    strFromProc = ObjPing.StdOut.ReadLine()
    WScript.Echo "Output is: " & strFromProc
Loop While Not ObjPing.Stdout.atEndOfStream

objShell.Quit
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
V Krishan
  • 153
  • 3
  • 15
  • When you set object references you need to prefix with `Set`, like so `Set ObjPing = objShell.Run("ping " & strComputerName, 1, true)`. Without an object reference you'll never be able to access `ObjPing.StdOut` but also that only works with the `Exec()` method not `Run()`. Basically, you read the documentation, plus there are other questions that deal with this exact problem. – user692942 Dec 13 '16 at 12:25
  • Worthwhile reading [Exec Method (Windows Script Host)](https://msdn.microsoft.com/en-us/library/ateytk4a(v=vs.84).aspx) before even asking here. – user692942 Dec 13 '16 at 12:31

0 Answers0