Group:  English: Windows Server ยป microsoft.public.windows.server.scripting
Thread: Enumerate uses in ad from text file

DotNetBag
.NET Development Newsgroups

HTVi
TV Discussion Newsgroups

Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
Rising Antivirus 2006

Enumerate uses in ad from text file
Rick <drummer10980[ at ]gmail.com> 09.07.2007 18:49:11
I have a list of users in a CSV file that I need to go through AD and
see if they have an email address. Is there a way to do this with a
script?

thanks
Rick

Re: Enumerate uses in ad from text file
"Richard Mueller [MVP]" <rlmueller-nospam[ at ]ameritech.nospam.net> 09.07.2007 20:18:33
Rick wrote:

[Quoted Text]
>I have a list of users in a CSV file that I need to go through AD and
> see if they have an email address. Is there a way to do this with a
> script?

Assuming the names are NT names of users (pre-Windows 2000 logon names), you
can use the FileSystemObject to read the file. You can use the NameTranslate
object to convert to Distinguished Names. Use the Split function to parse
comma delimited string into an array. Bind to each user object and retrieve
the value of the mail attribute:
=============
Option Explicit

Dim strFile, objFSO, objFile
Dim objRootDSE, strDNSDomain, strNetBIOSDomain, objTrans
Dim arrNames, strNTName, strUserDN, objUser, strEmail

' Constants for NameTranslate
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

Const ForReading = 1

' Specify file of user names.
strFile = "c:\Scripts\users.csv"

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

' Open file for read access.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, ForReading)

' Read contents of file into an array of comma delimited names.
arrNames = Split(objFile.ReadAll, ",")

' Close file.
objFile.Close

' Read NT names of users.
For Each strNTName In arrNames
' Remove any carriage returns.
strNTName = Replace(strNTName, vbCrLf, "")
' Specify NT Name of user. Trap error if user not found.
On Error Resume Next
objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strNTName
If (Err.Number = 0) Then
On Error GoTo 0
' Use Get method to retrieve Distinguished Name.
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
' Bind to user object.
Set objUser = GetObject("LDAP://" & strUserDN)
' Retrieve value of mail attribute.
strEmail = objUser.mail
If (strEmail = "") Then
Wscript.Echo "User " & strNTName & " has no email address"
Else
Wscript.Echo "User " & strNTName & " has email address " &
strEmail
End If
Else
On Error GoTo 0
Wscript.Echo "User " & strNTName & " not found"
End If
Next

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--


Re: Enumerate uses in ad from text file
Rick <drummer10980[ at ]gmail.com> 10.07.2007 11:58:23
On Jul 9, 4:18 pm, "Richard Mueller [MVP]" <rlmueller-
nos...[ at ]ameritech.nospam.net> wrote:
[Quoted Text]
> Rick wrote:
> >I have a list of users in a CSV file that I need to go through AD and
> > see if they have an email address. Is there a way to do this with a
> > script?
>
> Assuming the names are NT names of users (pre-Windows 2000 logon names), you
> can use the FileSystemObject to read the file. You can use the NameTranslate
> object to convert to Distinguished Names. Use the Split function to parse
> comma delimited string into an array. Bind to each user object and retrieve
> the value of the mail attribute:
> =============
> Option Explicit
>
> Dim strFile, objFSO, objFile
> Dim objRootDSE, strDNSDomain, strNetBIOSDomain, objTrans
> Dim arrNames, strNTName, strUserDN, objUser, strEmail
>
> ' Constants for NameTranslate
> Const ADS_NAME_INITTYPE_GC = 3
> Const ADS_NAME_TYPE_NT4 = 3
> Const ADS_NAME_TYPE_1779 = 1
>
> Const ForReading = 1
>
> ' Specify file of user names.
> strFile = "c:\Scripts\users.csv"
>
> ' 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)
>
> ' Open file for read access.
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.OpenTextFile(strFile, ForReading)
>
> ' Read contents of file into an array of comma delimited names.
> arrNames = Split(objFile.ReadAll, ",")
>
> ' Close file.
> objFile.Close
>
> ' Read NT names of users.
> For Each strNTName In arrNames
> ' Remove any carriage returns.
> strNTName = Replace(strNTName, vbCrLf, "")
> ' Specify NT Name of user. Trap error if user not found.
> On Error Resume Next
> objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strNTName
> If (Err.Number = 0) Then
> On Error GoTo 0
> ' Use Get method to retrieve Distinguished Name.
> strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
> ' Bind to user object.
> Set objUser = GetObject("LDAP://" & strUserDN)
> ' Retrieve value of mail attribute.
> strEmail = objUser.mail
> If (strEmail = "") Then
> Wscript.Echo "User " & strNTName & " has no email address"
> Else
> Wscript.Echo "User " & strNTName & " has email address " &
> strEmail
> End If
> Else
> On Error GoTo 0
> Wscript.Echo "User " & strNTName & " not found"
> End If
> Next
>
> --
> Richard Mueller
> Microsoft MVP Scripting and ADSI
> Hilltop Lab -http://www.rlmueller.net
> --

