Pinging computers in Active Directory

Associate
Joined
3 Jan 2009
Posts
2,056
Location
London
Hi all, I'm very much hoping one of you can help me with this :)

Its as title says, I have these laptops in Active directory which I want to ping, now what I wanted to do is export them into txt format and make some sort of batch file to ping them.

I have no idea how to do this, can somebody point me in the right direction pleasee :D

I have attached a screenshot so you guys can see the computers I'm trying to ping.

85793767.jpg
 
What are you trying to achieve? Based on your simple requirements something like:

ping CFS-D510-05 > ping.txt
ping CFS-D510-06 >> ping.txt
ping CFS-D510-07 >> ping.txt
ping CFS-D510-08 >> ping.txt (you get the idea)
EXIT
 
Heres something similar I did many moons ago. Need to change it from doing event logs to doing pings. Stick it in a text file and run it with cscript.exe

' Constants

WScript.Echo "Initializing..."

Const ForAppending = 8
Const ADS_SCOPE_SUBTREE = 2
auditfilename = "eventlogs" & DatePart("yyyy" , Now) & DatePart("m" , Now) & DatePart("d" , Now) & ".log"


' Setup objects

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(auditfilename) Then
Set objFile = objFSO.OpenTextFile(auditfilename, ForAppending)
Else
Set objFile = objFSO.CreateTextFile(auditfilename)
End If


' Main

' Enumerate All Computers Accounts in AD

objFile.WriteLine " "
objFile.WriteLine " "
objFile.WriteLine " "
objFile.WriteLine "Script Initialize OK."
objFile.WriteLine "Starting Machine Audit " & DatePart("d" , Now) & " " & DatePart("m" , Now) & " " & DatePart("yyyy" , Now) & " @ " & DatePart("h", Now) & ":" & DatePart("n" , Now) & ":" & DatePart("s" , Now)
objFile.WriteLine " "
objFile.WriteLine "Connecting to Active Directory"

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")

objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "SELECT Name, Location FROM 'LDAP://DC=YOURDOMAINNAME,DC=NET' WHERE objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
i = 0
Do Until objRecordSet.EOF
i = i + 1
objRecordSet.MoveNext
Loop

objFile.WriteLine "List of computers from AD enumerated"
objFile.WriteLine " "
objFile.WriteLine "Starting computer event log audit..."

WScript.Echo "Examine machines...(" & i & " to go)"

On Error Resume Next

objRecordSet.ReQuery
objRecordSet.MoveFirst
j = 1
Do Until objRecordSet.EOF

objFile.WriteLine "*************" & objRecordSet.Fields("Name").Value & " (" & j & "/" & i & ")************"
WScript.Echo "Currently Examining: " & objRecordSet.Fields("Name").Value & "(" & j & "/" & i & ")"


strComputer = objRecordSet.Fields("Name").Value

*** STICK THE PING IN HERE, AND POSSIBLY DELETE REST IN LOOP ***

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
If Err.Number <> 0 Then
Wscript.Echo strComputer & " " & Err.Description
objFile.WriteLine strComputer & " " & Err.Description
Err.Clear
Else

Set colLoggedEvents = objWMIService.ExecQuery _
("SELECT * FROM Win32_NTLogEvent WHERE Logfile = 'System' AND Type = 'Error'")

For each objEvent in colLoggedEvents
objFile.WriteLine "Category: " & objEvent.Category
objFile.WriteLine "Computer Name: " & objEvent.ComputerName
objFile.WriteLine "Event Code: " & objEvent.EventCode
objFile.WriteLine "Message: " & objEvent.Message
objFile.WriteLine "Record Number: " & objEvent.RecordNumber
objFile.WriteLine "Source Name: " & objEvent.SourceName
objFile.WriteLine "Time Written: " & objEvent.TimeWritten
objFile.WriteLine "Event Type: " & objEvent.Type
objFile.WriteLine "User: " & objEvent.User
objFile.WriteLine bjEvent.LogFile
Next

End If

objFile.WriteLine " "
j = j + 1
objRecordSet.MoveNext
Loop

