On various occasions, I had already tried to make sense out of directory services such as
LDAP
and Microsoft's
ADSI. Now, while that stuff is probably not rocket science, the
awkward terminology and syntax in this area have always managed to shy me away;
most of the time, there was another way to accomplish the same without
going through LDAP or ADSI, and so I went with that.
This time, the task was to retrieve the email address (in SMTP format) for a given
user. In my first attempt, I tried to tap the Outlook object model, but then figured
that a) there are a few systems in the local domain which do not have Outlook
installed and b) accessing Outlook's address info causes Outlook to display
warnings to the user reporting that somebody apparently is spelunking around
in data which they shouldn't be accessing. Which is probably a good idea, given
the overwhelming "success" of Outlook worms in the past, but not exactly helpful
in my case.
However, everybody here is connected to a Windows domain server and therefore has
access to its AD services, so that sounded like a more reliable approach.
I googled high and low, dissected funky scripts I found out there and put bits of
pieces of them together again to form this VBscript code:
user="Claus Brod"
context=GetObject("LDAP://rootDSE").Get("defaultNamingContext")
ou="OU=Users,"
Set objUser = GetObject("LDAP://CN=" & user & "," & ou & context)
WScript.Echo(objUser.mail)
groups=objUser.Get("memberOf")
For Each group in groups
WScript.Echo(" member of " & group)
Next
This works, but the
OU
part of the LDAP string (the "ADsPath") depends on the local
organizational structure and needs to be adapted for each particular environment;
I haven't found a good way to generalize this away. Hints most welcome.
PS: For those of you on a similar mission, Richard Mueller provides some helpful
scripts at
http://www.rlmueller.net/freecode3.htm.
to top