{"id":69553,"date":"2005-06-21T16:05:00","date_gmt":"2005-06-21T16:05:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/06\/21\/hey-scripting-guy-how-can-i-retrieve-the-value-of-an-active-directory-attribute-that-has-a-hyphen-in-its-name\/"},"modified":"2005-06-21T16:05:00","modified_gmt":"2005-06-21T16:05:00","slug":"hey-scripting-guy-how-can-i-retrieve-the-value-of-an-active-directory-attribute-that-has-a-hyphen-in-its-name","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-retrieve-the-value-of-an-active-directory-attribute-that-has-a-hyphen-in-its-name\/","title":{"rendered":"Hey, Scripting Guy! How Can I Retrieve the Value of an Active Directory Attribute that has a Hyphen in Its Name?"},"content":{"rendered":"<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" class=\"nearGraphic\" \/><\/p>\n<p>Hey, Scripting Guy! We have a custom application that adds some attributes to the Active Directory schema. These attributes have hyphens in their names, and I can&rsquo;t figure out how to retrieve the value of those attributes; any time I try to connect to one I get an error message. Any suggestions?<\/p>\n<p>&#8212; TW<\/p>\n<p><img decoding=\"async\" height=\"5\" width=\"5\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" alt=\"Spacer\" border=\"0\" \/><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" class=\"nearGraphic\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><\/a><\/p>\n<p>Hey, TW. You know a lot of people don&rsquo;t believe in coincidence or chance occurrence; instead, they believe that everything happens for a reason. (Of course, in the case of the Scripting Guys we usually can&rsquo;t figure out what the heck that reason could be, but, still&hellip;.)<\/p>\n<p>Why do we mention the fact that many people don&rsquo;t believe in coincidences? Well, interestingly enough, about the same time we received your email we were fooling around with some of the new attributes that have been added to the Active Directory schema for Windows Server 2003. Many of these new attributes also have hyphens in their names; thus we had simple little scripts similar to this:<\/p>\n<pre class=\"codeSample\">Set objUser = GetObject(LDAP:\/\/cn=Ken Myer, OU=Finance, DC=fabrikam, DC=com)<\/pre>\n<pre class=\"codeSample\">Wscript.Echo \"User account control computed: \" &amp; objUser.msDS-User-Account-Control-Computed<\/pre>\n<p>And what do you suppose happened when we tried running one of these simple little scripts? If you guessed, &ldquo;It probably worked and you got back the value you were looking for,&rdquo; well, take a look at this:<\/p>\n<pre class=\"codeSample\">C:\\Scripts\\test.vbs(2, 1) Microsoft VBScript runtime error: Object doesn't support <br \/>this property or method: 'objUser.msDS'<\/pre>\n<p>Yikes. ADSI doesn&rsquo;t like seeing a hyphen in an attribute name; in fact, as far as ADSI is concerned the name of our property (msDS-User-Account-Control-Computed) consists of just the four letters <b>msDS<\/b>. As soon as it encounters the hyphen ADSI assumes it has reached the end of the attribute name; it then tries looking for an attribute named msDS and, not surprisingly, can&rsquo;t find one. <\/p>\n<p>Ah, but now the chain of &ldquo;coincidences&rdquo; gets <i>really<\/i> spooky. Just a week or so before that we had resolved a problem that had been bugging us for nearly a year: <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/may05\/hey0524.mspx\"><b>how can you use ADO to read a text file that contains a space in the file name?<\/b><\/a> As it turned out, the answer was to enclose the file name in square brackets, like so:<\/p>\n<pre class=\"codeSample\">strFile = \"[Fabrikam Employees.txt]\"<\/pre>\n<p>Although it seemed like a million-to-one shot, we decided to put square brackets around our Active Directory attribute name:<\/p>\n<pre class=\"codeSample\">Set objUser = GetObject(LDAP:\/\/cn=Ken Myer, OU=Finance, DC=fabrikam, DC=com)<\/pre>\n<pre class=\"codeSample\">Wscript.Echo \"User account control computed: \" &amp; objUser.[msDS-User-Account-Control-Computed]<\/pre>\n<p>If you&rsquo;ve ever seen the <i>Twilight Zone<\/i> then you already know what happened: the square brackets worked like a charm! Do you need to get or set the value of an Active Directory attribute that has a hyphen in the name? Then just put square brackets around the name. Do that and &#8211; like magic &#8211; everything will work perfectly. Try it and see.<\/p>\n<p>Yes, <i>very<\/i> eerie, isn&rsquo;t it?<\/p>\n<p>By the way, you can put square brackets around <i>any<\/i> attribute name and your script will work just fine. We aren&rsquo;t sure why you&rsquo;d do this, but following script will return the CN for the user Ken Myer:<\/p>\n<pre class=\"codeSample\">Set objUser = GetObject(LDAP:\/\/cn=Ken Myer, OU=Finance, DC=fabrikam, DC=com)<\/pre>\n<pre class=\"codeSample\">Wscript.Echo \"CN: \" &amp; objUser.[CN]<\/pre>\n<p>Makes you shudder just thinking about it, doesn&rsquo;t it? But, then again, maybe it&rsquo;s all just a coincidence&hellip;.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! We have a custom application that adds some attributes to the Active Directory schema. These attributes have hyphens in their names, and I can&rsquo;t figure out how to retrieve the value of those attributes; any time I try to connect to one I get an error message. Any suggestions? &#8212; TW Hey, [&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,3,20,5],"class_list":["post-69553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-user-accounts","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! We have a custom application that adds some attributes to the Active Directory schema. These attributes have hyphens in their names, and I can&rsquo;t figure out how to retrieve the value of those attributes; any time I try to connect to one I get an error message. Any suggestions? &#8212; TW Hey, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69553","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=69553"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69553\/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=69553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}