Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

asked on

VB6 - Use CMD instead of Powershell

Hi,

I have below code that uses Powershell to pull some information. I would like to do the exact same thing, but using CMD instead. I can i do that?

Private Sub InfoAddressIp_Click()
Dim WshShell, oExec, sCommandLine
    sCommandLine = "ipconfig"

    Set WshShell = CreateObject("WScript.Shell")


    Set oExec = WshShell.Exec(sCommandLine)

    Do While oExec.Status = 0
         'WScript.Sleep 100
    Loop

    MsgBox oExec.StdOut.ReadAll
End Sub

Open in new window


Thanks again for your help
Avatar of Bill Prew
Bill Prew

That should work just as you have it, since ipoconfig is an actual EXE file.

Are you having a problem?


»bp
Avatar of Wilder1626

ASKER

By default, it use Windows Powershell instead of command prompt.
Ah, you must have made Powershell the default Windows shell on your computer?


»bp
If Bill is correct (and I suspect he is), go to the Taskbar Settings and change the 4th setting ("Replace command prompt with...") to off.

See figure B in this tutorial:
https://www.techrepublic.com/article/windows-10-april-2018-update-how-to-change-the-command-prompt-default-to-powershell
You might try starting a process right from VB rather than using a shell.  Try adapting these examples to VB and see if it works.



»bp
Thanks for the information. Let me go over those links, and will let you know shortly.
How do you know powershell is getting involved when you run your test?


»bp
In my VB6 application, i have a command button that runs the bellow script. But every time i click on that button, the Powershell blue window open for a couple of seconds before i get a message of the result. 
Private Sub InfoAddressIp_Click()
Dim WshShell, oExec, sCommandLine
    sCommandLine = "ipconfig"

    Set WshShell = CreateObject("WScript.Shell")


    Set oExec = WshShell.Exec(sCommandLine)

    Do While oExec.Status = 0
         'WScript.Sleep 100
    Loop

    MsgBox oExec.StdOut.ReadAll
End Sub

Open in new window

Do you get the output of the ipconfig command in the msgbox though, like you would with CMD?


»bp
yes i do get the msgbox with the info, same as in Powershell / cmd result if i query directly. let me try something here.
I'm so sorry, its totally my bad. I took the wrong code.

The code i should of paste is that one that clearly link to PowerShell but wondering how to use CMD to do the same thing.

Again big apologies for this huge confusion.
Dim WshShell, oExec, sCommandLine
   Set WshShell = CreateObject("WScript.Shell")

    sCommandLine = "powershell -executionpolicy bypass -Command ""get-service wuauserv | select Displayname,Status,ServiceName,Can*"""

    Set oExec = WshShell.Exec(sCommandLine)

    Do While oExec.Status = 0
         'WScript.Sleep 100
    Loop

    MsgBox oExec.StdOut.ReadAll


Open in new window

Phew, that makes a lot more sense, I thought my age was getting the best of me.

I'll try and work up a CMD based approach...


»bp
Here are a few you could try.  There are two main commands typically used to get info about services from the command line.  One is "sc" and the other is "wmic service".  You may have to explore some of the options of each to find the specific properties you need, Powershell seems to be calling them it's own property name and some are easy to map to these commands, some less obvious.  But wanted to get you this to play with.  Here's a little test I did (sand just ran as a VBS file from the DOS command prompt with CSCRIPT).

"sc /?" and "wmic service get /?" will give you some info on those commands and options.  Let me know how I can help further.

Test1
Test2
Test3

Sub Test1()
    Dim WshShell, oExec, sCommandLine

    Set WshShell = CreateObject("WScript.Shell")

    sCommandLine = "wmic service where (name='wuauserv') get * /format:list"

    Set oExec = WshShell.Exec(sCommandLine)

    Do While oExec.Status = 0
         'WScript.Sleep 100
    Loop

    MsgBox oExec.StdOut.ReadAll
End Sub

Sub Test2()
    Dim WshShell, oExec, sCommandLine

    Set WshShell = CreateObject("WScript.Shell")

    sCommandLine = "sc query wuauserv"

    Set oExec = WshShell.Exec(sCommandLine)

    Do While oExec.Status = 0
         'WScript.Sleep 100
    Loop

    MsgBox oExec.StdOut.ReadAll
End Sub

Sub Test3()
    Dim WshShell, oExec, sCommandLine

    Set WshShell = CreateObject("WScript.Shell")

    sCommandLine = "sc qc wuauserv"

    Set oExec = WshShell.Exec(sCommandLine)

    Do While oExec.Status = 0
         'WScript.Sleep 100
    Loop

    MsgBox oExec.StdOut.ReadAll
End Sub

Open in new window


»bp
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So far, i like you Test 2 but since you sent your last response,  i suddenly like that one that i can customize.
Glad that was helpful.


»bp