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
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