How Can I Determine the ADsPath for the Logged-On User?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! In my HTA, how can I determine the ADsPath for the logged-on user?

— AC

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AC. You know, when you work at Microsoft people automatically assume that you spend your days in an ivory tower, totally removed from the day-to-day activities and needs of system administrators. To be honest, that isn’t true: our tower is really more concrete and steel than it is ivory.

Well, except for Dean’s office.

As for being totally removed from the day-to-day activities and needs of system administrators, well, there might be an element of truth in that. For example, the Scripting Guys spend a lot of time writing about scripts and answering questions about scripts, but we rarely find ourselves sitting down to write a script that we actually use as part of our job. That’s not necessarily a good thing, but it is the nature of the technical writing business. To paraphrase the old saying, those who can’t – or at least those who don’t – teach.

Every now and then, however, we do get called upon to write a real script. One of those occasions arose just a week or two ago and, by happy coincidence, we were asked if we could do the very same thing you’re asking: determine the ADsPath for the logged-on user. As it turned out, we could, and we quickly put together a script to do so. And now we’ll let everyone else in on the secret.

Let’s start off by showing you how to do this in a plain old VBScript script, and then we’ll show you a simple way to embed the code in an HTA. Here’s a script that reports back the ADsPath for the logged-on user:

On Error Resume Next

Set objSysInfo = CreateObject(“ADSystemInfo”)

strUser = objSysInfo.UserName Set objUser = GetObject(“LDAP://” & strUser)

Wscript.Echo objUser.AdsPath

Note. Well, sure, it is pretty simple. But, remember, we didn’t say we worked hard on this real-life script, we just said we actually worked on a real-life script.

The script starts out by creating an instance of the ADSystemInfo object:

Set objSysInfo = CreateObject(“ADSystemInfo”)

Incidentally, this is an incredibly useful object, one that can return all sorts of information about the logged-on user and the local computer. (You might take a look at this script to get an idea of the information that can be returned using ADSystemInfo.) The one drawback to this object is the fact that it can only be created locally: you can’t create an instance of ADSystemInfo on a remote computer and then get information about the user logged on to that machine. And, yes, that would be very cool. But it can’t be done.

One of the pieces of information that ADSystemInfo returns is the UserName. The UserName is actually the distinguished name of the logged-on user; that’s going to look something like this:

CN=Ken Myer,OU=Finance,dc=fabrikam,dc=com

At this point we can actually construct the ADsPath ourselves; after all, the ADsPath is simply the LDAP: provider plus the distinguished name:

LDAP://CN=Ken Myer,OU=Finance,dc=fabrikam,dc=com

However, we wanted to show you a more generic way to use ADSystemInfo, so we decided to go ahead and bind to the Ken Myer user account using these two lines of code:

strUser = objSysInfo.UserName
Set objUser = GetObject(“LDAP://” & strUser)

As you can see, all we do is grab the value of the UserName property and store it in a variable named strUser, then use that variable as part of the GetObject call that binds us to the user account.

From there we simply echo back the value of the ADsPath attribute; seeing as how we’re connected to the Ken Myer user account, though, we could just as easily echo back the value of any (or all) of Ken’s account attributes.

That is easy, isn’t it?

Now, what about using this code in an HTA? Well, here’s a very simple example of that:

<html>
<head>
    <title>ADSystemInfo Sample</title>
</head>

<SCRIPT LANGUAGE=”VBScript”>

Sub Window_OnLoad On Error Resume Next

Set objSysInfo = CreateObject(“ADSystemInfo”)

strUser = objSysInfo.UserName Set objUser = GetObject(“LDAP://” & strUser)

DataArea.InnerHTML = objUser.AdsPath End Sub

</SCRIPT>

<body>

<span id=DataArea></span>

</body> </html>

This HTA contains only a single element: a <span> tag named DataArea. When the HTA starts up, it runs a subroutine named Window_Onload. (Any subroutine with that name automatically runs any time an HTA is started or refreshed.) The code for the Window_Onload subroutine should look familiar:

On Error Resume Next

Set objSysInfo = CreateObject(“ADSystemInfo”)

strUser = objSysInfo.UserName Set objUser = GetObject(“LDAP://” & strUser)

DataArea.InnerHTML = objUser.AdsPath

That’s right: it’s the script we just showed you, but with one exception: instead of using Wscript.Echo to echo back the AdsPath for the logged-on user we use this line of code to write the AdsPath to the <span> tag:

DataArea.InnerHTML = objUser.AdsPath

Now, does that look like the sort of thing someone sitting in an ivory tower could write?

Well, OK, now that you mention it ….

0 comments

Discussion is closed.

Feedback usabilla icon