wsh / vbs simple query read

Soldato
Joined
17 Jul 2008
Posts
7,392
I jsut want it to execute the "objShell.Run("ping " & strContents)" one at a time currently if there are 50 servers in the list it will ping all 50 at the same time (ie open 50 shells at once) (eventually it will not be a ping it will be a batch file that writes to a log file so they have to run one after another)

Dim objResult
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("test.txt", 1)
Set objShell = WScript.CreateObject("WScript.Shell")
Do Until objFile.AtEndOfStream
strContents = objFile.Readline
objResult = objShell.Run("ping " & strContents)
Loop
objFile.Close
 
Your script isn't waiting for the return from the command prompt and keeps on looping through your script.

Try the modification below which uses a ping function without the need to call a command prompt instance:

Code:
Dim objResult
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("test.txt", 1)
Set objShell = WScript.CreateObject("WScript.Shell")
Do Until objFile.AtEndOfStream
strContents = objFile.Readline
objResult = CBool(Ping(strContents))

Dim status
If objResult = True Then
	status = "UP"
else
	status = "DOWN"
End If

WScript.Echo strContents & ": " & status

Loop
objFile.Close 

Function Ping( myHostName )
' This function returns True if the specified host could be pinged.
' myHostName can be a computer name or IP address.
' The Win32_PingStatus class used in this function requires Windows XP or later.
' This function is based on the TestPing function in a sample script by Don Jones
' http://www.scriptinganswers.com/vault/computer%20management/default.asp#activedirectoryquickworkstationinventorytxt

    ' Standard housekeeping
    Dim colPingResults, objPingResult, strQuery

    ' Define the WMI query
    strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & myHostName & "'"

    ' Run the WMI query
    Set colPingResults = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )

    ' Translate the query results to either True or False
    For Each objPingResult In colPingResults
        If Not IsObject( objPingResult ) Then
            Ping = False
        ElseIf objPingResult.StatusCode = 0 Then
            Ping = True
        Else
            Ping = False
        End If
    Next

    Set colPingResults = Nothing
End Function

Ping function: http://www.robvanderwoude.com/vbstech_network_ping.php
 
Back
Top Bottom