Wednesday, August 21, 2013

Home /PowerShell /script / Copy SMTP Address From Exchange Resource Forest–PowerShell Script

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

________________________________________________________________________________

SS

Santhosh Sivarajan

Microsoft MVP · Identity & Cybersecurity Architect

Santhosh has 30+ years of hands-on enterprise experience in Identity and Access Management, Microsoft Entra ID, Active Directory, Microsoft 365 and Zero Trust architecture. He is the author of two books on Windows Server and security, and leads consulting, assessments and training at SAG Business Group.

1 comments:

You are so good at content creating! I love it. How are you doing now, friend?

Post a Comment

Popular Posts
Workstation Trust Relationship Issue Issue: You receive the following error message, when you try to login to the domain.  The security database on the server does not have ... ADMT Service Account - Permission and Configuration The ADMT service account needs to have proper permission in source and target domains.  You don’t need to use 2 separate accounts.  You can ... My First Peek into Microsoft Exchange 2010 By Santhosh Sivarajan Before I really dive into Exchange 2010, I thought I would install and play with it first. I took some screen shots and notes during the ins... ObjectSID and Active Directory What is an objectSID in Active Directory? When a new object is created in Active Directory, Domain Controller assigns a unique value used ... AD Group Report - List Group Members in Active Directory–PowerShell Script Updated Script - http://portal.sivarajan.com/2011/10/search-ad-collect-local-admin-group.html Script #1 This script... Add Users to a Group–PowerShell Script Purpose – Add users to a group from an input file – PowerShell V2 Script.  Input file – Input file (Users.csv) contains samAccountName in... User Account Migration and Merging – Part I (ADMT) Part I - User Account Migration and Merging Using ADMT Part II - User Account Migration and Merging Using QMM pre-creating user account ... User Account Migration and Merging – Part II (Quest Migration Manager) Part I - User Account Migration and Merging Using ADMT Part II - User Account Migration and Merging Using QMM Pre-creating user account in... Delete Stale or Inactive Computer Accounts from Active Directory Here is an easy way to identify and delete inactive or stale computers in an Active Directory environment.  Using the dsquery command you c... Converting PowerShell (PS1) to EXE / Standalone Application As we know, there many applications available to convert a PowerShell file to a standalone executable file.  Based on my experience, PowerSh...