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

Delete Old AD Computers with VBS

Hey all,
  I need a vb script that will delete a group of old computer accounts from Active Directory.  I currently have a csv file that has ONE COLUMN containing machine names of computers that I would like to delete.  So, I need a script that will reference that csv file and delete the matching account out of Active Directory.  Can anyone help?

Thanks,
Dan
0
dandmantra
Asked:
dandmantra
  • 8
  • 7
  • 2
  • +1
1 Solution
 
martin_babarikCommented:
Hello,

do you have DN names or just samid names of the computer accounts? Are this computers in one OU or in multiple OUs?
Martin
0
 
dandmantraAuthor Commented:
SAMID

They are in multiple OU's
0
 
ISWSIMBXCommented:
If you don't want to use a script, at my company we use this tool to disable and delete old computer accounts:

http://www.joeware.net/freetools/tools/oldcmp/index.htm
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
dandmantraAuthor Commented:
Thanks for the tip.  However, I need something that can reference my csv file.  I am familiar with joeware.net.

Dan
0
 
PakaCommented:
Here's the code:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23941287.html

Just change lines 1 & 3 to your domain and the CSV file name.  If you want it to run completely unattended, you might want to comment out lines 10 & 11.
0
 
dandmantraAuthor Commented:
I'm trying to get the script to work.  It is not finding any of the machine names.  I've changed the domain to mycompany.com and mycompany.  Neither seem to work.  Any ideas?
0
 
martin_babarikCommented:
On the line 1 you need to have just "domain", no DNS suffix.
Maybe try to add the "$" sign to the end of the computer name..? Not sure, the script doesn't seem to be the best I've ever seen.
0
 
martin_babarikCommented:
Sorry I think it's nonsense with the $ sign.
I'd like to help and post a script made for you, but am terribly busy. What kind of error do you have?
0
 
dandmantraAuthor Commented:
Well, it's not really returning an error.  It's just saying that it can't find the machine names, one by one.  I've changed line one to "mycompany" without the suffix and it still is not working.  It seems like it's not actually binding to my domain.
0
 
martin_babarikCommented:
Well as I examined the script very briefly I don't understand, how is it supposed to work by combining some netbios name and LDAP names...maybe it works, but never seen this.
You know what - if you are not in urgent pressure, give me few hours (24 at most) and I will write this script for you. It's not difficult, I just have milion things to do and don't know what to do first.
0
 
dandmantraAuthor Commented:
I certainly understand.  By looking at this other script, I'm not really sure how it would work at all either.  Maybe it's inexperience.  But, it doesn't seem to bind to the LDAP properly in order to compare the computer names.  

I wouldn't say that this is a huge rush.  It really is something that I want to get off my plate so that I can keep moving forward, though.  I certainly appreciate the attention.

Thanks!
0
 
PakaCommented:
I've been swamped too so I haven't had time to play with the script.  What does your csv look like?  Is it just a list of machine names like (or are there other characters in it):

machine1
machine2
machine3

If you can post a partial of your csv it would help.
0
 
martin_babarikCommented:
Well, here you go.
Just modify line no 6 to reflect the actual input file (now set to "C:\inputfile.txt")
Tested and working, let me know if it's fine for you.

Martin
Dim oNetwork, strUserName, strDom, aConnection, aCommand, aRecordSet, intPrimaryGroupID
Set oNetwork = CreateObject("Wscript.network")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set aConnection = CreateObject("ADODB.Connection")
Set aCommand = CreateObject("ADODB.Command")
Set oInputFile = oFSO.OpenTextFile("C:\inputfile.txt", 1)
Set oDomain = GetObject("LDAP://RootDSE")
strDom = oDomain.Get("DefaultNamingContext")
aConnection.Provider = "ADsDSOObject"
aConnection.Open
aCommand.ActiveConnection = aConnection
Do Until oInputFile.AtEndOfStream
	strComputerName = oInputFile.ReadLine & "$"
	WScript.Echo strComputerName
	aCommand.CommandText="<LDAP://" & strDom & ">;(&(objectCategory=computer)(samAccountName=" & strComputerName & "));distinguishedName,sAMAccountName;subTree"
	Set aResult = aCommand.Execute()
	strDN = aResult.Fields("distinguishedName") 
	WScript.Echo strDN
	Set objComputer = GetObject("LDAP://" & strDN)
	objComputer.DeleteObject (0)
Loop
Connection.Close

Open in new window

0
 
dandmantraAuthor Commented:
OK.  I haven't forgotten about you.  I've been caught up in some issues.  But have had a chance to try this script and see if I can get it to work for us.  Couple of problems.  First, the wscript.echo is popping up for every account that it's going to delete.  When you delete this line, the script doesn't work anymore.  When it reaches the end of the script, it returns an error

---------------------------
Windows Script Host
---------------------------
Script:   C:\Deletepcs.vbs
Line:      22
Char:     1
Error:     Object required: 'Connection'
Code:    800A01A8
Source:                 Microsoft VBScript runtime error

---------------------------
OK  
---------------------------


Any ideas?
0
 
martin_babarikCommented:
hello,

just run the script using cscript.exe (by default is wscript which pop ups the messages).
So open the command prompt and type: cscript scriptname.vbs
0
 
dandmantraAuthor Commented:
Still has a problem with line 22.
error.bmp
0
 
dandmantraAuthor Commented:
I figured it out.  By looking at your script, the active connection was called "aConnection".  So, the close connection statement needed to be changed to aConnection.Close.
0
 
martin_babarikCommented:
oops, sorry you're right. Good you figured it out yourself, I'd be searching for it few hours I guess:-)
0

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

  • 8
  • 7
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now