{"id":67963,"date":"2006-02-14T08:28:00","date_gmt":"2006-02-14T08:28:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/02\/14\/how-can-i-convert-a-dn-to-this-format-finance-fabrikam-comusers\/"},"modified":"2006-02-14T08:28:00","modified_gmt":"2006-02-14T08:28:00","slug":"how-can-i-convert-a-dn-to-this-format-finance-fabrikam-comusers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-convert-a-dn-to-this-format-finance-fabrikam-comusers\/","title":{"rendered":"How Can I Convert a DN to this Format: finance.fabrikam.com\/Users?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I convert a DN to this format: finance.fabrikam.com\/Users?<BR><BR>&#8212; CW<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, CW. You know, this is a pretty good question; in fact, we should have used it for the <A href=\"http:\/\/null\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><B>Winter Scripting Games<\/B><\/A>. Ah, but, on second thought, maybe it\u2019s a good thing we <I>didn\u2019t<\/I> use it. Why not? Well, because all the Scripting Games competitors could find the answer right here:<\/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>strDN = objUser.distinguishedName<\/p>\n<p>arrDN = Split(strDN, &#8220;,&#8221;)<\/p>\n<p>For i = 0 to UBound(arrDN)\n    intLength = Len(arrDN(i))\n    intCounter = intLength &#8211; 3\n    arrDN(i) = Right(arrDn(i), intCounter)\nNext<\/p>\n<p>For i = 1 to UBound(arrDN) &#8211; 1\n    strNewName = strNewName &amp; arrDN(i) &amp; &#8220;.&#8221;\nNext<\/p>\n<p>intLastItem = UBound(arrDN)\nstrNewName = strNewName &amp; arrDN(intLastItem) &amp; &#8220;\/&#8221; &amp; arrDN(0)<\/p>\n<p>Wscript.Echo strNewName\n<\/PRE>\n<P>OK, yes: it\u2019s a <I>little<\/I> complicated. But really, it\u2019s not as bad as it first looks, something you\u2019ll soon see for yourself.<\/P>\n<P>To begin with, CW, it appears your example referenced an OU, something with a DN (distinguished name) like this:<\/P><PRE class=\"codeSample\">ou=Users,ou=finance,dc=fabrikam,dc=com\n<\/PRE>\n<P>We thought we should point out that we\u2019re using a user account as our example. However, that shouldn\u2019t matter: this technique will work with any DN, regardless of the underlying object type. We went with the user account simply because it provided a way for us to offer up a sample script that anyone could use as-is, without having to change a thing.<\/P>\n<P>You\u2019re welcome.<\/P>\n<P>The script starts off by creating an instance of the <B>ADSystemInfo<\/B> object; this is an ADSI object that returns information about the logged-on user. We grab the value of the <B>UserName<\/B> property and store it in a variable named strUser, then use that value to bind to the actual user account in Active Directory. All that takes just two lines of code:<\/P><PRE class=\"codeSample\">strUser = objSysInfo.UserName\nSet objUser = GetObject(&#8220;LDAP:\/\/&#8221; &amp; strUser)\n<\/PRE>\n<P>Once we\u2019ve made a connection to the user account we can then get the value of the <B>distinguishedName<\/B> attribute, something we store in a variable named strDN. That DN will look something like this:<\/P><PRE class=\"codeSample\">cn=KenMyer,ou=finance,dc=fabrikam,dc=com\n<\/PRE>\n<P>OK, that part was easy. But <I>now<\/I> what do we do?<\/P>\n<P>Here\u2019s what we do. First, we issue this command:<\/P><PRE class=\"codeSample\">arrDN = Split(strDN, &#8220;,&#8221;)\n<\/PRE>\n<P>What this does is split our DN into an array named arrDN. By splitting the DN on the comma we end up with an array that looks like this:<\/P><PRE class=\"codeSample\">cn=KenMyer\nou=finance\ndc=fabrikam\ndc=com\n<\/PRE>\n<P>In turn, that brings us to this block of code:<\/P><PRE class=\"codeSample\">For i = 0 to UBound(arrDN)\n    intLength = Len(arrDN(i))\n    intCounter = intLength &#8211; 3\n    arrDN(i) = Right(arrDn(i), intCounter)\nNext\n<\/PRE>\n<P>At the moment, arrDN consists of all the items we need to construct our new name format. However, there\u2019s a problem: the first three characters in each of these elements (e.g., <B>cn=<\/B>KenMyer) are characters we don\u2019t want in our new name. (In other words, we want to chop the <B>cn=<\/B> off cn=KenMyer). That\u2019s what the preceding block of code does. For each item in the array we:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Use the <B>Length<\/B> function to determine the length of the item (that is, the number of characters in the string).<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Subtract 3 from the length. Why 3? Because that\u2019s the number of characters we want to get rid of.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Use the <B>Right<\/B> function to extract all but the first three letters of the string. That means that, working backwards, we take the characters <B>KenMyer<\/B> but <I>don\u2019t<\/I> take the characters <B>cn=<\/B>. We then assign this truncated value to the value of the array item.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>That should make more sense when you see what our array looks like now:<\/P><PRE class=\"codeSample\">KenMyer\nfinance\nfabrikam\ncom\n<\/PRE>\n<P>As you can see, we\u2019re getting closer.<\/P>\n<P>Next we take the middle items in the array (excluding the first and last items) and construct a string that looks like this:<\/P><PRE class=\"codeSample\">finance.fabrikam.\n<\/PRE>\n<P>Here\u2019s how we do that:<\/P><PRE class=\"codeSample\">For i = 1 to UBound(arrDN) &#8211; 1\n    strNewName = strNewName &amp; arrDN(i) &amp; &#8220;.&#8221;\nNext\n<\/PRE>\n<P>Notice that in this For Next loop we start with the second item in the array (remember, the first item in an array is always item 0, making the second item item 2). Inside the loop we build up a string value named strNewName that consists of the value of each array item plus a period tacked on to the end. Note again that we\u2019re not using <I>all<\/I> the array items: in addition to starting with item 2 we also stop before we get to the last item. Why? Well, we don\u2019t want to append a period to the very last item; working with all the items except the last one makes it easy to avoid putting a period at the end of that last item. When this block of code finishes we\u2019ll have a string that looks like this:<\/P><PRE class=\"codeSample\">finance.fabrikam.\n<\/PRE>\n<P>We\u2019re now so close we can taste it. To finish this off all we have to do is add three things to the end of the string: <\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The last item in the array (we can easily determine the item number of the last item by using the <B>UBound<\/B> function).<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The <B>\/ <\/B>character.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The user\u2019s CN (which happens to be the first item in our array).<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Sounds hard, doesn\u2019t it? And yet, all those chores can be achieved with just two lines of code:<\/P><PRE class=\"codeSample\">intLastItem = UBound(arrDN)\nstrNewName = strNewName &amp; arrDN(intLastItem) &amp; &#8220;\/&#8221; &amp; arrDN(0)\n<\/PRE>\n<P>And then we simply echo back our new name and call it a day:<\/P><PRE class=\"codeSample\">finance.fabrikam.com\/KenMyer\n<\/PRE>\n<P>Cool, huh?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I convert a DN to this format: finance.fabrikam.com\/Users?&#8212; CW Hey, CW. You know, this is a pretty good question; in fact, we should have used it for the Winter Scripting Games. Ah, but, on second thought, maybe it\u2019s a good thing we didn\u2019t use it. Why not? Well, because all [&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,9,3,4,21,20,5],"class_list":["post-67963","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-adsi","tag-scripting-guy","tag-scripting-techniques","tag-string-manipulation","tag-user-accounts","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I convert a DN to this format: finance.fabrikam.com\/Users?&#8212; CW Hey, CW. You know, this is a pretty good question; in fact, we should have used it for the Winter Scripting Games. Ah, but, on second thought, maybe it\u2019s a good thing we didn\u2019t use it. Why not? Well, because all [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67963","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=67963"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67963\/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=67963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}