?
Solved

Run shell script in silent mode

Posted on 2013-06-27
2
Medium Priority
?
1,176 Views
Last Modified: 2013-07-04
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
Comment
Question by:SweetingA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39281872
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
 
LVL 27

Accepted Solution

by:
MacroShadow earned 2000 total points
ID: 39282135
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question