{"id":68603,"date":"2005-11-03T16:44:00","date_gmt":"2005-11-03T16:44:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/11\/03\/how-can-i-determine-the-adspath-for-the-logged-on-user\/"},"modified":"2005-11-03T16:44:00","modified_gmt":"2005-11-03T16:44:00","slug":"how-can-i-determine-the-adspath-for-the-logged-on-user","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-adspath-for-the-logged-on-user\/","title":{"rendered":"How Can I Determine the ADsPath for the Logged-On User?"},"content":{"rendered":"<p><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\"> \n<P>Hey, Scripting Guy! In my HTA, how can I determine the ADsPath for the logged-on user?<BR><BR>&#8212; AC<\/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, 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\u2019t true: our tower is really more concrete and steel than it is ivory.<\/P>\n<P>Well, except for Dean\u2019s office.<\/P>\n<P>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\u2019s not necessarily a good thing, but it <I>is<\/I> the nature of the technical writing business. To paraphrase the old saying, those who can\u2019t &#8211; or at least those who don\u2019t &#8211; teach.<\/P>\n<P>Every now and then, however, we <I>do<\/I> 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\u2019re 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\u2019ll let everyone else in on the secret.<\/P>\n<P>Let\u2019s start off by showing you how to do this in a plain old VBScript script, and then we\u2019ll show you a simple way to embed the code in an HTA. Here\u2019s a script that reports back the ADsPath for the logged-on user:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Set objSysInfo = CreateObject(&#8220;ADSystemInfo&#8221;)<\/p>\n<p>strUser = objSysInfo.UserName\nSet objUser = GetObject(&#8220;LDAP:\/\/&#8221; &amp; strUser)<\/p>\n<p>Wscript.Echo objUser.AdsPath\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EFD\" 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>. Well, sure, it <I>is<\/I> pretty simple. But, remember, we didn\u2019t say we worked <I>hard<\/I> on this real-life script, we just said we actually <I>worked<\/I> on a real-life script.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>The script starts out by creating an instance of the <B>ADSystemInfo<\/B> object:<\/P><PRE class=\"codeSample\">Set objSysInfo = CreateObject(&#8220;ADSystemInfo&#8221;)\n<\/PRE>\n<P>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 <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/misc\/adsi\/msadvb03.mspx\"><B>this script<\/B><\/A> 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\u2019t create an instance of ADSystemInfo on a remote computer and then get information about the user logged on to <I>that<\/I> machine. And, yes, that would be very cool. But it can\u2019t be done.<\/P>\n<P>One of the pieces of information that ADSystemInfo returns is the <B>UserName<\/B>. The UserName is actually the distinguished name of the logged-on user; that\u2019s going to look something like this:<\/P><PRE class=\"codeSample\">CN=Ken Myer,OU=Finance,dc=fabrikam,dc=com\n<\/PRE>\n<P>At this point we can actually construct the ADsPath ourselves; after all, the ADsPath is simply the LDAP: provider plus the distinguished name:<\/P><PRE class=\"codeSample\">LDAP:\/\/CN=Ken Myer,OU=Finance,dc=fabrikam,dc=com\n<\/PRE>\n<P>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:<\/P><PRE class=\"codeSample\">strUser = objSysInfo.UserName\nSet objUser = GetObject(&#8220;LDAP:\/\/&#8221; &amp; strUser)\n<\/PRE>\n<P>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 <B>GetObject<\/B> call that binds us to the user account.<\/P>\n<P>From there we simply echo back the value of the ADsPath attribute; seeing as how we\u2019re connected to the Ken Myer user account, though, we could just as easily echo back the value of <I>any<\/I> (or all) of Ken\u2019s account attributes.<\/P>\n<P>That <I>is<\/I> easy, isn\u2019t it?<\/P>\n<P>Now, what about using this code in an HTA? Well, here\u2019s a very simple example of that:<\/P><PRE class=\"codeSample\">&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;ADSystemInfo Sample&lt;\/title&gt;\n&lt;\/head&gt;<\/p>\n<p>&lt;SCRIPT LANGUAGE=&#8221;VBScript&#8221;&gt;<\/p>\n<p>    Sub Window_OnLoad\n        On Error Resume Next<\/p>\n<p>        Set objSysInfo = CreateObject(&#8220;ADSystemInfo&#8221;)<\/p>\n<p>        strUser = objSysInfo.UserName\n        Set objUser = GetObject(&#8220;LDAP:\/\/&#8221; &amp; strUser)<\/p>\n<p>        DataArea.InnerHTML = objUser.AdsPath\n    End Sub<\/p>\n<p>&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;span id=DataArea&gt;&lt;\/span&gt;<\/p>\n<p>&lt;\/body&gt;\n&lt;\/html&gt;\n<\/PRE>\n<P>This HTA contains only a single element: a <B>&lt;span&gt;<\/B> tag named DataArea. When the HTA starts up, it runs a subroutine named <B>Window_Onload<\/B>. (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:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Set objSysInfo = CreateObject(&#8220;ADSystemInfo&#8221;)<\/p>\n<p>strUser = objSysInfo.UserName\nSet objUser = GetObject(&#8220;LDAP:\/\/&#8221; &amp; strUser)<\/p>\n<p>DataArea.InnerHTML = objUser.AdsPath\n<\/PRE>\n<P>That\u2019s right: it\u2019s 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 &lt;span&gt; tag:<\/P><PRE class=\"codeSample\">DataArea.InnerHTML = objUser.AdsPath\n<\/PRE>\n<P>Now, does that look like the sort of thing someone sitting in an ivory tower could write?<\/P>\n<P>Well, OK, now that you mention it \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! In my HTA, how can I determine the ADsPath for the logged-on user?&#8212; AC 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\u2019t true: [&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":[9,16,47,3,4,5],"class_list":["post-68603","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-adsi","tag-desktop-management","tag-general-management-tasks","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! In my HTA, how can I determine the ADsPath for the logged-on user?&#8212; AC 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\u2019t true: [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68603","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=68603"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68603\/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=68603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}