{"id":65273,"date":"2007-03-21T01:48:00","date_gmt":"2007-03-21T01:48:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/03\/21\/how-can-i-map-a-drive-based-on-a-users-name-and-ou\/"},"modified":"2007-03-21T01:48:00","modified_gmt":"2007-03-21T01:48:00","slug":"how-can-i-map-a-drive-based-on-a-users-name-and-ou","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-map-a-drive-based-on-a-users-name-and-ou\/","title":{"rendered":"How Can I Map a Drive Based on a User\u2019s Name and OU?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/H2>\n<P>Hey, Scripting Guy! I need to create a logon script that maps a network drive based on the logged-on user\u2019s user name and OU. How can I do that?<BR><BR>&#8212; KB<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>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 \u2013 Thursday, Friday, Saturday, and Sunday \u2013 he\u2019s 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. <\/P>\n<TABLE class=\"dataTable\" id=\"E2C\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. In case you\u2019re 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 <I>that<\/I> goes without saying.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>But all his fun came to a crashing halt come Monday morning and time to go back to work.<\/P>\n<P>That\u2019s why this is such a tough day for the Scripting Guy who writes this column; as difficult as this is to believe, he doesn\u2019t find work anywhere near as interesting and exciting as he finds college basketball. Of course, you\u2019re probably thinking, \u201cWe understand how he feels. On the other hand, the Scripting Guy who writes that column is a true professional: somehow he\u2019ll reach down deep inside and find a way to do his work even though he\u2019d much rather be sitting at home watching even <I>more<\/I> college basketball. Are we right or are we right?\u201d<\/P>\n<P>Well, to be honest, you\u2019re wrong: the Scripting Guy who writes this column is anything <I>but<\/I> a true professional. On the other hand, he <I>does<\/I> have a mortgage payment due at the end of the month. With that in mind, here\u2019s a logon script that can map a network drive based on the logged-on user\u2019s user name and OU:<\/P><PRE class=\"codeSample\">Set objSysInfo = CreateObject(&#8220;ADSystemInfo&#8221;)<\/p>\n<p>strUser = objSysInfo.UserName\nSet objUser = GetObject(&#8220;LDAP:\/\/&#8221; &amp; strUser)<\/p>\n<p>strUserName = objUser.samAccountName<\/p>\n<p>strOUPath = objUser.Parent\narrContainers = Split(strOUPath, &#8220;,&#8221;)\narrOU = Split(arrContainers(0), &#8220;=&#8221;)\nstrOU = arrOU(1)<\/p>\n<p>strDrive = &#8220;\\\\Mission\\Apps\\Timesheets\\&#8221; &amp; strOU &amp; &#8220;\\&#8221; &amp; strUserName<\/p>\n<p>Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\nobjNetwork.MapNetworkDrive &#8220;K:&#8221;, strDrive\n<\/PRE>\n<P>Can we explain how this script works? That\u2019s a good question; before we answer that let\u2019s check and see how much money is still owed on the Scripting House \u2026. Holy smokes! <\/P>\n<P>OK, it\u2019s safe to say that, yes, we can explain how this script works. In fact, by the looks of things we\u2019ll be explaining how scripts work for the next 100 years or so.<\/P>\n<P>As you can see, we start off by creating an instance of the <B>ADSystemInfo<\/B> object; this is a handy little ADSI object that directly \u2013 and instantly \u2013 retrieves information about the logged-on user and the computer he or she is using. What <I>kind<\/I> of information does the ADSystemInfo object retrieve? Well, for one thing, it can \u2013 via the <B>UserName<\/B> property \u2013 retrieve the distinguished name of the logged-on user; that\u2019s going to be a value similar to this:<\/P><PRE class=\"codeSample\">CN=kenmyer, OU=Finance, dc=fabrikam, dc=com\n<\/PRE>\n<P>Is that a useful piece of information to have? You bet it is; once we know the user\u2019s distinguished name we can then use this line of code to bind to that user\u2019s Active Directory account:<\/P><PRE class=\"codeSample\">Set objUser = GetObject(&#8220;LDAP:\/\/&#8221; &amp; strUser)\n<\/PRE>\n<P>And once we bind to the user account we can then determine both the user\u2019s logon name and his or her OU. Which just happens to be the very information we need in order to map the drive.<\/P>\n<P>Let\u2019s do the easy part first: determine the user\u2019s logon name. That\u2019s something we can do by grabbing the value of the <B>samAccountName<\/B> property (e.g., <I>kenmyer<\/I>) and storing it in a variable named strUserName, like so:<\/P><PRE class=\"codeSample\">strUserName = objUser.samAccountName\n<\/PRE>\n<P>Like we said, that was pretty easy. Unfortunately, retrieving the name of the user\u2019s OU is a little bit trickier; that\u2019s because there is no OU attribute that tells us which OU an account lives in. About the best we can do is use the <B>Parent<\/B> attribute to determine the full ADsPath to that OU. In other words, the Parent attribute returns a value similar to this:<\/P><PRE class=\"codeSample\">LDAP:\/\/OU=Finance, dc=fabrikam, dc=com\n<\/PRE>\n<P>But that doesn\u2019t help us, does it?<\/P>\n<P>Oh, wait: as a matter of fact it <I>does<\/I> help us. As you can see, the name of the user\u2019s OU \u2013 Finance \u2013 is embedded in the ADsPath. We just need to figure out a way to extract that OU name.<\/P>\n<P>Although there are probably a number of different ways we could do that, here\u2019s the approach we took. To begin with, we use this line of code and the VBScript <B>Split<\/B> function to split the OU\u2019s ADsPath on the comma:<\/P><PRE class=\"codeSample\">arrContainers = Split(strOUPath, &#8220;,&#8221;)\n<\/PRE>\n<P>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 <I>LDAP:\/\/OU=Finance, dc=fabrikam, dc=com<\/I> will be magically transformed into an array consisting of the following elements:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>LDAP:\/\/OU=Finance<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>dc=fabrikam<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>dc=com<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>As you can see, we\u2019re 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\u2019re going to take the value <I>LDAP:\/\/OU=Finance<\/I> and create a teeny-tiny ltitle array consisting of the following two items:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>LDAP:\/\/OU<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Finance<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>That\u2019s what this line of code does for us:<\/P><PRE class=\"codeSample\">arrOU = Split(arrContainers(0), &#8220;=&#8221;)\n<\/PRE>\n<P>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\u2019s OU name in the variable strOU:<\/P><PRE class=\"codeSample\">strOU = arrOU(1)\n<\/PRE>\n<P>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:<\/P><PRE class=\"codeSample\">strDrive = &#8220;\\\\Mission\\Apps\\Timesheets\\&#8221; &amp; strOU &amp; &#8220;\\&#8221; &amp; strUserName\n<\/PRE>\n<P>It shouldn\u2019t be too hard to see what\u2019s going on here. In KB\u2019s 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\u2019re simply combining the following values and storing the resulting string in a variable named strDrive:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\\\\Mission\\Apps\\Timesheets\\<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Finance<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\\<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>kenmyer<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>In other words, strDrive is going to be equal to this:<\/P><PRE class=\"codeSample\">\\\\Mission\\Apps\\Timesheets\\Finance\\kenmyer\n<\/PRE>\n<P>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:<\/P><PRE class=\"codeSample\">Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\nobjNetwork.MapNetworkDrive &#8220;K:&#8221;, strDrive\n<\/PRE>\n<P>That\u2019s all we need to do.<\/P>\n<P>No doubt you\u2019re 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\u2019s an almost-unbelievable feat of stamina and dedication; it\u2019s 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\u2019t all that hard to do. <\/P>\n<P>By the way, did we mention that Kentucky has been eliminated, too? You know, maybe this <I>is<\/I> 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. <\/P>\n<TABLE class=\"dataTable\" id=\"EYG\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Feel free to insert your own joke about Microsoft, buying heaven rather than buying \u2026 well, some other place \u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I need to create a logon script that maps a network drive based on the logged-on user\u2019s user name and OU. How can I do that?&#8212; KB 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,43,3,5],"class_list":["post-65273","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-ous","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I need to create a logon script that maps a network drive based on the logged-on user\u2019s user name and OU. How can I do that?&#8212; KB 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=65273"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65273\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=65273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}