Computer? Are You There?

Computer? Are You There?
Filed under: Management
Shadrocks's picture

Ever needed a script to ping a computers before performing some action against them? I have! I hate waiting forever for a script to complete because the list of computers were all offline.

Here is another more pertinent example of why this is important. Before I join a computer to the domain as part of my automated post image rebuild process, I ping the domain name to ensure it can communicate before running the join domain script. If I don't do this and run the script, the computer will appear and behave as though it's part of the domain but the domain will not recognize it as being registered.

This code will help you solve those problems.

Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("cmd /c ping -n 3 -w 1000 192.168.1.112")
Do While Not objExecObject.StdOut.AtEndOfStream
  strText = objExecObject.StdOut.ReadLine()
  If Instr(strText, "Reply") > 0 Then
    Wscript.Echo "Reply received."
    Exit Do
  End If
Loop

You can obviously write in additional code and variablize the IP address with a string of addresses. That makes this a valuable code snippet for beginners. Need to run a script against a list of computers with a VBscript, but it takes forever because half of the machines are not responding? Just modify this script to read in from a list and ping each one, then log the results or execute some code against.

Another usage idea for this is to create the additional code to read from a list of computers that are not responding to your Altiris servers, ping them, if they exists, restart the proper client. I use PSService (Part of PSTOOLS) for the last portion of this (http://technet.microsoft.com/en-us/sysinternals/bb...).

Here is an example script. The script will read from a list of computer in the file computers.txt in the same directory as the script.

'*******************************************************
On Error Resume Next

'Initialize global constants and variables.
Const FOR_READING = 1
g_strHostFile = "computers.txt"

'Read computer names for install from text file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(g_strHostFile) Then
 Set objTextStream = objFSO.OpenTextFile(g_strHostFile, FOR_READING)
Else
 WScript.Echo "Input file " & g_strHostFile & " not found."
 WScript.Quit
End If

'Loop through list of computers and perform tasks on each.
Do Until objTextStream.AtEndOfStream
 g_strComputer = objTextStream.ReadLine
 Wscript.Echo VbCrLf & g_strComputer
 Wscript.Echo String(Len(g_strComputer), "-")
'Ping host to ensure that it is accessible.
 blnPing = PingHost
 If blnPing = True Then
  WScript.Echo g_strComputer & " responded To ping."
  RestartAclient '############## RUN THIS Function ##################
 Else
  WScript.Echo "ERROR: " & g_strComputer & " did not respond to ping."
 End If
Loop
objTextStream.Close

'*******************************************************

Function PingHost

Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("ping -n 1 -w 1000 " & g_strComputer)
strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
 PingHost = True
Else
 PingHost = False
End If

End Function

'*******************************************************

Function RestartAclient

Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("psservice \\" & g_strComputer & " -u adminuser -p YourPassword restart aclient " )
 WScript.Echo g_strComputer & " Aclient Restart Sent"
strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
 PingHost = True
Else
 PingHost = False
End If

End Function

Enjoy all of the new possibilities now that you know the computer will exist before you execute something against it.

3.81081
Average: 3.8 (37 votes)
Syndicate content