Skip to main content

Cloud

PowerShell Add ProxyAddresses Script

I do a lot of email migration work so I’m always looking for new ways (and hopefully better ways) to do some common tasks. For example, I regularly have to add new aliases from the old mail system to the new mailboxes. Normally I would use the migration tool to perform this task, but once in a while you might not be able to do this for a variety of reasons. Nevertheless, you still need a way to quickly and safely do this.
I have used VBScripts to perform this task in the past but I wanted to find an easier way to do the same thing using PowerShell. So here’s an example of using PowerShell to parse through a CSV file, split up a string of SMTP addresses and add them to an existing mailbox. Oh, and did I mention that I’m also a big fan of looping through CSV files? 🙂
Here’s what our input file (AddProxy.csv) looks like:
Alias,TargetAlias
taccount1,taccount1@nwtraders.com%taccount1@nwtraders.org%taccount1@nwtraders.info
taccount2,taccount2@nwtraders.com%taccount2@nwtraders.org%taccount2@nwtraders.info
# ======================================================================
#
# NAME: addproxy.ps1
#
# AUTHOR: Erik Enger , PointBridge
# DATE : 3/08/2009
#
# COMMENT: Used to add proxyAddresses read in from a CSV file.
#
# Note: This script requires the Exchange Management Shell Snapin to function. Add this snapin to the Windows PowerShell default profile first.
#
# The c:windowssystem32windowspowershellv1.0profile.ps1 file should contain this line:
#
# add-pssnapin Microsoft.Exchange.Management.PowerShell.Admin
#
# ======================================================================
Write-Host "Beginning proxyAddress addition…" -foregroundcolor red -BackgroundColor darkmagenta
Write-Host `n
Import-CSV "c:migrationAddProxy.csv" | ForEach {
## Start a Transcript
$file="C:migrationScriptLogs"
$file+= $_.DisplayName +"-addproxy.log"
Start-Transcript $file -append

# Read in TargetAlias field from import file
$newproxy=$_.TargetAlias.Split(‘%’)

# Connect to Exchange mailbox
$user=Get-Mailbox -Identity $_.Alias -DomainController dc1.contoso.com
Write-Host "Processing user " $user.Name -foregroundcolor yellow -BackgroundColor darkmagenta

# List existing proxyAddresses
Write-Host "Existing proxyAddresses:" $user.EmailAddresses
Write-Host `n

# Begin looping through import file and adding any proxyAddresses that are missing
for($i=0;$i -le $newproxy.Count-1;$i++)
{
$newaddr=$newproxy[$i]
if ($user.EmailAddresses -notcontains $newaddr)
{
Write-Host "Adding new proxyAddress: " $newaddr
$user.EmailAddresses.Add("smtp:"+$newaddr)}
else
{Write-Host "Duplicate SMTP address found (" $newaddr ") Skipping addition…"}
}

Set-Mailbox -Instance $user -DomainController dc1.contoso.com
# Reset variables
$newaddr=""
Write-Host `n
### Stop the log for the current object
Date
Stop-Transcript
}
Write-Host "ProxyAddress additions complete." -foregroundcolor red -BackgroundColor darkmagenta
Write-Host `n`n`n
Running this will add any new SMTP addresses found in the import file to the respective user accounts. If an existing value is found it will be skipped. You could also reconfigure this script to remove addresses too. The "transcript" function captures the PowerShell console output and stores it in a log file for each user.
*************************
I’m adding this example of a VBScript that performs a similar function. It doesn’t have all the same logic as the PowerShell version the primary purpose was to add a new SMTP address to an existing list of proxyAddresses.
Hi. Here’s a sample VBScript I used before that read data from a spreadsheet and appended an SMTP address to the current list of proxyAddresses. While it’s not exactly the same method I’m using for the PowerShell script, it should show a comparison of the coding differences. I hope this helps.
‘==========================================================================

‘ VBScript Source File — Created with SAPIEN Technologies PrimalScript 4.0

‘ NAME: Add proxy address(es) to existing mailbox enable objects

‘ AUTHOR: Erik Enger, PointBridge, LLC
‘ DATE : 9/2/2005

‘ COMMENT: This script is designed to add proxy addresses from a spreasdsheet
‘ to an existing mailbox enabled object

‘==========================================================================
‘ Explicitly declaring 3 types of variables
Option Explicit
‘ Objects declared :
Dim objUser, objContainer
Dim objExcel, objSheet, objRootLDAP
‘ Strings declared :
Dim strOUContainer, strDN, strObjectClass
Dim strPathExcel, strPath, strObjCN
Dim strDomain, strOU, strProxy, strMailbox, strNick
Dim arrProxyAddresses
‘ Integers declared :
Dim intNumusers, intRunError, intRow, intCol
‘ Where is your spreadsheet?
‘ PLEASE ALTER Path to your spreadsheet
strPathExcel = "d:migrationscriptsupdate-metadir.xls"
‘ Connect and Open the Spreadsheet
‘ Connect to spreadsheet where users are stored
Set objExcel = CreateObject("Excel.Application")
‘ Open the Speadsheet (Error Handling Section).
On Error Resume Next
Err.Clear
objExcel.Workbooks.Open strPathExcel
If Err.Number <> 0 Then
Err.Clear
On Error GoTo 0
WScript.Echo "Edit the path to YOUR spreadsheet " & strPathExcel
Wscript.Quit
End If
On Error GoTo 0
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
‘ DO … LOOP Until Reading the spreadsheet
‘ Second row in spreadsheet has attributes so start at row 3
‘ Check intRow offset numbers
intRow = 2
‘ Do – Read each row in spreadsheet until reach a blank row
‘ For each row, create user and set attribute values.
‘ Loop until Empty cell
Do
‘ Read values from spreadsheet for this user.
strDN = Trim(objSheet.Cells(intRow, 1).Value)
strProxy = Trim(objSheet.Cells(intRow, 7).Value)
On Error Resume Next
‘ Section to bind to Active Directory
Set objRootLDAP = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://" & strOU _
& objRootLDAP.Get("DefaultNamingContext"))
‘ Modify attributes of mailboxes
Set objUser = GetObject("LDAP://"& strDN & ",ou=migrated users,dc=contoso,dc=com")
‘ Save the user’s proxy addresses to an Array
arrProxyAddresses = objUser.proxyAddresses
WScript.Echo "Adding Uniform proxy address: " & strProxy & " to: " & strDN
WScript.Echo
‘ Extend the array and insert the proxy address
ReDim Preserve arrProxyAddresses(UBound(arrProxyAddresses) + 1)
arrProxyAddresses(UBound(arrProxyAddresses)) = "smtp:" & strProxy
‘ Clear the existing list of proxy addresses from the user object
objUser.Put "proxyAddresses", ""
‘ Assign the new list of proxy addresses to the user object
objUser.Put "proxyAddresses", arrProxyAddresses
‘Commit changes to AD
objUser.SetInfo
‘ Increment to next user.
intRow = intRow + 1
intNumusers = intNumusers + 1
Loop Until objSheet.Cells(intRow, 1).Value = ""
‘ Echo confirmation Message
WScript.Echo intNumusers & " proxy addresses added."
‘ Tidy up Reset objects to nothing
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
Set objUser = Nothing
Set objContainer = Nothing
Set objSheet = Nothing
Set objExcel = Nothing
Set objRootLDAP = Nothing

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

PointBridge Blogs

More from this Author

Follow Us