Solved

VBScript to Disable User AD Account Using sAMAccountName From .CSV File

Posted on 2008-06-16
2
3,123 Views
Last Modified: 2013-12-23
Looking for a VBscript that will disable a group of user accounts in a W2K3 AD from a list of sAMAccountNames in a .csv file.  
-The CSV file will only have 1 column with the first cell titled "sAMAccountName" and all of names will follow in cells A2, A3, A4...  
-The .CSV file would located on the root drive of a local workstation that the script would be ran from and be titled "disabledaccounts".
-Finally, if possible, on every account that gets disabled, populate a note within each user account on the ADDRESS Tab, in the STREET field, that says "Account Disabled--Not Validated."
Is this possible?
0
Comment
Question by:azstbzimms
2 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 350 total points
ID: 21796147
Paste the script below into a text file with a .vbs extension.  Customize the value of the strCSV variable with the location of the CSV file.  Customize the value of the strDomain variable with the Distinguished Name of your domain.

Running the script will disable each of the users from the file and set their street address attribute to "Account Disabled--Not Validated."


On Error Resume Next
 

Const ADS_SCOPE_SUBTREE = 2

Const ForReading = 1
 

strCSV = "c:\userlist.csv"

strDomain = "dc=yourdomain,dc=local"
 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile(strCSV, ForReading)
 

Do Until objFile.AtEndOfStream

    strLine = objFile.ReadLine

	arrLine = Split(strLine, ",")

	strAccount = arrLine(0)

	If LCase(strAccount) <> "samaccountname" Then	

		Set objConnection = CreateObject("ADODB.Connection")

		Set objCommand =   CreateObject("ADODB.Command")

		objConnection.Provider = "ADsDSOObject"

		objConnection.Open "Active Directory Provider"

		Set objCommand.ActiveConnection = objConnection
 

		objCommand.Properties("Page Size") = 1000

		objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
 

		objCommand.CommandText = "SELECT AdsPath FROM 'LDAP://" & strDomain & _

			"' WHERE objectCategory='user' AND samaccountname='" & strAccount & "'"  
 

		Set objRecordSet = objCommand.Execute
 

		objRecordSet.MoveFirst
 

		Do Until objRecordSet.EOF

			strUserPath = objRecordSet.Fields("AdsPath").Value

			Set objUser = GetObject(strUserPath)

			objUser.StreetAddress = "Account Disabled--Not Validated."

			objUser.AccountDisabled = True

			objUser.SetInfo

			objRecordSet.MoveNext

		Loop

	End If

Loop
 

objFile.Close

Open in new window

0
 

Author Closing Comment

by:azstbzimms
ID: 31467788
Thanks for your accurate and prompt solution!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

14 Experts available now in Live!

Get 1:1 Help Now