Solved

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

Posted on 2008-06-16
2
3,133 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

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

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

A common practice in small networks is making file sharing easy which works extremely well when intra-network security is not an issue. In essence, everyone, that is "Everyone", is given access to all of the shared files - often the entire C: drive …
Many of us in IT utilize a combination of roaming profiles and folder redirection to ensure user information carries over from one workstation to another; in my environment, it was to enable virtualization without needing a separate desktop for each…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 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