Richard,
thank you for the script, one thing though, my file has last name,
first name in each row. the script takes the last name from the first
row and uses that alone, then it takes the first name from the first
row and last name from the second row etc as a user name.

again , thanks for the response.
do you write any books on scripting?
Rick

Re: Enumerate uses in ad from text file
"Richard Mueller [MVP]" <rlmueller-nospam[ at ]ameritech.nospam.net> 10.07.2007 16:29:06

"Rick" <drummer10980[ at ]gmail.com> wrote in message
news:1184068703.440052.188780[ at ]k79g2000hse.googlegroups.com...
[Quoted Text]
> On Jul 9, 4:18 pm, "Richard Mueller [MVP]" <rlmueller-
> nos...[ at ]ameritech.nospam.net> wrote:
>> Rick wrote:
>> >I have a list of users in a CSV file that I need to go through AD and
>> > see if they have an email address. Is there a way to do this with a
>> > script?
>>
>> Assuming the names are NT names of users (pre-Windows 2000 logon names),
>> you
>> can use the FileSystemObject to read the file. You can use the
>> NameTranslate
>> object to convert to Distinguished Names. Use the Split function to parse
>> comma delimited string into an array. Bind to each user object and
>> retrieve
>> the value of the mail attribute:
>> =============
>> Option Explicit
>>
>> Dim strFile, objFSO, objFile
>> Dim objRootDSE, strDNSDomain, strNetBIOSDomain, objTrans
>> Dim arrNames, strNTName, strUserDN, objUser, strEmail
>>
>> ' Constants for NameTranslate
>> Const ADS_NAME_INITTYPE_GC = 3
>> Const ADS_NAME_TYPE_NT4 = 3
>> Const ADS_NAME_TYPE_1779 = 1
>>
>> Const ForReading = 1
>>
>> ' Specify file of user names.
>> strFile = "c:\Scripts\users.csv"
>>
>> ' 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)
>>
>> ' Open file for read access.
>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> Set objFile = objFSO.OpenTextFile(strFile, ForReading)
>>
>> ' Read contents of file into an array of comma delimited names.
>> arrNames = Split(objFile.ReadAll, ",")
>>
>> ' Close file.
>> objFile.Close
>>
>> ' Read NT names of users.
>> For Each strNTName In arrNames
>> ' Remove any carriage returns.
>> strNTName = Replace(strNTName, vbCrLf, "")
>> ' Specify NT Name of user. Trap error if user not found.
>> On Error Resume Next
>> objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strNTName
>> If (Err.Number = 0) Then
>> On Error GoTo 0
>> ' Use Get method to retrieve Distinguished Name.
>> strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
>> ' Bind to user object.
>> Set objUser = GetObject("LDAP://" & strUserDN)
>> ' Retrieve value of mail attribute.
>> strEmail = objUser.mail
>> If (strEmail = "") Then
>> Wscript.Echo "User " & strNTName & " has no email address"
>> Else
>> Wscript.Echo "User " & strNTName & " has email address " &
>> strEmail
>> End If
>> Else
>> On Error GoTo 0
>> Wscript.Echo "User " & strNTName & " not found"
>> End If
>> Next
>>
>> --
>> Richard Mueller
>> Microsoft MVP Scripting and ADSI
>> Hilltop Lab -http://www.rlmueller.net
>> --
>
> Richard,
> thank you for the script, one thing though, my file has last name,
> first name in each row. the script takes the last name from the first
> row and uses that alone, then it takes the first name from the first
> row and last name from the second row etc as a user name.
>
> again , thanks for the response.
> do you write any books on scripting?
> Rick
>

If the file has one name per line, then it is not comma delimited. The file
can be read one line at a time by using the ReadLine method of the file
object, instead of using ReadAll and the Split function. For example (in
brief):
==============
' Specify file of user names, on name per line.
strFile = "c:\Scripts\users.txt"