objFile.WriteLine "Computer audit finished."
objFile.WriteLine "Finishing Machine Audit " & DatePart("d" , Now) & DatePart("m" , Now) & DatePart("yyyy" , Now) & " @ " & DatePart("h", Now) & ":" & DatePart("n" , Now) & ":" & DatePart("s" , Now)
objFile.WriteLine "*****************************Closing Audit File*************************************************"

objFile.Close
 
I use this little batch file now and again

@Echo ON

SET sListOfPCs=<INPUT TEXT FILE LOCATION>\PCs.txt
SET sListOfPCsUp=<OUTPUT TEXT FILE LOCATION>\PCS_UP.LOG

DEL "%sListOfPCsUp%" /F > NUL 2>&1

FOR /F "TOKENS=*" %%A IN ('TYPE "%sListOfPCs%"') DO (
PING %%A > NUL 2>&1
IF NOT ERRORLEVEL 1 ECHO.%%A>>"%sListOfPCsUp%"
)

Where PCs.txt is a text file with all the hostnames I want to ping, and PCS_UP.log is the output.

It's very crude but did the job at the time.
 

Saw this, not quite what I wanted ;)

I managed to do it in the end by writing all names up in excel, then writing the code and then copying that into a text document.

What are you trying to achieve? Based on your simple requirements something like:

Aim was to figure out which computers were in cupboard or bin. If they haven't been turned on in a week they lose their IP address and say its not a recognised IP address whereas if a PC is just off and has been turned on in the last week it says something else.

Because all PCs here are likely to have been turned on in the last week its easy to know which are still active and which are dead ;)

If that makes any sense at all?
 
Nope.

If you want to find out which are dead, then perhaps ping them each week and determine which have been off for x weeks, then remove them from AD. My script above could easily be amended to do that.

Your script looks way beyond me :p

I managed to do what I was trying to do though so dw :)
 
Still not making much sense why you want to do this.

If a computer is in a bin! You should know about it, hopefully users aren't just binning laptops!

If a user is off on 2+ weeks holiday your task will assume they are dead!

If its to do with DHCP addresses, just set a shorter lease.
 
The only reason I would see doing the script would be to see which computers are left on at night as judging by your AD name it looks like a school :D.
 
Sorry guys I forgot to check back to answer all your questions, I rarely venture into this part of the forum :p

The only reason I would see doing the script would be to see which computers are left on at night as judging by your AD name it looks like a school :D.

We have a script to turn off all computers so there is never a need to do this anyway. ;)

Still not making much sense why you want to do this.

If a computer is in a bin! You should know about it, hopefully users aren't just binning laptops!

If a user is off on 2+ weeks holiday your task will assume they are dead!

If its to do with DHCP addresses, just set a shorter lease.

In our cupboard there are 3 broken dell laptops which I immediately replaced with any old computer to ensure that the teachers could still have a computer on their desk; unfortunately I forgot to remove these computers from AD until now.

Ok, because I work in a school and all these laptops are required for the teacher to teach a lesson each and every laptop will have been turned on in the last 7days.

If a computer has not been turned on in 1 week it loses an IP address (these computers will be the ones in the cupboard) if a computer is off, it will still have an IP address but not reply when it is pinged giving a different message, and computers that are on will obviously reply when pinged.

So there are 3 different replies a PC can give:

- Computer replys and is therefore turned on
- Computer doesn't reply but still has IP address
- Computer doesn't reply and has no IP address (these are computers that are broken)

This means I can identify the names of the computers in the cupboard (The ones that don't reply and don't have an IP address) and delete them from AD.

I managed to do exactly what I wanted to do perfectly. :)

Edit: I know it works because I know how many dell laptops we have and how many in the cupboard are broken so if something was wrong I could obviously tell.

Does that now make sense?

Oh and one of you is a husband to a teacher here? She said something about this thread so hello to you :p
 
If I'm reading this right you're trying to ascertain which computer accounts may be dead? Try using the OLDcomp utility from Joewares site http://www.joeware.net/freetools/index.htm

Just be careful with it.
Good little app the OldComp, iirc it checks AD for the last sid update? It should update regularly so if it hasn't chances are the PC is gone. Very useful for me as I always forget to tidy up and some of our hardware can spend months on a desks not being used.
 
Back
Top Bottom