Querying for the Computer Name Before Imaging

Querying for the Computer Name Before Imaging

As I was working on our hardware independent imaging, it occurred to me that checking Active Directory for the existence of the computername that I was about to image would be pretty cool. We use WinPE 2.1 on DS 6.9, so my first thought was to pilfer someone else's vbscript (in I/T, plagiarism is the sincerest form of flattery) and modify it for my needs. The script worked well enough on my laptop, so off to a deployment task it went. And failed. I beat my head upon this problem for awhile, and called tech support. Apparently, ldap binds do not work with WinPE 2.1, so I couldn't query Active Directory with vbscript (Thanks anyway, Nate!). "How about dsquery?" I asked myself. It doesn't work from WinPE 2.1 either. What was I to do?

Batch file programming! I created a Run Script task, set to execute locally from the server when the agent is connected:

REM Does computer exist in AD?

@echo off
if exist .\temp\%NAME%q.txt del /f /q .\temp\%NAME%q.txt

for /f %%a in ('dsquery server -name *dc* -o rdn') do (
	for /f %%b in ('dsquery computer -name %NAME% -server %%a ^| find /i /c "dc=com"') do (
		if %%b==1 echo %%a >> .\temp\%NAME%q.txt
		)
	)

All of our domain controllers have 'dc' in the name, so I queried AD for all servers named *dc* and output the name only. Then, I fed this into the query for computer name, counting the number of times each domain controller had come back with a hit. I echoed the domain controller's name into a text file, which I then displayed with another run script task in vbscript:

'vbscript Output from file

set WshShell = WScript.CreateObject("WScript.Shell")

set objFSO=CreateObject("Scripting.FileSystemObject")
set objTextFile=objFSO.OpenTextFile(".\temp\%NAME%q.txt",1)
Do Until objTextFile.AtEndOfStream
strComputer=objTextFile.Readall
Continue=WshShell.Popup ("Computer name found on: " & Chr(13) & Chr(10) & strComputer,0,"Attention!",0)

Loop

objTextFile.Close

objFSO.DeleteFile (".\temp\%NAME%q.txt"), true

Part of the beauty of this is that if no domain controllers think that the computer exists, no window will be displayed. If the computer does exist, the window will display only the domain controllers that had a copy of the computer object.

I have these tasks execute before the image goes to the pc, but I guess anytime before mini-setup would work. I would not recommend modifying the task to delete the computer object if it exists. I had considered this option, and felt that if one of my pc techs decided to name a pc with the same name as a domain controller, it would ruin my day.

Enjoy!

Mike

3.785715
Average: 3.8 (28 votes)
Syndicate content