' Open file for read access.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, ForReading)

' Read file one line at a time.
Do Until objFile.AtEndOfStream
strName = Trim(objFile.ReadLine)
' Skip blank lines.
If (strName <> "") Then
' Do something with each name.
End If
Loop
objFile.Close
===========
However, from your description I cannot tell what form of name you have in
the file. It does not sound like the NT name (sAMAccountName, also called
the "pre-Windows 2000 logon name"). It sounds like it could be the Common
Name of the users (the value of the cn attribute). This makes it difficult
to identify the users, as the Common Name need not be unique in the domain.
If the file has Common Names, unless you know for sure that the values of cn
and sAMAccountName are always the same, you may need to use ADO to search AD
for the users, then decide what to do if you find more than one. If the file
has first and last names (as assigned in ADUC), it may be more difficult to
uniquely identify the users. You would use ADO to search AD for the users
that have the specified values assigned to the givenName (first name) and sn
(last name) attributes.

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--


Re: Enumerate uses in ad from text file
Rick <drummer10980[ at ]gmail.com> 10.07.2007 17:42:13
On Jul 10, 12:29 pm, "Richard Mueller [MVP]" <rlmueller-
nos...[ at ]ameritech.nospam.net> wrote:
[Quoted Text]
> "Rick" <drummer10...[ at ]gmail.com> wrote in message
>
> news:1184068703.440052.188780[ at ]k79g2000hse.googlegroups.com...
>
>
>
>
>
> > On Jul 9, 4:18 pm, "Richard Mueller [MVP]" <rlmueller-
> > nos...[ at ]ameritech.nospam.net> wrote:
> >> Rick wrote:
> >> >I have a list of users in a CSV file that I need to go through AD and
> >> > see if they have an email address. Is there a way to do this with a
> >> > script?
>
> >> Assuming the names are NT names of users (pre-Windows 2000 logon names),
> >> you
> >> can use the FileSystemObject to read the file. You can use the
> >> NameTranslate
> >> object to convert to Distinguished Names. Use the Split function to parse
> >> comma delimited string into an array. Bind to each user object and
> >> retrieve
> >> the value of the mail attribute:
> >> =============
> >> Option Explicit
>
> >> Dim strFile, objFSO, objFile
> >> Dim objRootDSE, strDNSDomain, strNetBIOSDomain, objTrans
> >> Dim arrNames, strNTName, strUserDN, objUser, strEmail
>
> >> ' Constants for NameTranslate
> >> Const ADS_NAME_INITTYPE_GC = 3
> >> Const ADS_NAME_TYPE_NT4 = 3
> >> Const ADS_NAME_TYPE_1779 = 1
>
> >> Const ForReading = 1
>
> >> ' Specify file of user names.
> >> strFile = "c:\Scripts\users.csv"
>
> >> ' 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)
>
> >> ' Open file for read access.
> >> Set objFSO = CreateObject("Scripting.FileSystemObject")
> >> Set objFile = objFSO.OpenTextFile(strFile, ForReading)
>
> >> ' Read contents of file into an array of comma delimited names.
> >> arrNames = Split(objFile.ReadAll, ",")
>
> >> ' Close file.
> >> objFile.Close
>
> >> ' Read NT names of users.
> >> For Each strNTName In arrNames
> >> ' Remove any carriage returns.
> >> strNTName = Replace(strNTName, vbCrLf, "")
> >> ' Specify NT Name of user. Trap error if user not found.
> >> On Error Resume Next
> >> objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strNTName
> >> If (Err.Number = 0) Then
> >> On Error GoTo 0
> >> ' Use Get method to retrieve Distinguished Name.
> >> strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
> >> ' Bind to user object.
> >> Set objUser = GetObject("LDAP://" & strUserDN)
> >> ' Retrieve value of mail attribute.
> >> strEmail = objUser.mail
> >> If (strEmail = "") Then
> >> Wscript.Echo "User " & strNTName & " has no email address"
> >> Else
> >> Wscript.Echo "User " & strNTName & " has email address " &
> >> strEmail
> >> End If
> >> Else
> >> On Error GoTo 0
> >> Wscript.Echo "User " & strNTName & " not found"
> >> End If
> >> Next
>
> >> --
> >> Richard Mueller
> >> Microsoft MVP Scripting and ADSI
> >> Hilltop Lab -http://www.rlmueller.net
> >> --
>
> > Richard,
> > thank you for the script, one thing though, my file has last name,
> > first name in each row. the script takes the last name from the first
> > row and uses that alone, then it takes the first name from the first
> > row and last name from the second row etc as a user name.
>
> > again , thanks for the response.
> > do you write any books on scripting?
> > Rick
>
> If the file has one name per line, then it is not comma delimited. The file
> can be read one line at a time by using the ReadLine method of the file
> object, instead of using ReadAll and the Split function. For example (in
> brief):
> ==============
> ' Specify file of user names, on name per line.
> strFile = "c:\Scripts\users.txt"
>
> ' Open file for read access.
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.OpenTextFile(strFile, ForReading)
>
> ' Read file one line at a time.
> Do Until objFile.AtEndOfStream
> strName = Trim(objFile.ReadLine)
> ' Skip blank lines.
> If (strName <> "") Then
> ' Do something with each name.
> End If
> Loop
> objFile.Close
> ===========
> However, from your description I cannot tell what form of name you have in
> the file. It does not sound like the NT name (sAMAccountName, also called
> the "pre-Windows 2000 logon name"). It sounds like it could be the Common
> Name of the users (the value of the cn attribute). This makes it difficult
> to identify the users, as the Common Name need not be unique in the domain.
> If the file has Common Names, unless you know for sure that the values of cn
> and sAMAccountName are always the same, you may need to use ADO to search AD
> for the users, then decide what to do if you find more than one. If the file
> has first and last names (as assigned in ADUC), it may be more difficult to
> uniquely identify the users. You would use ADO to search AD for the users
> that have the specified values assigned to the givenName (first name) and sn
> (last name) attributes.
>
> --
> Richard Mueller
> Microsoft MVP Scripting and ADSI
> Hilltop Lab -http://www.rlmueller.net
> --- Hide quoted text -
>
> - Show quoted text -

