{"id":71063,"date":"2004-11-05T16:12:00","date_gmt":"2004-11-05T16:12:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/11\/05\/how-can-i-work-with-a-cn-that-has-a-comma-in-it\/"},"modified":"2004-11-05T16:12:00","modified_gmt":"2004-11-05T16:12:00","slug":"how-can-i-work-with-a-cn-that-has-a-comma-in-it","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-work-with-a-cn-that-has-a-comma-in-it\/","title":{"rendered":"How Can I Work with a CN that Has a Comma in It?"},"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! My user accounts have commas in their CN attributes; for example, <b>Myer, Ken<\/b>. How do I bind to those user accounts using a script? I always get the error message &ldquo;An invalid dn syntax has been specified.&rdquo;<\/p>\n<p>&#8212; GT<\/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, GT. For those of you who don&rsquo;t know, the CN attribute is the attribute that uniquely identifies a user account within an OU or other Active Directory container. (You can have more than one <b>Myer, Ken<\/b> in your domain, but you can have only a single <b>Myer, Ken<\/b> in, say, the Accounting OU.) The CN attribute is also the attribute used to display user names in Active Directory Users and Computers. When you add a user from within Active Directory Users and Computers, one of the fields you typically fill out is labeled <b>Full Name<\/b> (and then, for some reason, when you later view the user account properties, this same field gets re-labeled <b>Display Name<\/b>). Administrators like to specify CNs (full names) using the last name, first name syntax; that way, when they look at an OU all the users are arranged in alphabetical order by last name. (If you specified <b>Ken Myer<\/b> as the full name, users will be arranged in alphabetical order by <i>first<\/i> name.)<\/p>\n<p>In other words, it&rsquo;s very common for users to have CNs similar to Myer, Ken. That&rsquo;s great when you&rsquo;re working in Active Directory Users and Computers, but not so great when you&rsquo;re trying to manage these users with ADSI scripts. For example, this script &#8211; which attempts to bind to the Myer, Ken user account &#8211; will fail with an invalid syntax error:<\/p>\n<pre class=\"codeSample\">Set objUser = GetObject(<a href=\"\/\/CN=Myer, Ken,OU=Accounting,DC=fabrikam,DC=com\">LDAP:\/\/CN=Myer, Ken,OU=Accounting,DC=fabrikam,DC=com<\/a>)<\/pre>\n<pre class=\"codeSample\">Wscript.Echo objUser.CN<\/pre>\n<p>So what&rsquo;s the problem here? Well, it&rsquo;s not with the user&rsquo;s CN; <b>Myer, Ken<\/b> is perfectly valid. The problem is that when you write a binding string in ADSI the comma is used to separate the individual values within the ADsPath. When we write <b>CN=Myer, Ken,OU=Accounting<\/b>, our script thinks <b>CN=Myer<\/b> is the first value, and <b>Ken<\/b> is the second value. That&rsquo;s because of the comma between Myer and Ken. And because <b>Ken<\/b> by itself doesn&rsquo;t make any sense within an ADsPath, the script blows up.<\/p>\n<p>So what do you about that? As it turns out, the comma is a reserved character when it comes to Active Directory paths. (Other reserved characters include the semicolon, the plus sign, the backslash, and the left and right angle brackets. For a complete list, see the <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/ldap\/ldap\/distinguished_names.asp\"><b>Lightweight Access Directory Protocol<\/b><\/a> documentation on MSDN.) <\/p>\n<p>For most reserved characters, you can use them in binding strings simply by placing a backslash (\\) in front of the characters. This script will successfully bind to the <b>Myer, Ken<\/b> user account and report back the user&rsquo;s CN:<\/p>\n<pre class=\"codeSample\">Set objUser = GetObject(<a href=\"\/\/CN=Myer\\, Ken,OU=Accounting,DC=fabrikam,DC=com\">LDAP:\/\/CN=Myer\\, Ken,OU=Accounting,DC=fabrikam,DC=com<\/a>)<\/pre>\n<pre class=\"codeSample\">Wscript.Echo objUser.CN<\/pre>\n<p>Again, note how the user CN is specified:<b> CN=Myer\\, Ken,OU<\/b>. After Myer we place a backslash and a comma, then we continue with the rest of the path (including the blank space that separates <b>Myer,<\/b> and <b>Ken<\/b>).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! My user accounts have commas in their CN attributes; for example, Myer, Ken. How do I bind to those user accounts using a script? I always get the error message &ldquo;An invalid dn syntax has been specified.&rdquo; &#8212; GT Hey, GT. For those of you who don&rsquo;t know, the CN attribute is [&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,5],"class_list":["post-71063","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! My user accounts have commas in their CN attributes; for example, Myer, Ken. How do I bind to those user accounts using a script? I always get the error message &ldquo;An invalid dn syntax has been specified.&rdquo; &#8212; GT Hey, GT. For those of you who don&rsquo;t know, the CN attribute is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71063","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=71063"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71063\/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=71063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}