How Can I Map a Drive Based on a User’s Name and OU?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I need to create a logon script that maps a network drive based on the logged-on user’s user name and OU. How can I do that?

— KB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, KB. You know, KB, this is probably the toughest day of the year for the Scripting Guy who writes this column. For the past four days – Thursday, Friday, Saturday, and Sunday – he’s done nothing but watch college basketball. And not only has he seen some incredible games (Ohio State vs. Xavier and Washington State vs. Vanderbilt, to name just two) but he also got to witness both Duke and Arizona being eliminated in the opening round. For awhile there, he assumed that he must have died and gone to heaven.

Note. In case you’re wondering, most theologians agree that heaven would not be heaven if either the Duke or Arizona basketball teams were there. The same is true of Oregon, although that goes without saying.

But all his fun came to a crashing halt come Monday morning and time to go back to work.

That’s why this is such a tough day for the Scripting Guy who writes this column; as difficult as this is to believe, he doesn’t find work anywhere near as interesting and exciting as he finds college basketball. Of course, you’re probably thinking, “We understand how he feels. On the other hand, the Scripting Guy who writes that column is a true professional: somehow he’ll reach down deep inside and find a way to do his work even though he’d much rather be sitting at home watching even more college basketball. Are we right or are we right?”

Well, to be honest, you’re wrong: the Scripting Guy who writes this column is anything but a true professional. On the other hand, he does have a mortgage payment due at the end of the month. With that in mind, here’s a logon script that can map a network drive based on the logged-on user’s user name and OU:

Set objSysInfo = CreateObject(“ADSystemInfo”)

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

strUserName = objUser.samAccountName

strOUPath = objUser.Parent arrContainers = Split(strOUPath, “,”) arrOU = Split(arrContainers(0), “=”) strOU = arrOU(1)

strDrive = “\\Mission\Apps\Timesheets\” & strOU & “\” & strUserName

Set objNetwork = CreateObject(“Wscript.Network”) objNetwork.MapNetworkDrive “K:”, strDrive

Can we explain how this script works? That’s a good question; before we answer that let’s check and see how much money is still owed on the Scripting House …. Holy smokes!

OK, it’s safe to say that, yes, we can explain how this script works. In fact, by the looks of things we’ll be explaining how scripts work for the next 100 years or so.

As you can see, we start off by creating an instance of the ADSystemInfo object; this is a handy little ADSI object that directly – and instantly – retrieves information about the logged-on user and the computer he or she is using. What kind of information does the ADSystemInfo object retrieve? Well, for one thing, it can – via the UserName property – retrieve the distinguished name of the logged-on user; that’s going to be a value similar to this:

CN=kenmyer, OU=Finance, dc=fabrikam, dc=com

Is that a useful piece of information to have? You bet it is; once we know the user’s distinguished name we can then use this line of code to bind to that user’s Active Directory account:

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

And once we bind to the user account we can then determine both the user’s logon name and his or her OU. Which just happens to be the very information we need in order to map the drive.

Let’s do the easy part first: determine the user’s logon name. That’s something we can do by grabbing the value of the samAccountName property (e.g., kenmyer) and storing it in a variable named strUserName, like so:

strUserName = objUser.samAccountName

Like we said, that was pretty easy. Unfortunately, retrieving the name of the user’s OU is a little bit trickier; that’s because there is no OU attribute that tells us which OU an account lives in. About the best we can do is use the Parent attribute to determine the full ADsPath to that OU. In other words, the Parent attribute returns a value similar to this:

LDAP://OU=Finance, dc=fabrikam, dc=com

But that doesn’t help us, does it?

Oh, wait: as a matter of fact it does help us. As you can see, the name of the user’s OU – Finance – is embedded in the ADsPath. We just need to figure out a way to extract that OU name.

Although there are probably a number of different ways we could do that, here’s the approach we took. To begin with, we use this line of code and the VBScript Split function to split the OU’s ADsPath on the comma:

arrContainers = Split(strOUPath, “,”)

Why do we do that? Well, what the Split function does is look for the specified delimiter (in this case, the comma) and separate the string value into an array of values, creating a new array value each time it encounters the delimiter. That means that a value like LDAP://OU=Finance, dc=fabrikam, dc=com will be magically transformed into an array consisting of the following elements:

LDAP://OU=Finance

dc=fabrikam

dc=com

As you can see, we’re getting closer to our goal. With that in mind, our next step is to create a second array by splitting the very first item (item 0) on the equals sign. In other words, we’re going to take the value LDAP://OU=Finance and create a teeny-tiny ltitle array consisting of the following two items:

LDAP://OU

Finance

That’s what this line of code does for us:

arrOU = Split(arrContainers(0), “=”)

Amazingly enough, we now know which OU the user account resides in: it lives in the Finance OU. And guess what? The name of that OU just happens to be the second time (element 1) in the array arrOU. That means we can then use this line of code to store the user’s OU name in the variable strOU:

strOU = arrOU(1)

At this point we only have to do two more things: we need to piece together the path to the shared folder we want to map, then we need to go ahead and map the drive. To piece together the path we use this line of code:

strDrive = “\\Mission\Apps\Timesheets\” & strOU & “\” & strUserName

It shouldn’t be too hard to see what’s going on here. In KB’s case, users have folders that reside on the server \\Mission, in the Apps\Timesheets folder. Within the Timesheets folder are separate folders for each OU, and within those folders are separate folders for each user. Therefore we’re simply combining the following values and storing the resulting string in a variable named strDrive:

\\Mission\Apps\Timesheets\

Finance

\

kenmyer

In other words, strDrive is going to be equal to this:

\\Mission\Apps\Timesheets\Finance\kenmyer

And as soon as we have the folder path we can then use this little block of code to map drive K to the appropriate folder:

Set objNetwork = CreateObject(“Wscript.Network”)
objNetwork.MapNetworkDrive “K:”, strDrive

That’s all we need to do.

No doubt you’re wondering whether the Scripting Guy who writes this column really spent the entire weekend doing nothing but sitting in front of the TV watching basketball. The answer to that is yes, he did. We realize that that’s an almost-unbelievable feat of stamina and dedication; it’s amazing that a human being could do absolutely nothing for so long. In all fairness, however, it must be admitted that the Scripting Guy who writes this column cheated a little bit. After all, he lives in the Seattle area, and with it raining all day Saturday and most of the day Sunday, well, staying inside and watching TV wasn’t all that hard to do.

By the way, did we mention that Kentucky has been eliminated, too? You know, maybe this is heaven after all. And if the Scripting Guys are still here and are still sitting in their same offices that can only mean one thing: Microsoft, having passed on YouTube, decided to buy heaven instead.

Note. Feel free to insert your own joke about Microsoft, buying heaven rather than buying … well, some other place ….

0 comments

Discussion is closed.

Feedback usabilla icon