![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | LDAP query list of users Hi, I need to create a VBScript that can read a list of usernames or logonnames in a txt file then return the names as an AD LDAP query. I’ve been supplied with a list of random names that need their company field to be amended in AD, this is one of the fields that you can change for multiple user, using the standard AD tools. If anyone has an example script I can use it would be most appreciated. Thanks, Wedders. |
My System Specs![]() |
| | #2 (permalink) |
| | Re: LDAP query list of users Wedders wrote: Quote: > > I need to create a VBScript that can read a list of usernames or > logonnames > in a txt file then return the names as an AD LDAP query. > > I’ve been supplied with a list of random names that need their company > field > to be amended in AD, this is one of the fields that you can change for > multiple user, using the standard AD tools. > > If anyone has an example script I can use it would be most appreciated. of the user object. This is also called the NT name of the user, or in ADUC it is called the "pre-Windows 2000 logon name". You can use ADO to retrieve the Distinguished Name of the object that has a given sAMAccountName, but it would be more efficient to use the NameTranslate object. Also, ADO returns a read-only recordset, so you still need to bind to the objects anyway, so you might as well use NameTranslate. For information on NameTranslate, see this link: http://www.rlmueller.net/NameTranslateFAQ.htm A program to revise the company attribute, assume a hardcoded value for all users, could be similar to below (not tested): =========== Option Explicit Dim objFSO, strFile, objFile, objRootDSE, strDNSDomain Dim strNetBIOSDomain, objTrans, strNTName, strUserDN Dim strCompany Const ForReading = 1 ' Constants for NameTranslate. Const ADS_NAME_INITTYPE_GC = 3 Const ADS_NAME_TYPE_NT4 = 3 Const ADS_NAME_TYPE_1779 = 1 ' Specify value of company attribute. strCompany = "Ajax Inc." ' Specify the text file of user names. strFile = "c:\Scripts\UserList.txt" ' Open the file for read access. Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFile, ForReading) ' Determine DNS domain name from RootDSE object. Set objRootDSE = GetObject("LDAP://RootDSE") strDNSDomain = objRootDSE.Get("defaultNamingContext") ' Use the NameTranslate object to find the NetBIOS domain name ' from the DNS domain name. Set objTrans = CreateObject("NameTranslate") objTrans.Init ADS_NAME_INITTYPE_GC, "" objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4) ' Remove trailing backslash. strNetBIOSDomain = Left(strNetBIOSDomain, _ Len(strNetBIOSDomain) - 1) ' Read the file of user names. Do Until objFile.AtEndOfStream strNTName = Trim(objFile.ReadLine) ' Skip blank lines. If (strNTName <> "") Then ' Use NameTranslate to convert NT name to Distinguished Name. On Error Resume Next objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strNTName If (Err.Number <> 0) Then On Error GoTo 0 Wscript.Echo "User " & strNTName _ & " not found in Active Directory" Else On Error GoTo 0 strUserDN = objTrans.Get(ADS_NAME_TYPE_1779) ' Bind to user object. Set objUser = GetObject("LDAP://" & strUserDN) ' Only update company attribute if modified. If (objUser.company <> strCompany) Then ' Update company attribute. objUser.company = strCompany ' Save changes. objUser.SetInfo End If End If End If Loop ' Clean up. objFile.Close -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net -- |
My System Specs![]() |
| | #3 (permalink) |
| | Re: LDAP query list of users Thanks Richard works a treat! Just had to add dim objUser which was missing. Thanks again, Paul. "Richard Mueller [MVP]" wrote: Quote: > Wedders wrote: > Quote: > > > > I need to create a VBScript that can read a list of usernames or > > logonnames > > in a txt file then return the names as an AD LDAP query. > > > > I’ve been supplied with a list of random names that need their company > > field > > to be amended in AD, this is one of the fields that you can change for > > multiple user, using the standard AD tools. > > > > If anyone has an example script I can use it would be most appreciated. > I assume by username or logonname you mean the value of the sAMAccountName > of the user object. This is also called the NT name of the user, or in ADUC > it is called the "pre-Windows 2000 logon name". You can use ADO to retrieve > the Distinguished Name of the object that has a given sAMAccountName, but it > would be more efficient to use the NameTranslate object. Also, ADO returns a > read-only recordset, so you still need to bind to the objects anyway, so you > might as well use NameTranslate. For information on NameTranslate, see this > link: > > http://www.rlmueller.net/NameTranslateFAQ.htm > > A program to revise the company attribute, assume a hardcoded value for all > users, could be similar to below (not tested): > =========== > Option Explicit > > Dim objFSO, strFile, objFile, objRootDSE, strDNSDomain > Dim strNetBIOSDomain, objTrans, strNTName, strUserDN > Dim strCompany > > Const ForReading = 1 > ' Constants for NameTranslate. > Const ADS_NAME_INITTYPE_GC = 3 > Const ADS_NAME_TYPE_NT4 = 3 > Const ADS_NAME_TYPE_1779 = 1 > > ' Specify value of company attribute. > strCompany = "Ajax Inc." > > ' Specify the text file of user names. > strFile = "c:\Scripts\UserList.txt" > > ' Open the file for read access. > Set objFSO = CreateObject("Scripting.FileSystemObject") > Set objFile = objFSO.OpenTextFile(strFile, ForReading) > > ' Determine DNS domain name from RootDSE object. > Set objRootDSE = GetObject("LDAP://RootDSE") > strDNSDomain = objRootDSE.Get("defaultNamingContext") > > ' Use the NameTranslate object to find the NetBIOS domain name > ' from the DNS domain name. > Set objTrans = CreateObject("NameTranslate") > objTrans.Init ADS_NAME_INITTYPE_GC, "" > objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain > strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4) > ' Remove trailing backslash. > strNetBIOSDomain = Left(strNetBIOSDomain, _ > Len(strNetBIOSDomain) - 1) > > ' Read the file of user names. > Do Until objFile.AtEndOfStream > strNTName = Trim(objFile.ReadLine) > ' Skip blank lines. > If (strNTName <> "") Then > ' Use NameTranslate to convert NT name to Distinguished Name. > On Error Resume Next > objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strNTName > If (Err.Number <> 0) Then > On Error GoTo 0 > Wscript.Echo "User " & strNTName _ > & " not found in Active Directory" > Else > On Error GoTo 0 > strUserDN = objTrans.Get(ADS_NAME_TYPE_1779) > ' Bind to user object. > Set objUser = GetObject("LDAP://" & strUserDN) > ' Only update company attribute if modified. > If (objUser.company <> strCompany) Then > ' Update company attribute. > objUser.company = strCompany > ' Save changes. > objUser.SetInfo > End If > End If > End If > Loop > > ' Clean up. > objFile.Close > > -- > Richard Mueller > MVP Directory Services > Hilltop Lab - http://www.rlmueller.net > -- > > > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| working LDAP query, well almost.. 800A000D | VB Script | |||
| Query LDAP to get user Telephone | VB Script | |||
| How can I query LDAP in WLM? | Live Mail | |||
| Not all users are retrieved in an LDAP query via ASP | VB Script | |||
| help with LDAP query | PowerShell | |||