Solved

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

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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…
We recently endured a series of broadcast storms that caused our ISP to shut us down for brief periods of time. After going through a multitude of tests, we determined that the issue was related to Intel NIC drivers on some new HP desktop computers …
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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