Solved

How do I delete user account from AD using vbscript and a spreadsheet with the accounts to remove

Posted on 2008-06-19
5
1,842 Views
Last Modified: 2008-10-28
I need a vbscript that will look at a spreadsheet and then delete those accounts from Active Directory based on the user logon.
0
Comment
Question by:WNottsC
  • 3
5 Comments
 
LVL 24

Accepted Solution

by:
purplepomegranite earned 500 total points
Comment Utility
The attached should do what you want.

I have tested the Excel code part (just change the variables at the beginning of the script for your needs), but I have been unable to check the AD part due to not being on a domain right now (or having access to a lab domain), so this part needs testing.

It should work - it was taken from another script to delete user accounts that worked.  Let me know!
strSourceWorkbook="E:\ee\AD\Delete AD Accounts from Excel List\test.xls" ' The workbook containing the account list

strSourceSheet="Sheet1" ' Set this to the name of the sheet containing the username list

rngUsername="B2" ' Set this to the first cell containing username in Excel
 

On Error Resume Next

Const ADS_PROPERTY_DELETE = 4

Const E_ADS_PROPERTY_NOT_FOUND  = &h8000500D
 

' Initialise Excel

set oExcel=CreateObject("excel.application")

if Err.Number<>0 then

	Err.Clear

	WScript.Echo "Failed to open Excel"

	WScript.Quit

end if

oExcel.Visible=false

oExcel.DisplayAlerts=false ' Don't show any dialog box prompts
 

' Initialise connection

set adoCon = CreateObject("ADODB.Connection")

set adoCmd =   CreateObject("ADODB.Command")

adoCon.Provider = "ADsDSOObject"

adoCon.Open "Active Directory Provider"

set adoCmd.ActiveConnection = adoCon

adoCmd.Properties("Page Size") = 1000

adoCmd.Properties("Timeout") = 30

adoCmd.Properties("Cache Results") = False
 

' Obtain usernames from Excel file

set srcWorkbook=oExcel.Workbooks.Open(strSourceWorkBook)

if Err.Number=0 then

	i=0

	while not srcWorkbook.Sheets(strSourceSheet).Range(rngUsername).Offset(i).Value=""

		strUsername=srcWorkbook.Sheets(strSourceSheet).Range(rngUsername).Offset(i).Value

		wsclript.echo "Deleting user " & strUserName

		DeleteUser strUsername

		i=i+1

	wend

	srcWorkbook.Close

else

	wscript.echo "Couldn't open workbook: " & Err.Description

	Err.Clear

end if

oExcel.Quit
 

' Close connection

adoRec.Close

set adoRec = Nothing

adoCon.Close

set adoCon = Nothing
 
 

sub DeleteUser(strUsername)

	set objRootDSE = GetObject("LDAP://RootDSE")

	strDNSDomain = objRootDSE.Get("defaultNamingContext")

	strFilter = "(&(SAMAccountType=805306368)(SAMAccountName=" & strUsername & "))"

	strFields = "distinguishedName, ADsPath"
 

	strQuery = "<LDAP://" & strDNSDomain & ">;" & strFilter & ";" & strFields & ";subtree"

	adoCmd.CommandText = strQuery

	set adoRec = adoCmd.Execute

	while not adoRec.EOF

	      set objUser = GetObject(adoRec.Fields("ADsPath").Value)

			wscript.echo "Deleting " & objRecordSet.Fields("distinguishedName").Value

			Set objUser = GetObject("LDAP://" & objRecordSet.Fields("distinguishedName").Value)

			objUser.Delete "user", "distinguishedName=" & objRecordSet.Fields("distinguishedName").Value

	      adoRec.MoveNext

	wend

end sub

Open in new window

0
 
LVL 24

Expert Comment

by:purplepomegranite
Comment Utility
Thanks for the points, but why the B-grade?

http://www.experts-exchange.com/help.jsp#hi97
0
 

Author Comment

by:WNottsC
Comment Utility
The solution was very good but I did have to provide a few changes in order to get this fully functioning.
0
 
LVL 24

Expert Comment

by:purplepomegranite
Comment Utility
Thanks for clarifying.  Would it be possible to post the changes so that this question has a complete solution for anyone else looking for a similar function?  And also for my reference, of course! :-)
0
 
LVL 6

Expert Comment

by:Ahmed Abdel Salam
Comment Utility
I am looking for same request .. this script is not working

any update on this ??
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now