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:[email protected]]
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 [email protected]
with the body: unsubscribe ntsysadmin