Delete Multiple AD Computer Accounts via VBScript w/logging

I have the following vbscript that works for a single PC deletion.

What I'd like to do, is expand it so it loops through a text file that I have (let's say c:\systemlist.txt) for all the pc's on that list.  And export the results (successful/unsuccesful) to a text file.

Would somebody be able to point me in the right direction for the script that's needed?

As always, thanks for helping.
strComputer = "####"
set objComputer = GetObject("LDAP://CN=" & strComputer & _
objComputer.DeleteObject (0)

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

usslindstromAuthor Commented:
*Oh - and if possible, I would need it to search all the AD OUs (as we have a ton), instead of just pointing it to a single container object*
This should do what you want.

Set oFS = CreateObject("Scripting.FileSystemObject")
Set oFile = oFS.OpenTextFile("c:\systemlist.txt",1)
Do while not oFile.AtEndOfStream
     ComputerName = oFile.ReadLine
     ComputerDN = GetObjectDN(computerName,Domain)
     If ComputerDN <> "?" Then 
          Set oComputer = GetObject("LDAP://" & ComputerDN)
          wscript.echo("Machine not found " & ComputerName)
     End If
Function GetObjectDN(strObject, strDomain)
	On Error Resume Next : Err.Clear
	Set objNameTranslate = CreateObject("NameTranslate")
	objNameTranslate.Init 3, ""
	objNameTranslate.Set 3, strdomain & "\" & strObject
	strObjectDN = objNameTranslate.Get(1)
	If Err.Number <> 0 Then
		strObjectDN = "?"
	End If
	Set objNameTranslate = Nothing
	On Error Goto 0
	GetObjectDN = strObjectDN
End Function

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
usslindstromAuthor Commented:
Thank you very much for your assistance.

But please forgive me, I can't get the script to work.

The script returns all PCs in the txt file as "not found" - so I took the time to practice on some of my debugging.  :)   - please forgive my very basic understanding of vbscript...

But I added this line:    wscript.echo(ComputerDN) to see what it would return, and the msgbox only returns a question mark.

Would you be able to help me get more out of this script when ya' get a chance?   Thanks again for helping.
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

? means the account was not found.

Did you change line 1 to YOUR domain name?
usslindstromAuthor Commented:
Yup - on changing the first line to my domain name.  - I was just testing with my little "debug" addition to the script.

Now, I could be completely wrong in how I put my domain name. . .  I just put the fqdn ( etc.   But does this need to be in the format "DC=domainname, DC=COM"?

On a serious note - thank you very much for taking your time to assist me.  It's greatly appreciated.
No, like I put in the code, you are only supposed to put the netbios name, not the FQDN..
usslindstromAuthor Commented:
I'm missing something fundamental here.

Sorry to be so bothersome.  I included some snapshots of "me at work" :) - but I think I'm still out of the loop on the mechanics involved.
Well, if you're going to scribble out all the relevant information, there's not much I can do to help..  LOL
usslindstromAuthor Commented:
sorry - that's from my work.   lol

Pretty much a mandatory thing if I post any questions about it.  - sure does make it hard to ask questions too.  lol

In this case, let's say my domain is ""  and the NETBIOS name is "DOMAINNAME"

Even if I put that in the script that was provided earlier, it can't locate any of the PCs on the list, however, when I search AD - they come up.
You're right, it does make it difficult.

I don't know what to tell you, the code is good, and I can't see what's going on, so...

Good luck,
usslindstromAuthor Commented:
Couldn't get much in the way of the NETBIOS resolution...  But need to thank ya' - in taking the time to help me out.

I can imagine that when I get the netbios resolution worked out, that this script here will be great.

It's much appreciated.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.