Wednesday, August 21, 2013

Copy SMTP Address From Exchange Resource Forest–PowerShell Script

This PowerShell script which you can use to copy or update the primary SMTP address.  I am currently using this script to copy the primary SMTP address from a resource forest in to user objects in an account/identify forest.

It has two functions functions (1) Search and (2) Search and Update as shown in the following screenshot:

clip_image002[5]

In this script my account forest is Sivarajan.com and resource forest is ss-ts.com

Script:

#
# Search and update account forest (sivarajan.com) account with Primary SMTP Address
#
# Author - Santhosh Sivarajan
#
# Version 1.0
#

Clear
$N1 = 0
$N2 = 0
$SivaFile = New-Item -type file -force "E:\Scripts\Siva_NO_PSMTP.csv"
"samAccountName`t`tName" | Out-File $SivaFile -encoding ASCII
$OutputFile = New-Item -type file -force "E:\Scripts\PSMTP_Output.csv"
"samAccountName`t`tNamet`t$TPSMTP" | Out-File $SivaFile -encoding ASCII
$SivaSearchRoot = "LDAP://DC=Sivarajan,DC=com" #Account Forest
$SSTSSearchRoot = "LDAP://DC=SS-TS,DC=com" #Resource Forest

Function SearchnUpdate
{
Clear
$SObjFilter = "(&(objectCategory=person)(objectCategory=User)(!mail=*)(!userAccountControl:1.2.840.113556.1.4.803:=2))"
$SobjSearch = New-Object System.DirectoryServices.DirectorySearcher
$SobjSearch.PageSize = 15000
$SobjSearch.Filter = $SObjFilter 
$SobjSearch.SearchRoot = $SivaSearchRoot
Write-host "Searching Sivarajan.com and SS-TS.com Domains"
Write-host ""
Write-host "Found the following user accounts in Sivarajan.com Domain:"
Write-host ""
$SAllObj = $SobjSearch.FindAll()
    foreach ($SObj in $SAllObj)
    {   
        $TPSMTP = ""
        $Suser = [ADSI] $SObj.path
        $SobjItemT = $SObj.Properties
        $Ssamaccountname = $SobjItemT.samaccountname
        $TObjFilter = "(&(objectCategory=person)(objectCategory=User)(samaccountname=$Ssamaccountname)(mail=*))"
        $TobjSearch = New-Object System.DirectoryServices.DirectorySearcher
        $TobjSearch.PageSize = 15000
        $TobjSearch.Filter = $TObjFilter 
        $TobjSearch.SearchRoot = $SSTSSearchRoot
        $TAllObj = $TobjSearch.Findone()
        $Tuser = [ADSI] $TAllObj.path
        $TobjItem = $TAllObj.Properties
        $TPSMTP= $TobjItem.mail
        Write-host -NoNewLine "Sivarajan\$Ssamaccountname -> "
            If ($TPSMTP -eq $NULL)
                 {
                Write-host "No mailbox found in SSTS" -ForegroundColor Red
                }
            Else
                {
                Write-host "Updatig with" $TPSMTP -ForegroundColor Green
                $Suser.psbase.invokeSet("mail","$TPSMTP")
                $Suser.setinfo()
                "$Ssamaccountname`t`t$Sname`t`t$TPSMTP" | Out-File $OutputFile -encoding ASCII -Append
                $N1++
                }
    }
Write-host ""
Write-host "Total Number of obejcts updated $N1"
Write-host "Review $OutputFile for more details"
Write-host ""
}

Function Search
{
Clear
$SObjFilter = "(&(objectCategory=person)(objectCategory=User)(!mail=*)(!userAccountControl:1.2.840.113556.1.4.803:=2))"
$SobjSearch = New-Object System.DirectoryServices.DirectorySearcher
$SobjSearch.PageSize = 15000
$SobjSearch.Filter = $SObjFilter 
$SobjSearch.SearchRoot = $SivaSearchRoot
Write-host "Searching Sivarajan.com and SSTS.com Domains"
Write-host ""
Write-host "Found the following user accounts in Sivarajan.com Domain:"
Write-host ""
$SAllObj = $SobjSearch.FindAll()
    foreach ($SObj in $SAllObj)
    {   
        $TPSMTP = ""
        $Suser = [ADSI] $SObj.path
        $SobjItemT = $SObj.Properties
        $Ssamaccountname = $SobjItemT.samaccountname
        $Sname = $SobjItemT.name
        $TObjFilter = "(&(objectCategory=person)(objectCategory=User)(samaccountname=$Ssamaccountname)(mail=*))"
        $TobjSearch = New-Object System.DirectoryServices.DirectorySearcher
        $TobjSearch.PageSize = 15000
        $TobjSearch.Filter = $TObjFilter 
        $TobjSearch.SearchRoot = $SSTSSearchRoot
        $TAllObj = $TobjSearch.Findone()
        $Tuser = [ADSI] $TAllObj.path
        $TobjItem = $TAllObj.Properties
        $TPSMTP= $TobjItem.mail
        Write-host -NoNewLine "Sivarajan\$Ssamaccountname -> "
            If ($TPSMTP -eq $NULL)
                 {
                Write-host "No mailbox found in SSTS" -ForegroundColor Red
                $N2++
                }
            Else
                {
                Write-host "Mailbox Found ($TPSMTP) in SSTS" -ForegroundColor Green
                $N1++
                }
        "$Ssamaccountname`t`t$Sname`t`t$TPSMTP" | Out-File $SivaFile -encoding ASCII -Append


       
    }
Write-host ""
Write-host "Number of obejcts with no Email address in Sivarajan.com-> $N2"
Write-host "Number of obejcts with no Email address in Sivarajan.com and have mailbox in SSTS-> $N1"
Write-host "Review $SivaFile for more details"
Write-host ""
}

Write-Host "`t`tSelect 1 - Search" -ForegroundColor Red
Write-Host "`t`tSelect 2 - Seach and Update" -ForegroundColor Red
$Option = Read-Host

switch ($Option)
    {
        1 {Search}
     2 {SearchnUpdate}
    default {"Invalid Selection"}
    }

 

Output

You will see a real time status on the console as shown the in the following screenshot:

image

It also creates an output file - PSMTP_Output.csv"

 

________________________________________________________________________________

Migrating from Windows Server 2008 or Windows Server 2008 R2 to Windows Sever 2012?

Paperback - http://www.amazon.com/dp/1849687447/?tag=packtpubli-20

eBook - http://www.packtpub.com/migrating-from-2008-and-2008-r2-to-windows-server-2012/book

________________________________________________________________________________

0 comments:

Post a Comment

Popular Posts

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More