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

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

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
azstbzimms
Asked:
azstbzimms
1 Solution
 
Shift-3Commented:
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
 
azstbzimmsAuthor Commented:
Thanks for your accurate and prompt solution!
0

Featured Post

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now