Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable Users from text File.

Status
Not open for further replies.

Wazz

Technical User
Aug 12, 2002
209
GB
I am trying to write a script that will delete user accounst. I know this has been done a million times before which is adding the frustration I am currently feeling. Please help!

I have a list of 300+ user accounts that need to be disabled and I have these names in a text file in the format first name[space]surname. I have a used a script I found and altered it to fit what I want, however, the name of my user is never found.

I have added wscript.echo objCommand.CommandText into the script and I am sure the format of this is not correct and this could be where my problem lies. I know this script doesnt actually disable anything yet, I havent done that bit.. IF i cant even find one username out of 300 I didnt see much poibt of carrying on!

Thanks
Wazz

Code:
'on error resume next
'Option explicit

Dim strOutfile, strSDesk, strDisOu, oshell, strUserName, strChange
Dim objRootDSE, DNSDomain, objConnection, objCommand, objRecordset
Dim strADsPath, strUserInfo, strMemberOf, strFileName, rdns
Dim strOUs, strService, strNewOu, objOUobj

' Read Usernames from Text File
Const ForReading = 1
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("UserAccountsToDisable.txt", ForReading)
Do Until objTextFile.AtEndOfStream
strUserName = objTextFile.Readline
arrTargets = Split(strUserName , ",")
For Each strUserName in arrTargets

' Determine the DNS domain from the RootDSE object. 

Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")
Set objDomain = GetObject("LDAP://" & strDNSDomain)
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

wscript.echo strUserName

objCommand.CommandText = "<LDAP://" & strDNSDomain & ">;" & "(&(objectCategory=User)(cn=" & strUserName &"))" & ";ADsPath;subtree"

Wscript.echo strDNSDomain
wscript.echo objCommand.CommandText

'get a list of users with this name' 

Set objRecordset = objCommand.Execute
If objrecordset.recordcount = 0 then

'If account not found write to text file
Set objLogFile = objFSO.OpenTextFile _
("UserNameNotFound.txt", ForAppending, True)
objLogFile.WriteLine strUserName

end if
 
what does the

wscript.echo strUserName

spit out? I am sure this is not in the right format, and even then the user objects in AD, their 'cn', what format are they in? how do they compare to your list of First LastNames?
 
You know... I wrote you a whole long reply and then re-read what you had written. I have it on the text file as firstname.surname where it should be firstname[space]surname.

I will probably get stuck on the next few lines but till then.. Thanks!!!!
 
I only have a few more lines to write but even they have beaten me!

After the last end if if the above code I have added the code below. I cant seem to make the examples I have seen fit the script I have. Im sure I know where my error is but its beaten me.

Im sure the line thas wrong is set objuser = GetObject("LDAP://" strDNSDomain ) The strDNSDomain is the path to the user account... How do I fit this all in? The examples all use GetObject but I have already got it!

Thanks
Wazz

Code:
Const ADS_UF_ACCOUNTDISABLE = 2

set objuser = GetObject("LDAP://"  strDNSDomain )
intUAC = objUser.Get("userAccountControl")

     objUser.Put "userAccountControl", intUAC OR ADS_UF_ACCOUNTDISABLE
objUser.SetInfo
 
'this line here is wrong.
set objuser = GetObject("LDAP://" strDNSDomain )

you need to the ADsPath property of the user account in question.
your objRecordset recordset should contain a record with a field of ADsPath, you can use this as the bind string and replace the strDNSDomain
 
I entered a wscript.echo and realised teh path for the variable ADsPath was correct so I added that to teh line you mention above and it worked.

Thank you very much mrmovie!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top