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