Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-06-16
2
Medium Priority
?
3,167 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 1400 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Enterprise networks where VoIP phones have been deployed frequently use port configurations that allow both a computer and an IP phone to be plugged into the same switch port but use different VLANs. On Cisco equipment I'm referring to the "native V…
I'm a big fan of Windows' offline folder caching and have used it on my laptops for over a decade.  One thing I don't like about it, however, is how difficult Microsoft has made it for the cache to be moved out of the Windows folder.  Here's how to …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

610 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