{"id":67703,"date":"2006-03-22T08:06:00","date_gmt":"2006-03-22T08:06:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/03\/22\/how-can-i-get-a-list-of-all-the-users-who-have-an-alternate-recipient\/"},"modified":"2006-03-22T08:06:00","modified_gmt":"2006-03-22T08:06:00","slug":"how-can-i-get-a-list-of-all-the-users-who-have-an-alternate-recipient","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-a-list-of-all-the-users-who-have-an-alternate-recipient\/","title":{"rendered":"How Can I Get a List of All the Users Who Have an Alternate Recipient?"},"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 get a list of all the users who have an alternate recipient (altRecipient)?<BR><BR>&#8212; SA<\/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, SA. You know, one of the great things about being a Scripting Guy is that we don\u2019t ever have to know what we\u2019re talking about; all we do have to know is how to write a script that does whatever it is you ask us to do and everyone\u2019s happy. Case in point: retrieving a list of all the users who have an alternate recipient. To be honest, we have only the vaguest notion of what an alternate recipient is, believing that this represents an alternate location for forwarding Microsoft Exchange email addresses. That\u2019s all we know, and, like we said, we aren\u2019t even sure <I>that\u2019s<\/I> correct. But that\u2019s OK; after all, everyone will forgive us our ignorance as long as we can show you a script that will return a list of all the users who have an alternate recipient:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const ADS_SCOPE_SUBTREE = 2<\/p>\n<p>Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)\nSet objCommand =   CreateObject(&#8220;ADODB.Command&#8221;)\nobjConnection.Provider = &#8220;ADsDSOObject&#8221;\nobjConnection.Open &#8220;Active Directory Provider&#8221;\nSet objCommand.ActiveConnection = objConnection<\/p>\n<p>objCommand.Properties(&#8220;Page Size&#8221;) = 1000\nobjCommand.Properties(&#8220;Searchscope&#8221;) = ADS_SCOPE_SUBTREE <\/p>\n<p>objCommand.CommandText = _\n    &#8220;SELECT Name, altRecipient FROM &#8216;LDAP:\/\/dc=fabrikam,dc=com&#8217; WHERE objectCategory=&#8217;user&#8217; &#8221; &amp; _\n        &#8220;AND altRecipient=&#8217;*'&#8221; \nSet objRecordSet = objCommand.Execute<\/p>\n<p>objRecordSet.MoveFirst<\/p>\n<p>Do Until objRecordSet.EOF\n    Wscript.Echo objRecordSet.Fields(&#8220;Name&#8221;).Value, objRecordSet.Fields(&#8220;altRecipient&#8221;).Value\n    objRecordSet.MoveNext\nLoop\n<\/PRE>\n<P>As you can see, this script simply searches Active Directory, ferreting out all the users who have an alternate recipient. As usual, we won\u2019t discuss the ins and outs of searching Active Directory; that goes beyond what we can do in this column. But don\u2019t worry: we would never leave you hanging like that, wondering how an Active Directory search script works. If you need (or want) more information on the principles behind Active Directory search scripts, take a look at our two-part <I>Tales from the Script<\/I> series <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/tales\/sg0405.mspx\"><B>Dude: Where\u2019s My Printer?<\/B><\/A><\/P>\n<P>No, thank <I>you<\/I>. After all, you guys let us get away with writing a column that doesn\u2019t actually explain anything but instead just refers you to some other column if you actually want to know what\u2019s going on! <I>(Editor\u2019s Note: This statement implies that you have a choice in the matter. In case you were wondering &#8211; you don\u2019t. And yes, in case you were wondering about that too, the Scripting Editor <\/I>has <I>had a long day today.)<\/I><\/P>\n<P>What we\u2019ll focus on today is the actual query that returns the list of users who have an alternate recipient. As SA noted, the Active Directory attribute name for an alternate recipient is <B>altRecipient<\/B>; to get a list of users who have an alternate recipient what we need to do is search for all the user accounts where the altRecipient attribute is set to something. That\u2019s exactly what this query does:<\/P>\n<P>objCommand.CommandText = _ &#8220;SELECT Name, altRecipient FROM &#8216;LDAP:\/\/dc=fabrikam,dc=com&#8217; WHERE objectCategory=&#8217;user&#8217; &#8221; &amp; _ &#8220;AND altRecipient=&#8217;*'&#8221; <\/P>\n<P>What we\u2019re doing here is retrieving two attribute values &#8211; <B>Name<\/B> and <B>altRecipient<\/B> &#8211; from the fabrikam.com domain. More importantly, we\u2019re retrieving those values only for Active Directory objects that meet both of the following criteria:<\/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 <B>objectCategory<\/B> is equal to <I>user<\/I>. This ensures that we get back only user accounts that have an alternate recipient. Could other objects even <I>have<\/I> alternate recipients? Well, we\u2019re not sure, so we decided not to take any chances and eliminate the possibility of getting back anything <I>but<\/I> user accounts.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The value of altRecipient is equal to something. To indicate this we use wildcard syntax: <B>altRecipient=\u2019*\u2019<\/B>. As you might have guessed, when searching Active Directory the asterisk represents any character or set or characters. By using an asterisk all by itself we\u2019re asking the script to return all the user accounts where altRecipient is equal to something; we don\u2019t care what the value is, just as long as it\u2019s <I>something<\/I>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>You know, that\u2019s a good question. If the asterisk means \u201cShow us all the user accounts where altRecipient is equal to something,\u201d doesn\u2019t that mean we\u2019ll get back <I>all<\/I> the user accounts, even those that don\u2019t have an alternate recipient? As the Scripting Guys have told you many times before, just because something is blank that doesn\u2019t mean that it doesn\u2019t have a value. <\/P>\n<P>Well, to tell you the truth, we tell people a lot of things; you can\u2019t expect us to keep track of <I>everything<\/I> we say, can you? But you\u2019re right: if a user has a blank altRecipient attribute value then he or she will show up in the search. Hopefully, though, you don\u2019t have any users with blank altRecipient attributes; instead, users who do not have an alternate recipient should have a <I>Null<\/I> altRecipient value (which happens to be the default value). Unlike a blank space or an empty string (\u201c\u201d), a Null value truly means <I>no value whatsoever<\/I>; users with a Null altRecipient value will not show up in our search results.<\/P>\n<TABLE id=\"EIF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. No, we won\u2019t leave you hanging here, either. For more information on setting Active Directory attribute values to Null take a look at <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/mar05\/hey0323.mspx\"><B>this much-beloved <\/B><B><I>Hey, Scripting Guy! <\/I><\/B><B>column<\/B><\/A>.<\/P>\n<P>Good point: all <I>Hey, Scripting Guy!<\/I> columns <I>are<\/I> much-beloved, aren\u2019t they?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After executing our query we get back a recordset consisting of all the users who have an alternate recipient. All we have to do then is set up a Do Until loop that loops through that collection, echoing back the values of the Name and altRecipient attributes:<\/P><PRE class=\"codeSample\">Do Until objRecordSet.EOF\n    Wscript.Echo objRecordSet.Fields(&#8220;Name&#8221;).Value, objRecordSet.Fields(&#8220;altRecipient&#8221;).Value\n    objRecordSet.MoveNext\nLoop\n<\/PRE>\n<P>It\u2019s that simple. (Just imagine all the cool things we Scripting Guys could show you if we actually knew what we were doing!)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I get a list of all the users who have an alternate recipient (altRecipient)?&#8212; SA Hey, SA. You know, one of the great things about being a Scripting Guy is that we don\u2019t ever have to know what we\u2019re talking about; all we do have to know is how to [&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,19,3,8,198,5],"class_list":["post-67703","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-activex-data-objects-ado","tag-scripting-guy","tag-searching-active-directory","tag-users","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I get a list of all the users who have an alternate recipient (altRecipient)?&#8212; SA Hey, SA. You know, one of the great things about being a Scripting Guy is that we don\u2019t ever have to know what we\u2019re talking about; all we do have to know is how to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67703","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=67703"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67703\/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=67703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}