Friday, April 06, 2012

Powershell script to enumerate SMTP addreses and alieses

I used the script to collect the information from Exchange 2003 for migration to Exchange 2010, but it can be used for collecting the information from Exchange 2010 (but it can be easy done with Poweshell snapin for Exchange). For setup PowerShell environment for Exchange 2010 you can try my blog page powershell-ms-exchange-part1. 
To view the script click on "Read More"

 #        Name:  AD-seacher.ps1
#      Author:  Boris Sirotin   -
        Date:  03/06/2012
# Description:  Exchange 2003 enumeration of SMTP addresses and aliases and
#               created CSV file with headers:
#        Logon Name,Other Name,Mail Address
# Log Name - is AD logon name, Other name - we will use it if no Log name found
# for example if the object is Contact, Mail Address - SMTP address

#create a CSV log file with file name format like 2012-03-06T14 year-mounth-day-time of the day (hours)
Function do-LogFile ($myLogPath){
    if (!(Test-Path $myLogPath)){New-Item -type directory -path $myLogPath}
    $script:myLogFile=$myLogPath + "\SMTP-addr-" +$myLogDate + ".csv"
    New-Item -type file -path $myLogFile -Force

    $myHeader="Logon Name,Other Name,Mail address"
    add-content -path $myLogFile -Value $myHeader
do-logFile "c:\temp"
# the most tricky part Searching of AD ref: -

$strFilter = "(&(objectCategory=User))"

$objDomain = New-Object System.DirectoryServices.DirectoryEntry

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"

# you can get different attributes from ADSI manager, if you want to enumerate different
# objectCategory
$colProplist = "sAMAccountName", "name", "proxyAddresses"

foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()

foreach ($objResult in $colResults) {
     $objItem = $objResult.Properties
     $mySAMname=$objItem.samaccountname   #case aware you need to type in low case
     $myName=$                #case aware you need to type in low case
     $myAddresses=$objItem.proxyaddresses #case aware you need to type in low case
     foreach ($addr in $myAddresses) {
        if ($myAddresses.Count -ne $NULL) {
            if ($addr -notlike "X400*") {
                [string]$strAddr=$addr # convert object to string to avoid extra cartridge return in output
                if (!$mySamName) {$strSamName="None"} #put None in CSV file if no Logon name.
                $mycontent=$strSAMname + "," + $strName + "," + $strAddr
                add-content -path $myLogFile -Value $myContent

No comments:

Post a Comment