N
N
Nordman992020-01-19 09:56:21
VBScript
Nordman99, 2020-01-19 09:56:21

How to execute commands on the command line from VB and get the result of their execution?

I welcome everyone! I started studying VBS in order to automate various processes on the computer
. At the moment, there is such a task - to ping a certain IP on the Internet - if it pings, then the Internet is working
Then - to ping a certain domain name on the Internet - if it pings, then the DNS of the norms is working
Already I haven’t tried any options, but they all boil down to creating a WScript.CreateObject("WScript.Shell") object
and using its Exec or Run methods, I don't see any other methods in VBS
Tried:
Set WshShell = CreateObject("WScript.Shell")
set WshExec = WshShell.Exec ("cmd /k ping 8.8.8.8") - I don't recommend anyone to try this option - it starts endlessly opening and closing the CMD window without any results,
Set WshShell = CreateObject("WScript.Shell")
set WshExec = WshShell.Exec ( "cmd /with ping 8.8.8.8") - Already better - the window opens only once - but again to no avail - nothing is executed in it and no no results are
returned Set WshShell = CreateObject("WScript.Shell")
WshShell.run ( "cmd /c ping 8.8.8.8", 1 ,true) - a CMD window opens and the ping 8.8.8.8 command is sent to it, but it is not executed after it
again prompts you to enter C:\> it again receives the ping 8.8.8.8 command and everything repeats
5e23fdc6265d5597811741.jpeg
Moreover, what if you even just open notepad
Set WshShell = CreateObject("WScript.Shell")
set WshExec = WshShell.Exec ( "notepad ")
or
Set WshShell = CreateObject("WScript.Shell")
WshShell.run ( "notepad", 1 ,true)
Then the notepad opens without problems
. How to perform my task in VBS - please tell me

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
CityCat4, 2020-01-19
@Nordman99

Okay, google.
What is WMI.
By God, this is worth looking into. It's a thing that can do everything .
Here's an example of how to ping:

' Check on box availability 
' Input:        strComputer                     -       checking box name
Function boxAvailable(strComputer)
  
  Dim objPing, objStatus
  
  Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
        ExecQuery("select * from Win32_PingStatus where address = '" & strComputer & "'")
        
  For Each objStatus in objPing
    If (IsNull(objStatus.StatusCode) Or objStatus.StatusCode <> 0) Then
      boxAvailable = False
     Else
       boxAvailable = True
    End If
  Next
  
End Function

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question