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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VBscript to change ALL user logon name via LDAP

Status
Not open for further replies.

MadMage

MIS
May 23, 2003
3
US
I need to change all of the "user logon name" fields (not the pre-2000 field) for the 4k users on my native Win2K domain. I am forced to use VBscript to do this for several esoteric reasons (main one being cause corporate HQ said that is all I am allowed to use) to match the list of user id's I have in an excel file. The excel file has two columns, one listing the employee ID number and the other the new standard for the "user logon name" field. It is assumed that over 50% of the current id's are already correct, but regardless of correct or not, we want to correlate via the employee ID number and then when matched, rename the current "user logon name" with the one contained on the excel spreadsheet. One major problem, my knowledge of VBscript is woefully thin, mostly real basic scripts I have dl'ed from the online world and made minor adjustments to. I am buying the O'Reilly Active Directory Cookbook and Windows 2000 Scripting Bible this weekend. If anyone could suggest or help me out, I would be most appreciative.
 
so, what is the link between the spreadsheet and AD, you said you have employeeID and newAccountName in the spreadsheet, what field in AD matches with one of these?
 
Dim WshShell
Dim WshNetwork
Dim oCommand
Dim oConnection
Dim oRS
Dim aArray
Dim i
Dim strSN
Dim strGN


Const ADS_RIGHT_DS_CREATE_CHILD = &H1
Const ADS_RIGHT_DS_DELETE_CHILD = &H2
Const ADS_RIGHT_ACTRL_DS_LIST = &H4
Const ADS_RIGHT_DS_SELF = &H8
Const ADS_RIGHT_DS_READ_PROP = &H10
Const ADS_RIGHT_DS_WRITE_PROP = &H20
Const ADS_RIGHT_DS_DELETE_TREE = &H40
Const ADS_RIGHT_DS_LIST_OBJECT = &H80
Const ADS_RIGHT_DS_CONTROL_ACCESS = &H100
Const ADS_RIGHT_DELETE = &H10000
Const ADS_RIGHT_READ_CONTROL = &H20000
Const ADS_RIGHT_WRITE_DAC = &H40000
Const ADS_RIGHT_WRITE_OWNER = &H80000
Const ADS_RIGHT_SYNCHRONIZE = &H100000
Const ADS_RIGHT_ACCESS_SYSTEM_SECURITY = &H1000000
Const ADS_RIGHT_GENERIC_ALL = &H10000000
Const ADS_RIGHT_GENERIC_EXECUTE = &H20000000
Const ADS_RIGHT_GENERIC_WRITE = &H40000000
Const ADS_RIGHT_GENERIC_READ = &H80000000

strInputfile = "d:\braupdateusers.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objTextStream1 = FSO.OpenTextFile(strInputfile, 1, False)
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oCommand = CreateObject("ADODB.Command")
Set dicUsers = Wscript.CreateObject("Scripting.Dictionary")

Do While Not objTextStream1.AtEndOfStream
sLine = LCase(Trim(objTextStream1.ReadLine))
If sLine <> &quot;&quot; Then
If Not dicUsers.Exists(sLine) Then
dicUsers.Add sLine, &quot;1&quot;
End If
End If
Loop

objTextStream1.Close
Set objTextStream1 = Nothing

'################################################################
'##### GET A RECORDSET BACK FROM ACTIVE DIRECTORY OF THE DNSNODES
'#setup ADODB Command Object, setup ADODB Connection object ####
'################################################################
Set oConnection = CreateObject(&quot;ADODB.Connection&quot;) '#
oConnection.Provider = &quot;ADsDSOObject&quot; '#
oConnection.Open = &quot;Active Directory Provider&quot; '#
oCommand.ActiveConnection = oConnection '#then Create a connection.
Set oRoot = GetObject(&quot;LDAP://RootDSE&quot;) '#
oCommand.Properties(&quot;Page Size&quot;) = 999
sdnsDomain = &quot;dc=blaa,dc=net&quot;
sQuery = &quot;SELECT scriptPath, sAMAccountName, userAccountControl FROM 'LDAP://ou=Users,ou=UK-Dunkirk,DC=blaa,DC=net' WHERE objectClass = 'person' AND sAMAccountName='bra*'&quot;
oCommand.commandtext = sQuery 'set the command text
oCommand.Properties(&quot;Page Size&quot;) = 999 'set the page size to allow for more than a 1000 records
Set oRs = oCommand.execute
While Not oRS.EOF
If Right(CStr(oRS.Fields(&quot;sAMAccountName&quot;)), 1) <> &quot;$&quot; AND oRS.Fields(&quot;userAccountControl&quot;) <> &quot;514&quot; AND oRS.Fields(&quot;userAccountControl&quot;) <> &quot;66050&quot; Then
If dicUsers.Exists(LCase(CStr(oRS.Fields(&quot;sAMAccountName&quot;)))) Then
'msgbox &quot;have found &quot; & CStr(oRS.Fields(&quot;sAMAccountName&quot;))
If IsNull(oRS.Fields(&quot;scriptPath&quot;)) Then
Call updateScriptPAth(lcase(oRS.Fields(&quot;sAMAccountName&quot;)))
Else
If Trim(LCase(CStr(oRS.Fields(&quot;scriptPath&quot;)))) <> &quot;fsc\loginloader.bat&quot; Then
Call updateScriptPAth(lcase(oRS.Fields(&quot;sAMAccountName&quot;)))
End If
End If
End If
End If
oRS.MoveNext
Wend


Set oRS = Nothing
Set oConnection = Nothing
Set oCommand = Nothing
Set WshNetwork = Nothing
Set WshShell = Nothing
Wscript.Quit


Sub updateScriptPath(strSAMName)

Set User = GetObject(&quot;WinNT://DOMFSC01/&quot; & strSAMName & &quot;,user&quot;)
User.LoginScript = &quot;fsc\loginloader.bat&quot;
User.SetInfo
Set User = Nothing

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top