This blog is by design...

Tuesday, November 22, 2005

Enumerate AD group using NT Name and NameTranslate Method

This is a revision on my earlier post today. It was noted that
the first post didn't pick up contacts. I revised this one so it
will pick up any object in the dist list.

'***************************************************
'The constant below should reflect the name of the group
'you wish to enumerate. For a Distribution group, you must
'use the 'alias' name rather than the display name of the group
'See ADUsers and computers to find the alias name of a dist group

Const strGroup = "ITNET"

'This will create a file in a folder called c:\reports. Either create
'the folder or change the hardcoded path down below
'***************************************************
On Error Resume Next
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_USER_PRINCIPAL_NAME = 9
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1 'Distinguished Name

Set objRootDSE = GetObject("LDAP://rootDSE")
Set objDomainRoot = GetObject("LDAP://"& _
objRootDSE.Get("defaultNamingContext"))
strDomain = Right(objDomainRoot.Name, Len(objDomainRoot.Name)-3)
'wscript.Echo strDomain

Set filesys = CreateObject("Scripting.FileSystemObject")
Set memberfile = filesys.CreateTextFile("c:\Reports\"& _
strGroup &".txt", True)

Set objTrans = CreateObject("NameTranslate")

'********************
'translate NT group name into AD DistinguishedName

objTrans.Init ADS_NAME_TYPE_1779, strDomain
objTrans.Set ADS_NAME_TYPE_NT4, strDomain & "\" & strGroup
strGroupDN = objTrans.Get(ADS_NAME_TYPE_1779)
'wscript.Echo strGroupDN
'*********************

set objGroup = GetObject("LDAP://"& strGroupDN)
'wscript.Echo objGroup.displayName

arrMember = objGroup.GetEx("member")
For Each User in arrMember
'WScript.Echo vbTab & User

set objUser = GetObject("LDAP://"& User)
set objProxy = GetObject("LDAP://"& objUser.distinguishedName)

For Each proxyAddresses in objProxy.GetEx("proxyAddresses")
If Instr(proxyAddresses, "SMTP:") then
strEmail = Right(proxyAddresses, Len(proxyAddresses)-5)
End If
Next

memberfile.Writeline objUser.mailNickName & "," & strEmail
Next
memberfile.close

set objRootDSE = Nothing
set objDomainRoot = Nothing
set filesys = Nothing
set memberfile = Nothing
set objTrans = Nothing
set objGroup = Nothing
set userObj = Nothing
set objProxy = Nothing

wscript.Echo "All Done"

1 Comments:

  • Kris,

    Thanks for the script. I got it to work... mostly. It did, in fact, export the user/e-mail address, however, it pulled the distribution list aliases, and their associated aliased e-mail address, rather than the actual user and e-mail for that particular user. This is sort of the problem I was originally trying to avoid. Any help you could offer on how to tweak the script would be greatly appreciated. Thanks again!

    J. Gohil

    p.s. I accepted the answer because the script does work fine, I just figure it needs "tweaking".

    By Anonymous J. Gohil, at 11:15 AM  

Post a Comment

<< Home