• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1239
  • Last Modified:

Run shell script in silent mode

Does anyone know how to chang the code below t run in silent mode and not pop up the window during the ping event?

Function SystemOnline(ByVal ComputerName As String)

Dim oShell, oExec As Variant
Dim strText, strCmd As String
 
strText = ""
strCmd = "ping -n 3 -w 1000 " & ComputerName
Set oShell = CreateObject("WScript.Shell")
Set oExec = oShell.Exec(strCmd)
 
Do While Not oExec.StdOut.AtEndOfStream
    strText = oExec.StdOut.ReadLine()
    If InStr(strText, "Reply") > 0 Then
        SystemOnline = True
        Exit Do
    End If
Loop

End Function
0
SweetingA
Asked:
SweetingA
  • 2
1 Solution
 
MacroShadowCommented:
The short answer is you can't, Shell.Exec can't be hidden, only Shell.Run supports that feature, the problem is you can't use Shell.Run to return info.
0
 
MacroShadowCommented:
For the longer answer...
You can direct the output from Shell.Run to a text file and parse it from there:
Option Explicit

Function SystemOnline(ByVal ComputerName As String) As Boolean

    Dim oFSO As Object, oShell As Object
    Dim oTextFile As Variant
    Dim strName As String, strTempFile As String, strText As String, strcomspec As String

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oShell = CreateObject("Wscript.Shell")
    strcomspec = oShell.ExpandEnvironmentStrings("%comspec%")
    strName = oFSO.GetTempName
    strTempFile = strName
    oShell.Run strcomspec & " /c ping -n 3 -w 1000 " & ComputerName & " > " & strTempFile, 0, True
    Set oTextFile = oFSO.OpenTextFile(strTempFile, 1)
    Do While oTextFile.AtEndOfStream <> True
        strText = oTextFile.ReadLine
        If InStr(strText, "Reply") > 0 Then
            SystemOnline = True
            Exit Do
        End If
    Loop
    oTextFile.Close
    oFSO.DeleteFile (strTempFile)
End Function

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now