VBscript_book If you looked at yesterday’s post, carefully I’ll admit as WordPress loves to mangle the display of <CODE>; you might have noticed that I check to see if a computer is on the network, before I try and do something to it.
The reason is simple.
Checking that a computer is on the network, is faster, than just trying to copy a file to it, and checking for a failure.  I don’t know why, but I suspect a “Ping” is faster than a file operation.
Here is some (crude) code I wrote the other day, which shows a ping in use:

Set objFSO = CreateObject("Scripting.FileSystemObject")

If not objFSO.FileExists("C:\computer_lists\file_targets.txt") Then
   WScript.Quit(99)
End If


Set ObjStream = objFSO.OpenTextFile("C:\computer_lists\file_targets.txt",1)

file_destination = ""

Do While Not ObjStream.AtEndOfStream
   strComputer = ObjStream.ReadLine
   If strComputer = ""    Then
      WScript.Quit
   End If

   If Reachable(strComputer) Then
      file_destination = "\\" + strComputer + "\c$\pluckaduck\raffle.exe"

              On Error Resume Next
      objFSO.CopyFile "c:\pluckaduck\raffle.exe",file_destination,True
      If Err.Number <> 0 Then
         WScript.echo strComputer + " - failed copy"
      Else
         WScript.Echo strComputer
      End If

      On Error GoTo 0
   Else
      WScript.Echo "Computer is Unreachable!"
   End If

Loop

WScript.Quit

Function Reachable(strComputer)
   '     On Error Resume Next
   Dim wmiQuery, objWMIService, objPing, objStatus
   wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputer & "'"
   Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
   Set objPing = objWMIService.ExecQuery(wmiQuery)

   For Each objStatus In objPing
      If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
         Reachable = False 'if computer is unreacable, return false
      Else
         Reachable = True 'if computer is reachable, return true
      End If
   Next
End Function

I found the code for the Reachable Function here.

Update Sept 2010:
there is a new version of the Reachable function here.
Bookmark and Share