How Can I Get a List of All the Users Who Have an Alternate Recipient?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I get a list of all the users who have an alternate recipient (altRecipient)?

— SA

SpacerHey, Scripting Guy! AnswerScript Center

Hey, SA. You know, one of the great things about being a Scripting Guy is that we don’t ever have to know what we’re 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’s 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’s all we know, and, like we said, we aren’t even sure that’s correct. But that’s 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:

On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject(“ADODB.Connection”) Set objCommand = CreateObject(“ADODB.Command”) objConnection.Provider = “ADsDSOObject” objConnection.Open “Active Directory Provider” Set objCommand.ActiveConnection = objConnection

objCommand.Properties(“Page Size”) = 1000 objCommand.Properties(“Searchscope”) = ADS_SCOPE_SUBTREE

objCommand.CommandText = _ “SELECT Name, altRecipient FROM ‘LDAP://dc=fabrikam,dc=com’ WHERE objectCategory=’user’ ” & _ “AND altRecipient=’*'” Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF Wscript.Echo objRecordSet.Fields(“Name”).Value, objRecordSet.Fields(“altRecipient”).Value objRecordSet.MoveNext Loop

As you can see, this script simply searches Active Directory, ferreting out all the users who have an alternate recipient. As usual, we won’t discuss the ins and outs of searching Active Directory; that goes beyond what we can do in this column. But don’t 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 Tales from the Script series Dude: Where’s My Printer?

No, thank you. After all, you guys let us get away with writing a column that doesn’t actually explain anything but instead just refers you to some other column if you actually want to know what’s going on! (Editor’s Note: This statement implies that you have a choice in the matter. In case you were wondering – you don’t. And yes, in case you were wondering about that too, the Scripting Editor has had a long day today.)

What we’ll 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 altRecipient; 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’s exactly what this query does:

objCommand.CommandText = _ “SELECT Name, altRecipient FROM ‘LDAP://dc=fabrikam,dc=com’ WHERE objectCategory=’user’ ” & _ “AND altRecipient=’*'”

What we’re doing here is retrieving two attribute values – Name and altRecipient – from the fabrikam.com domain. More importantly, we’re retrieving those values only for Active Directory objects that meet both of the following criteria:

The objectCategory is equal to user. This ensures that we get back only user accounts that have an alternate recipient. Could other objects even have alternate recipients? Well, we’re not sure, so we decided not to take any chances and eliminate the possibility of getting back anything but user accounts.

The value of altRecipient is equal to something. To indicate this we use wildcard syntax: altRecipient=’*’. 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’re asking the script to return all the user accounts where altRecipient is equal to something; we don’t care what the value is, just as long as it’s something.

You know, that’s a good question. If the asterisk means “Show us all the user accounts where altRecipient is equal to something,” doesn’t that mean we’ll get back all the user accounts, even those that don’t have an alternate recipient? As the Scripting Guys have told you many times before, just because something is blank that doesn’t mean that it doesn’t have a value.

Well, to tell you the truth, we tell people a lot of things; you can’t expect us to keep track of everything we say, can you? But you’re right: if a user has a blank altRecipient attribute value then he or she will show up in the search. Hopefully, though, you don’t have any users with blank altRecipient attributes; instead, users who do not have an alternate recipient should have a Null altRecipient value (which happens to be the default value). Unlike a blank space or an empty string (“”), a Null value truly means no value whatsoever; users with a Null altRecipient value will not show up in our search results.

Note. No, we won’t leave you hanging here, either. For more information on setting Active Directory attribute values to Null take a look at this much-beloved Hey, Scripting Guy! column.

Good point: all Hey, Scripting Guy! columns are much-beloved, aren’t they?

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:

Do Until objRecordSet.EOF
    Wscript.Echo objRecordSet.Fields(“Name”).Value, objRecordSet.Fields(“altRecipient”).Value
    objRecordSet.MoveNext
Loop

It’s that simple. (Just imagine all the cool things we Scripting Guys could show you if we actually knew what we were doing!)

0 comments

Discussion is closed.

Feedback usabilla icon