• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 887
  • Last Modified:

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

  • 6
  • 5
1 Solution
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

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.
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

? 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 (domainname.com) 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 "domainname.com"  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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now