From: DDFrank on
Hi

I am trying to write a script which reads a list of servers from a txt
document and then PING each server. It reads the file and adds each server,
in turn, to a variable. When I try to run an object which incorporates
running PING and should PING the variable content (the server name) it fails
to be able to read the contents of the variable. How can I get this to work?
I have tried all sorts of scenarios, by changing the syntax and even tried to
use a Function but get the same results.

DO While ObjTS.AtEndOfStream<>True
stroutput = objTS.Readline
Set ObjExec = Objshell.exec("ping.exe stroutput")

strPingResults = LCase(objExec.StdOut.ReadAll)

If InStr(strPingResults, "reply from") Then
WScript.Echo stroutput & " responded to ping."
Else
WScript.Echo stroutput & " did not respond to ping."
End If

'script continues

Loop
From: Tom Lavedas on
On Jun 10, 10:11 am, DDFrank <DDFr...(a)discussions.microsoft.com>
wrote:
> Hi
>
> I am trying to write a script which reads a list of servers from a txt
> document and then PING each server. It reads the file and adds each server,
> in turn, to a variable. When I try to run an object which  incorporates
> running PING and should PING the variable content (the server name) it fails
> to be able to read the contents of the variable. How can I get this to work?
> I have tried all sorts of scenarios, by changing the syntax and even tried to
> use a Function but get the same results.
>
> DO While ObjTS.AtEndOfStream<>True
> stroutput = objTS.Readline
> Set ObjExec = Objshell.exec("ping.exe stroutput")
>
> strPingResults = LCase(objExec.StdOut.ReadAll)
>
> If InStr(strPingResults, "reply from") Then
>   WScript.Echo stroutput & " responded to ping."  
> Else
>   WScript.Echo stroutput & " did not respond to ping."
> End If
>
> 'script continues
>
> Loop

Here is an old routine that doesn't require the Exec or file
reading ...

If isconnectible(stroutput , 1,250) Then
WScript.Echo stroutput & " responded to ping."
Else
WScript.Echo stroutput & " did not respond to ping."
End If

Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Authors: Alex Angelopoulos/Torgeir Bakken
' Modified by: Tom Lavedas
' Works an "all" WSH versions
' sHost is a hostname or IP
' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used

Dim nRes
If iPings = "" Then iPings = 1 ' default number of pings
If iTO = "" Then iTO = 250 ' default timeout per ping
with CreateObject("WScript.Shell")
nRes = .Run("%comspec% /c ping.exe -n " & iPings & " -w " & iTO
_
& " " & sHost & " | find ""TTL="" > nul 2>&1", 0, True)
end with
IsConnectible = (nRes = 0)

End Function

For systems operating with XP or later, there is also a
Win32_PingStatus class. An example from the MS Technet Script Center
is as follows ...

strMachines = "atl-dc-01;atl-win2k-01;atl-nt4-01;atl-dc-02"
aMachines = split(strMachines, ";")

For Each machine in aMachines
Set objPing = GetObject("winmgmts:
{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& machine & "'")
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0
Then
WScript.Echo("machine " & machine & " is not reachable")
End If
Next
Next

Take your pick.
_____________________
Tom Lavedas
From: Al Dunbar on


"DDFrank" <DDFrank(a)discussions.microsoft.com> wrote in message
news:4C97449E-F266-4DDE-B36E-915FF6DF05D9(a)microsoft.com...
> Hi
>
> I am trying to write a script which reads a list of servers from a txt
> document and then PING each server. It reads the file and adds each
> server,
> in turn, to a variable. When I try to run an object which incorporates
> running PING and should PING the variable content (the server name) it
> fails
> to be able to read the contents of the variable. How can I get this to
> work?
> I have tried all sorts of scenarios, by changing the syntax and even tried
> to
> use a Function but get the same results.
>
> DO While ObjTS.AtEndOfStream<>True
> stroutput = objTS.Readline
> Set ObjExec = Objshell.exec("ping.exe stroutput")

Think about it - this is equivalent to typing this command:

ping.exe stroutput

..Exec is not failing to read the contents of the variable, you are simply
not telling it to use the variable contents but the variable name.

Try this instead:

Set ObjExec = Objshell.exec("ping.exe " & stroutput)


/Al



> strPingResults = LCase(objExec.StdOut.ReadAll)
>
> If InStr(strPingResults, "reply from") Then
> WScript.Echo stroutput & " responded to ping."
> Else
> WScript.Echo stroutput & " did not respond to ping."
> End If
>
> 'script continues
>
> Loop