Andre Magyar
Andre Magyar

Reputation: 33

Getting actual powershell return value in vb6?

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.

Upvotes: 3

Views: 1488

Answers (1)

user6017774
user6017774

Reputation:

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

Upvotes: 3

Related Questions