{"id":65903,"date":"2006-12-06T13:53:00","date_gmt":"2006-12-06T13:53:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/12\/06\/how-can-i-determine-the-ou-the-local-computer-belongs-to\/"},"modified":"2006-12-06T13:53:00","modified_gmt":"2006-12-06T13:53:00","slug":"how-can-i-determine-the-ou-the-local-computer-belongs-to","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-ou-the-local-computer-belongs-to\/","title":{"rendered":"How Can I Determine the OU the Local Computer Belongs To?"},"content":{"rendered":"<p><img decoding=\"async\" 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\"><\/p>\n<p>Hey, Scripting Guy! In a logon script, which attribute do I use to determine the OU that the local computer belongs to?<\/p>\n<p>&#8212; JP<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><img decoding=\"async\" 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 decoding=\"async\" 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><\/p>\n<p>Hey, JP. Most people are familiar with Cupid, the Roman god of love. In fact, you\u2019d be hard-pressed to find anyone who doesn\u2019t know that Cupid carries with him a quiver of gold-tipped arrows. Get struck by one of Cupid\u2019s arrows, and you\u2019ll instantly fall in love with whomever Cupid dictates. <\/p>\n<p>Ah, but here\u2019s something that most people <i>don\u2019t<\/i> know: in addition to his gold-tipped arrows Cupid also carries a few lead-tipped arrows. What happens if you get struck by one of <i>those<\/i> arrows? You got it: you\u2019ll instantly <i>hate<\/i> whomever Cupid dictates. Something to keep in mind the next time you see Cupid buzzing around your window.<\/p>\n<p>So why are we wasting valuable Web space talking about Roman mythology? That\u2019s easy: we Scripting Guys have taken a sacred vow to tell people things that they don\u2019t already know. Most people don\u2019t know that Cupid can cause you to hate others just as easily as he can cause you to love others; therefore, it\u2019s our <i>duty<\/i> to tell you this. Likewise, it\u2019s our duty to tell you that\u2013 for some reason \u2013 there is no Active Directory attribute that tells you which OU a computer belongs to. If you were hoping to use a line of code like this to determine a computer\u2019s OU, well, sorry but you\u2019re out of luck:<\/p>\n<pre class=\"codeSample\">Wscript.Echo objComputer.OU\n<\/pre>\n<p>But wait; don\u2019t go just yet. After all, the Scripting Guys have taken a <i>lot<\/i> of sacred vows; let\u2019s see if we can find another one that might prove useful. Let\u2019s see: \u201cAlways put the garbage out the night <i>before<\/i> the garbage gets picked up.\u201d Good advice, but it probably doesn\u2019t apply here. <\/p>\n<p>How about this one: \u201cAlways make fun of the Oakland Raiders even when there\u2019s absolutely no reason to mention the Oakland Raiders\u201d Actually, we\u2019d never do <i>that<\/i>; Oakland Raider fans are miserable enough as it is.<\/p>\n<p>Oh, wait, here we go: \u201cWhenever there\u2019s no obvious way to do something try to find a way to do it anyway.\u201d<\/p>\n<p>Based on that sacred vow:<\/p>\n<pre class=\"codeSample\">Set objSysInfo = CreateObject(\"ADSystemInfo\")\nstrComputer = objSysInfo.ComputerName\nSet objComputer = GetObject(\"LDAP:\/\/\" &amp; strComputer)\narrOUs = Split(objComputer.Parent, \",\")\narrMainOU = Split(arrOUs(0), \"=\")\nWscript.Echo arrMainOU(1)\n<\/pre>\n<p>Yes, this <i>is<\/i> an odd-looking script; for one thing, who\u2019d have ever guessed that you could use that many <b>Split<\/b> functions in such a short snippet of code? So let\u2019s see if we can explain how this all works.<\/p>\n<p>To begin with, we create an instance of the <b>ADSystemInfo<\/b> object, an object that can return all sorts of useful information about the local computer and the logged-on user (for a sample script that shows <i>all<\/i> the information that can be returned using ADSystemInfo click <a href=\"http:\/\/null\/technet\/scriptcenter\/scripts\/misc\/adsi\/msadvb03.mspx\"><b>here<\/b><\/a>). We then use this line of code to store the value of the <b>ComputerName<\/b> property in a variable named strComputer:<\/p>\n<pre class=\"codeSample\">strComputer = objSysInfo.ComputerName\n<\/pre>\n<p>As it turns out, the ComputerName property actually returns the distinguished name of the computer; that means you\u2019re going to get back a value similar to this:<\/p>\n<pre class=\"codeSample\">CN=ATL-WS-01,OU=Accounting,OU=Finance,DC=fabrikam,DC=com\n<\/pre>\n<p>As you can see, the computer\u2019s OU \u2013 Accounting \u2013 happens to be embedded within the distinguished name. All we have to do now is figure out how to extract the OU name.<\/p>\n<p>To be honest, there are several different ways to go about extracting the OU name. After giving this a tiny bit of thought we decided to start the process by using this line of code to connect to the computer account in Active Directory:<\/p>\n<pre class=\"codeSample\">Set objComputer = GetObject(\"LDAP:\/\/\" &amp; strComputer)\n<\/pre>\n<p>That, in turn, brings us to this portion of the script:<\/p>\n<pre class=\"codeSample\">arrOUs = Split(objComputer.Parent, \",\")\n<\/pre>\n<p>What the heck is going on <i>here<\/i>? What we\u2019re doing is using the Split function to split the value of the computer\u2019s <b>Parent<\/b> attribute into an array. What\u2019s the Parent attribute? In this case, it\u2019s going to be the ADsPath of the container in which the computer object resides. In other words, it\u2019s equal to this:<\/p>\n<pre class=\"codeSample\">LDAP:\/\/OU=Accounting,OU=Finance,DC=fabrikam,DC=com\n<\/pre>\n<p>When we split the value of the Parent attribute on the comma, we end up with an array (named arrOUs) that contains the following elements:<\/p>\n<pre class=\"codeSample\">LDAP:\/\/OU=Accounting\nOU=Finance\nDC=fabrikam\nDC=com\n<\/pre>\n<p>As you can see, we\u2019re getting closer; in fact, the first element in our array (element 0) is exactly what we need, assuming we can get rid of the <b>LDAP:\/\/OU=,<\/b> that is. So then why <i>don\u2019t<\/i> we get rid of the <b>LDAP:\/\/OU=<\/b>? The following line of code takes element 0 in the array arrOUs \u2013 <b>LDAP:\/\/OU=Accounting<\/b> \u2013 and splits <i>that<\/i> value on the equals sign (=):<\/p>\n<pre class=\"codeSample\">arrMainOU = Split(arrOUs(0), \"=\")\n<\/pre>\n<p>The net result? Another array (arrMainOU) containing these two elements:<\/p>\n<pre class=\"codeSample\">LDAP:\/\/OU\nAccounting\n<\/pre>\n<p>All we have to do now is echo back the second item in the array (element 1) and we\u2019re done:<\/p>\n<pre class=\"codeSample\">Wscript.Echo arrMainOU(1)\n<\/pre>\n<p>Like we said, it\u2019s a roundabout way of getting the computer OU but, for better or worse, there\u2019s nothing <i>but<\/i> a roundabout way of getting the computer OU. But hey, as long as it works, right?<\/p>\n<p>Now, before we go, is there anything else that the Scripting Guys know that you need to know as well? Maybe, say, the story of Cupid and Psyche, complete with deranged man-killing sheep, talking towers, and a box full of eternal sleep?<\/p>\n<p>Oh. Well, OK; guess we\u2019ll save that story for a future column. Hey, what are you guys doing tomorrow?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! In a logon script, which attribute do I use to determine the OU that the local computer belongs to? &#8212; JP Hey, JP. Most people are familiar with Cupid, the Roman god of love. In fact, you\u2019d be hard-pressed to find anyone who doesn\u2019t know that Cupid carries with him a quiver [&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,43,3,4,5],"class_list":["post-65903","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-adsi","tag-ous","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! In a logon script, which attribute do I use to determine the OU that the local computer belongs to? &#8212; JP Hey, JP. Most people are familiar with Cupid, the Roman god of love. In fact, you\u2019d be hard-pressed to find anyone who doesn\u2019t know that Cupid carries with him a quiver [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65903","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=65903"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65903\/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=65903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}