{"id":68363,"date":"2005-12-09T16:03:00","date_gmt":"2005-12-09T16:03:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/12\/09\/how-can-i-use-alternate-credentials-when-searching-active-directory\/"},"modified":"2005-12-09T16:03:00","modified_gmt":"2005-12-09T16:03:00","slug":"how-can-i-use-alternate-credentials-when-searching-active-directory","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-use-alternate-credentials-when-searching-active-directory\/","title":{"rendered":"How Can I Use Alternate Credentials When Searching Active Directory?"},"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! How can I use alternate credentials when searching Active Directory?<\/p>\n<p>&#8212; PT<\/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\"><img decoding=\"async\" height=\"288\" width=\"120\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" align=\"right\" alt=\"Script Center\" border=\"0\" title=\"Script Center\" class=\"farGraphic\" \/><\/a><\/p>\n<p>Hey, PT. So what&rsquo;s the big idea here? Devoted readers of this column (both of them) know that we usually <i>love<\/i> tackling questions where the answer involves searching Active Directory. Why? That&rsquo;s easy: because we don&rsquo;t really <i>answer<\/i> those questions. Instead we simply hammer out a script and then, instead of explaining how the script works, tell people to read the two-part series <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/tales\/sg0405.mspx\"><b>Dude, Where&rsquo;s My Printer?<\/b><\/a> for more information. As you can tell, that makes for a nice, easy day at the office.<\/p>\n<p>But your question is different: unfortunately for us, specifying alternate credentials isn&rsquo;t mentioned anywhere in our series on searching Active Directory. That means we&rsquo;re actually going to have to <i>answer<\/i> this question. Talk about unfair: not only do we have to come in to work, but now we&rsquo;re expected to actually do something while we&rsquo;re here! <\/p>\n<p>Trust us: that is <i>not<\/i> the American way.<\/p>\n<p>So we&rsquo;ll make a deal with you: we&rsquo;ll answer your question, but only by focusing on the part of the script where you specify alternate credentials. If you need more information about the rest of the code, or if you need more information about searching Active Directory, please see the two-part series <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/tales\/sg0405.mspx\"><b>Dude, Where&rsquo;s My Printer?<\/b><\/a><\/p>\n<p>Boy, if only we had a dollar for every time we&rsquo;ve said <i>that<\/i>.<\/p>\n<p>Here&rsquo;s a script that uses alternate credentials in order to search Active Directory:<\/p>\n<pre class=\"codeSample\">Const ADS_SCOPE_SUBTREE = 2<br \/><\/pre>\n<pre class=\"codeSample\">Set objConnection = CreateObject(\"ADODB.Connection\")<br \/>Set objCommand =   CreateObject(\"ADODB.Command\")<br \/>objConnection.Provider = \"ADsDSOObject\"<\/pre>\n<pre class=\"codeSample\"><br \/>objConnection.Properties(\"User ID\") = \"fabrikam\\kenmyer\"<br \/>objConnection.Properties(\"Password\") = \"A2sXrco1Fq1#om!\"<br \/>objConnection.Properties(\"Encrypt Password\") = TRUE<br \/>objConnection.Properties(\"ADSI Flag\") = 3<br \/><\/pre>\n<pre class=\"codeSample\">objConnection.Open \"Active Directory Provider\"<br \/>Set objCommand.ActiveConnection = objConnection<br \/><\/pre>\n<pre class=\"codeSample\">objCommand.Properties(\"Page Size\") = 1000<br \/>objCommand.Properties(\"Searchscope\") = ADS_SCOPE_SUBTREE<\/pre>\n<pre class=\"codeSample\"><br \/>objCommand.CommandText = _<br \/>    \"SELECT Name FROM 'LDAP:\/\/DC=fabrikam,DC=com' WHERE \" _<br \/>        &amp; \"objectCategory='user'\"  <br \/>Set objRecordSet = objCommand.Execute<\/pre>\n<pre class=\"codeSample\">&nbsp;<\/pre>\n<pre class=\"codeSample\">objRecordSet.MoveFirst<br \/>Do Until objRecordSet.EOF<br \/>    Wscript.Echo objRecordSet.Fields(\"Name\").Value<br \/>    objRecordSet.MoveNext<br \/>Loop<\/pre>\n<p>Like we said, for today we&rsquo;re going to focus on these four lines of code, the four lines where we specify the alternate credentials:<\/p>\n<pre class=\"codeSample\">objConnection.Properties(\"User ID\") = \"fabrikam\\kenmyer\"<br \/>objConnection.Properties(\"Password\") = \"A2sXrco1Fq1#om!\"<br \/>objConnection.Properties(\"Encrypt Password\") = TRUE<br \/>objConnection.Properties(\"ADSI Flag\") = 3<\/pre>\n<p>Note that these four lines of code are required <i>only<\/i> if you want to conduct the search under alternate credentials; that is, only if you want to bind to Active Directory using a user account other than the one you used when logging on to Windows. If you want to do a search using your current logon credentials all you have to do is remove these four lines of code and the script is good to go. <\/p>\n<p>As you can probably tell (the object reference objConnection is a dead giveaway), these four lines of code involve four properties of the ADO (ActiveX Data Objects) <b>Connection<\/b> object. The properties <b>User ID<\/b> and <b>Password<\/b> should be self-explanatory: these are simply the user name and the password for the account you want to use when binding to Active Directory. In this example, we&rsquo;ve specified the User ID using the <i>domain\\user name<\/i> syntax. However, we could also specify the user name as the logon name itself (e.g., <b>kenmyer<\/b>) or as the user&rsquo;s UPN (Universal Principal Name): <b>kenmyer@fabrikam.com<\/b>. That&rsquo;s entirely up to you. <\/p>\n<p>As for the password, we&rsquo;ve hard-coded the password into the script for educational purposes. Needless to say, however, that&rsquo;s not the way we recommend that you do things. Instead, you should probably have the script prompt you for a password each time the script is run. If you&rsquo;re not sure how to do that, well, don&rsquo;t worry about it: as we are wont to do, we&rsquo;ll simply refer you <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/feb05\/hey0204.mspx\"><b>somewhere else<\/b><\/a>. (In this case, a previous <i>Hey, Scripting Guy!<\/i> column on prompting for &#8211; and masking &#8211; passwords.)<\/p>\n<p>That leaves us with just two properties to dispose of. Setting <b>Encrypt Password<\/b> to True simply tells the script to encrypt the password when sending it across the network; by default, this value is set to False. The <b>ADSI Flag<\/b> property, meanwhile, is a bitmask property used to specify authentication options. The value 3 is actually a bitmask value composed of two separate properties:<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"ELF\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><b>Constant<\/b><\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\"><b>Value<\/b><\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\"><b>Description<\/b><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" class=\"evenRecord\">\n<td>\n<p class=\"lastInCell\">ADS_SECURE_AUTHENTICATION<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">1<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Requests secure authentication. When this flag is set, Active Directory will use Kerberos, and possibly NTLM, to authenticate the client.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\">ADS_USE_ENCRYPTION<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">2<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Requires ADSI to use encryption for data exchange over the network.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>You can find more information about the ADSI Flag property in the <a target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/adsi\/adsi\/ads_authentication_enum.asp\"><b>ADSI SDK<\/b><\/a>. <\/p>\n<p>That&rsquo;s really all you have to do: configure the appropriate values for those 4 properties and away you go. <\/p>\n<p>Now, if you&rsquo;ll excuse us, we need to take a break. After all, the last thing we expected to have to do at work today was work!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I use alternate credentials when searching Active Directory? &#8212; PT Hey, PT. So what&rsquo;s the big idea here? Devoted readers of this column (both of them) know that we usually love tackling questions where the answer involves searching Active Directory. Why? That&rsquo;s easy: because we don&rsquo;t really answer those questions. [&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,8,5],"class_list":["post-68363","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-searching-active-directory","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I use alternate credentials when searching Active Directory? &#8212; PT Hey, PT. So what&rsquo;s the big idea here? Devoted readers of this column (both of them) know that we usually love tackling questions where the answer involves searching Active Directory. Why? That&rsquo;s easy: because we don&rsquo;t really answer those questions. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68363","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=68363"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68363\/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=68363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}