All you need to do is update the LDAP query filter.

You can see what you need to do for that, here:

http://theessentialexchange.com/blogs/michael/archive/2012/01/17/sending-an-email-to-users-whose-password-is-about-to-expire-a-powershell-rewrite.aspx


From: Heaton, Joseph@Wildlife [mailto:joseph.hea...@wildlife.ca.gov]
Sent: Monday, April 22, 2013 11:29 AM
To: NT System Admin Issues
Subject: RE: Need to create a csv through Powershell/Exchange Shell

Michael,

That worked magnificently.  Thank you.  I would prefer the "teach to fish" 
approach as well, but I really appreciate the help.  There's some tweaking that 
I'd like to do to it, to filter out disabled accounts, but I'll just play with 
it on my own.

Joe Heaton
Enterprise Server Support
CA Department of Fish and Wildlife
1807 13th Street, Suite 201
Sacramento, CA  95811
Desk:  (916) 323-1284

From: Michael B. Smith [mailto:mich...@smithcons.com]
Sent: Friday, April 19, 2013 2:50 PM
To: Heaton, Joseph@Wildlife; NT System Admin Issues
Subject: RE: Need to create a csv through Powershell/Exchange Shell

I GREATLY prefer the "teach a man to fish" paradigm, but to do this one 
properly requires some fairly in-depth knowledge of the AD PowerShell module 
and how Exchange stores information in AD. To do it efficiently needs an LDAP 
search and using a filter in PowerShell.

So that it can be exported properly, the output needs to be simple objects (not 
complex), but not just strings.

Oh, and since it can't be guaranteed that FirstName, LastName are unique, it 
also includes the sAMAccountName (unique in any given domain).

So I wrote this one. And will blog it. :P I had the various pieces already 
(yay, for code re-use!).

Adjust $secondaryDomain as a parameter, or in the source; as meets your needs.

You can Export-Csv or Export-CliXML to your heart's content. :)

Param(
                [string]$secondaryDomain = "@TheEssentialExchange.com",
                [int]$secondaryDomainLen = $secondaryDomain.Length
)

filter strip-Addresses
{
                $proxies = $_.proxyAddresses

                $primary   = ""
                $secondary = ""

                $object = "" | Select GivenName, Surname, sAMAccountName, 
PrimarySmtp, SecondarySmtp

                $object.GivenName      = $_.GivenName
                $object.SurName        = $_.SurName
                $object.sAMAccountName = $_.sAMAccountName

                foreach( $proxy in $proxies )
                {
                                $len = $proxy.Length

                                ## note: "SMTP:".Length == 5

                                ## note: The primary SMTP address has a 
CAPITALIZED "SMTP:" prefix
                                ## all secondary SMTP addresses have a 
lowercase "smtp:" prefix

                                ## note: any interesting secondary proxy 
address will be longer than
                                ## "SMTP:".Length + $secondaryDomainLen

                                if( $len -gt 5 )
                                {
                                                $prefix = $proxy.SubString( 0, 
5 )
                                                $temp   = $proxy.SubString( 5 ) 
 ##strip off "smtp:", if present

                                                if( $prefix -ceq "SMTP:" )
                                                {
                                                                $primary = $temp
                                                                if( 
$secondary.Length -gt 0 )
                                                                {
                                                                                
break   ## we have both primary and secondary,
                                                                                
                ## we don't need to look any more
                                                                }
                                                }
                                                elseif( $prefix -ceq "smtp:" 
-and $len -gt ( 5 + $secondaryDomainLen ) )
                                                {
                                                                if( 
$temp.EndsWith( $secondaryDomain ) )
                                                                {
                                                                                
$secondary = $temp
                                                                                
if( $primary.Length -gt 0 )
                                                                                
{
                                                                                
                break   ## we have both primary and secondary,
                                                                                
                                ## we don't need to look any more
                                                                                
}
                                                                }
                                                }
                                }
                }

                $object.PrimarySmtp   = $primary
                $object.SecondarySmtp = $secondary

                $object
}

Import-Module ActiveDirectory

Get-AdUser -LDAPFilter "(&(objectCategory=user)(proxyAddresses=*))" `
                -Properties GivenName, SurName, proxyAddresses -ResultSetSize 
$null |
                strip-Addresses

From: Heaton, Joseph@Wildlife [mailto:joseph.hea...@wildlife.ca.gov]
Sent: Friday, April 19, 2013 4:32 PM
To: NT System Admin Issues
Subject: RE: Need to create a csv through Powershell/Exchange Shell

Don,

That helps with that part, but here's what I'm trying to get to:

Primary SMTP address, First Name, Last Name, Secondary smtp address

In one report/csv/whatever.


Joe Heaton
Enterprise Server Support
CA Department of Fish and Wildlife
1807 13th Street, Suite 201
Sacramento, CA  95811
Desk:  (916) 323-1284

~ Finally, powerful endpoint security that ISN'T a resource hog! ~
~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/>  ~

---
To manage subscriptions click here: 
http://lyris.sunbelt-software.com/read/my_forums/
or send an email to 
listmana...@lyris.sunbeltsoftware.com<mailto:listmana...@lyris.sunbeltsoftware.com>
with the body: unsubscribe ntsysadmin

~ Finally, powerful endpoint security that ISN'T a resource hog! ~
~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/>  ~

---
To manage subscriptions click here: 
http://lyris.sunbelt-software.com/read/my_forums/
or send an email to 
listmana...@lyris.sunbeltsoftware.com<mailto:listmana...@lyris.sunbeltsoftware.com>
with the body: unsubscribe ntsysadmin

~ Finally, powerful endpoint security that ISN'T a resource hog! ~
~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/>  ~

---
To manage subscriptions click here: 
http://lyris.sunbelt-software.com/read/my_forums/
or send an email to listmana...@lyris.sunbeltsoftware.com
with the body: unsubscribe ntsysadmin

Reply via email to