Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - LDAP query list of users

Reply
 
Old 09-28-2009   #1 (permalink)
Wedders


 
 

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 SpecsSystem Spec
Old 09-28-2009   #2 (permalink)
Richard Mueller [MVP]


 
 

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.
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 SpecsSystem Spec
Old 09-30-2009   #3 (permalink)
Wedders


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46