3

Alright, so I have been trying to do this for a while, and I have come to the realization that this isn't really something that is often asked, and with vb6 getting phased out more and more, there seems to be less help than I would like regarding the language.

The title doesn't say it all actually, as I am looking to do something very specific. I need to execute a shell command (that I know how to do), however, after I execute it, I want to be able to save the return value of that command as a string. For example, if the command is ipconfig, I want the entire return value of that, all the text I would see in powershell after executing that command, saved to a string in my program.

As far as I know, I need to "import" a few things, because I have to use WshShell, which I don't know where to get. So that's part of the question, what classes do I have to add and how, or if there is a way to do it without adding classes then even better. In addition, I have heard a lot about the use of CreatePipe and such regarding similar problems, but I don't know how to use it.

Basically, what I'm saying is that I am quite uneducated regarding the subject, and any insight would be much appreciated, and thanks to all who reply.

1
  • 1
    You don't need to import anything for WshShell - just add a reference to Windows Script Hosting. As for output from shell command, I think one idea would be to pipe the output from the shell command to a text file, wait for shell to finish and then read the text file Commented Jul 8, 2016 at 0:11

1 Answer 1

3

There are many ways. Using VBScript's WSHShell.Exec is the easiest.

This is VBScript but VBScript can be pasted into VB (not vice versa though).

Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")

Set oExec = WshShell.Exec("ipconfig")

Do While oExec.Status = 0
     WScript.Sleep 100
Loop
MsgBox oExec.StdOut.ReadAll

Slightly modified from help.

This is how to ping from VBS/VB

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * From win32_PingStatus where address='104.43.195.251'")
'msgbox colItems
For Each objItem in colItems
    msgbox "Status" & objItem.statuscode & " Time " & objItem.ResponseTime
Next
Sign up to request clarification or add additional context in comments.

1 Comment

If I do this, will the powershell session close after this is completed? I hope not, because it is important for it to stay open. While I see no reason why it shouldn't close, one never can tell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.