thanks Richard,
my CSV file contains first name, last name in seperate cells. I will
try the solution above.

Rick

Re: Enumerate uses in ad from text file
"Richard Mueller [MVP]" <rlmueller-nospam[ at ]ameritech.nospam.net> 10.07.2007 18:56:14
[Quoted Text]
>
>> If the file has one name per line, then it is not comma delimited. The
>> file
>> can be read one line at a time by using the ReadLine method of the file
>> object, instead of using ReadAll and the Split function. For example (in
>> brief):
>> ==============
>> ' Specify file of user names, on name per line.
>> strFile = "c:\Scripts\users.txt"
>>
>> ' Open file for read access.
>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> Set objFile = objFSO.OpenTextFile(strFile, ForReading)
>>
>> ' Read file one line at a time.
>> Do Until objFile.AtEndOfStream
>> strName = Trim(objFile.ReadLine)
>> ' Skip blank lines.
>> If (strName <> "") Then
>> ' Do something with each name.
>> End If
>> Loop
>> objFile.Close
>> ===========
>> However, from your description I cannot tell what form of name you have
>> in
>> the file. It does not sound like the NT name (sAMAccountName, also called
>> the "pre-Windows 2000 logon name"). It sounds like it could be the Common
>> Name of the users (the value of the cn attribute). This makes it
>> difficult
>> to identify the users, as the Common Name need not be unique in the
>> domain.
>> If the file has Common Names, unless you know for sure that the values of
>> cn
>> and sAMAccountName are always the same, you may need to use ADO to search
>> AD
>> for the users, then decide what to do if you find more than one. If the
>> file
>> has first and last names (as assigned in ADUC), it may be more difficult
>> to
>> uniquely identify the users. You would use ADO to search AD for the users
>> that have the specified values assigned to the givenName (first name) and
>> sn
>> (last name) attributes.
>>
>> --
>> Richard Mueller
>> Microsoft MVP Scripting and ADSI
>> Hilltop Lab -http://www.rlmueller.net
>> --- Hide quoted text -
>>
>> - Show quoted text -
>
> thanks Richard,
> my CSV file contains first name, last name in seperate cells. I will
> try the solution above.
>
> Rick
>

Is each line the Common Name of a user?
Or, is each line the value of the FirstName, then a comma, then the
LastName? If so, do these values correspond to the fields in ADUC labeled
"First name:" and "Last name:"?

In either case, this will be difficult. It involves using ADO to search AD
for the user objects having these values. It can be done, but involves
repeated searches, and you may find one match, no matches, or more than one
match for each search.

It would be better if you had a file with either the NT names or the
Distinguished Names.

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--


Home | Search | Terms | Imprint | Contact
Newsgroups Reader - provided by WiredBox.Net