Skip to main content

Exchange proxyaddress display and removal powershell gui for Exchange 2003,2007,2010

ProxyAddresses are something in Exchange environments that can be a challenge to maintain over a long period of time and also during migrations or consolidations something that you need to deal with. If your lucky then just modifying your recipient policies may work the magic that you need if not and you have a large number of users that you need to modify you need to look at creating a script or doing a lot of mouse work.

The first challenge if your going to write a script to do this is because your going to be deleting information just doing a one-liner or simple script may mean you will end up removing more information than you want. So one way to tackle this is using a simple GUI script that will allow mutiple selects to at least control what and which accounts you are going to affect. The good thing about createing a simple Gui is that it can also be used to do some analysis first. So the script i've created allows you to search based on the proxy address domain you want to look at and also limit what branch of your domain you want to query eg the whole domain, OU or subtree OU query. It also has the ability to export the search results.

A few other things about this script is that it uses ADSI so should work against any version of Exchange. It uses Putex so it should only affect one entry in the proxyaddresses array (hopefully the one your targeting to delete) which is hopefully a little safer then some approachs simular scrisst take of rewriting the whole proxyaddresses object which if it goes wrong can get ugly.

The ldap query the script uses does a wildcard smtp* + the domain you want to search so it will match a full or partial match. There is also some code to stop you deleting any proxyaddresses that may be the primary email address which should be avoided.

To use the script you need to put the domain you want to search for in the format @proxyaddress.com and then click search. This should find any accounts with that paricular proxy addresses. To delete specific proxy address you need to select the row fully and click the delete button. The script is latched by default so will prompt on every deletetion there is a checkbox to disable this (but do so with great care).

Note this script if for demostration purposes only and has had no real testing so is only sutible for test enviroments. I've put a download of the script here

The script itself looks like

[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$form = new-object System.Windows.Forms.form

function SearchforProxy(){
if ($pnProxyAddress.Text -match "@"){
$msTable.clear()
$root = [ADSI]'LDAP://RootDSE'
if ($rbSearchDWide.Checked -eq $true){
$dfDefaultRootPath = "LDAP://" + $root.DefaultNamingContext.tostring()
}
else{
$dfDefaultRootPath = "LDAP://" + $ouOUNameDrop.SelectedItem.ToString()
}
$dfRoot = [ADSI]$dfDefaultRootPath
$gfGALQueryFilter = "(&(&(&(&(!mailnickname=systemmailbox*)(objectCategory=person)(objectClass=user)(proxyAddresses=smtp:*" + $pnProxyAddress.Text + ")))))"
$dfsearcher = new-object System.DirectoryServices.DirectorySearcher($dfRoot)
if($ouOUCheckBox.Checked -eq $false -band $rbSearchDWide.Checked -eq $false){$dfsearcher.SearchScope = "OneLevel"}
$dfsearcher.Filter = $gfGALQueryFilter
$srSearchResult = $dfsearcher.FindAll()
foreach ($emResult in $srSearchResult) {
$unUserobject = New-Object System.DirectoryServices.directoryentry
$unUserobject = $emResult.GetDirectoryEntry()
$address = ""
$userName = ""
$mail = ""
$prxfound = ""
foreach($prxaddress in $unUserobject.ProxyAddresses){
$sstring = "*" + $pnProxyAddress.Text
if ($prxaddress -like $sstring){
if($prxfound -eq ""){$prxfound = $prxaddress}
else{$prxfound = $prxfound + ";" + $prxaddress}

}
if ($address -eq ""){$address = $prxaddress}
else{$address = $address + ";" + $prxaddress}
}
if ($unUserobject.Mail -ne $null){$mail = $unUserobject.Mail.ToString()}
$msTable.Rows.Add($unUserobject.SamaccountName.ToString(),$mail,$prxfound,$unUserobject.psbase.Parent.Name.ToString(),$address,$unUserobject.distinguishedName.ToString())

}
$dgDataGrid.DataSource = $msTable
}
else{
$msgstring = "Address format incorrect it must contain @ sign"
$a = new-object -comobject wscript.shell
$b = $a.popup($msgstring,0,"Warning",1)

}
}

function ExportGrid(){
$exFileName = new-object System.Windows.Forms.saveFileDialog
$exFileName.DefaultExt = "csv"
$exFileName.Filter = "csv files (*.csv)|*.csv"
$exFileName.InitialDirectory = "c:\temp"
$exFileName.ShowHelp = $true
$exFileName.ShowDialog()
if ($exFileName.FileName -ne ""){
$logfile = new-object IO.StreamWriter($exFileName.FileName,$true)
$logfile.WriteLine("UserName,Primary-EmailAddress,ProxyAddres-Found,OU,ProxyAddresses,distinguishedName")
foreach($row in $msTable.Rows){
$logfile.WriteLine("`"" + $row[0].ToString() + "`"," + $row[1].ToString() + "," + $row[2].ToString() + "," + $row[3].ToString() + "," + $row[4].ToString()+ $row[5].ToString())
}
$logfile.Close()
}

}

function DeletedSelected(){
if ($dgDataGrid.SelectedRows.Count -eq 0){
$msgstring = "Now Rows Selected"
$a = new-object -comobject wscript.shell
$b = $a.popup($msgstring,0,"Warning",1)
}
else{
$buttons=[system.windows.forms.messageboxbuttons]::yesno
$return = ""
$return = [system.windows.forms.messagebox]::Show("This will remove the Selected Proxy Addresses Click Yes to Procced","",$buttons)
if ($return -eq [Windows.Forms.DialogResult]::Yes){
$lcLoopCount = 0
while ($lcLoopCount -le ($dgDataGrid.SelectedRows.Count-1)) {

$UserDN = "LDAP://" + $dgDataGrid.SelectedRows[$lcLoopCount].Cells[5].Value
$userObject = [ADSI]$UserDN
$sstring = "*" + $pnProxyAddress.Text
foreach($prxaddress in $userObject.ProxyAddresses){
if ($prxaddress -like $sstring){
if($prxaddress -cmatch "SMTP:") {
$Message = "Not Deleteing " + $prxaddress + " as this is the primary SMTP address"
[system.windows.forms.messagebox]::Show($Message)
}
else{
$Message = "Deleteing the following proxy Address's`r`n`r`n" + $prxaddress + "`r`n`r`nFrom " + $userObject.DisplayName.ToString()
$mreturn = ""
if ($cmfCheckBox.Checked -eq $true){
$mreturn = [system.windows.forms.messagebox]::Show($Message,"",$buttons)
if ($mreturn -eq [Windows.Forms.DialogResult]::Yes){
$userObject.PutEx(4, 'proxyAddresses', @("$prxaddress"))
$userObject.setinfo()
}
}
else{
$userObject.PutEx(4, 'proxyAddresses', @("$prxaddress"))
$userObject.setinfo()
}
}
}
}
$lcLoopCount += 1
}
}
else {[system.windows.forms.messagebox]::Show("Not proceeding with delete") }
SearchforProxy

}


}

$msTable = New-Object System.Data.DataTable
$msTable.TableName = "ProxyAddress"
$msTable.Columns.Add("UserName")
$msTable.Columns.Add("Primary-EmailAddress")
$msTable.Columns.Add("ProxyAddress-Found")
$msTable.Columns.Add("OU")
$msTable.Columns.Add("ProxyAddresses")
$msTable.Columns.Add("distinguishedName")


# Add RadioButtons
$rbSearchDWide = new-object System.Windows.Forms.RadioButton
$rbSearchDWide.Location = new-object System.Drawing.Size(20,20)
$rbSearchDWide.size = new-object System.Drawing.Size(150,17)
$rbSearchDWide.Checked = $true
$rbSearchDWide.Text = "Search Domain Wide"
$rbSearchDWide.Add_Click({if ($rbSearchDWide.Checked -eq $true){$ouOUNameDrop.Enabled = $false}})
$form.Controls.Add($rbSearchDWide)

$rbSearchOUWide = new-object System.Windows.Forms.RadioButton
$rbSearchOUWide.Location = new-object System.Drawing.Size(20,60)
$rbSearchOUWide.size = new-object System.Drawing.Size(150,17)
$rbSearchOUWide.Checked = $false
$rbSearchOUWide.Add_Click({if ($rbSearchDWide.Checked -eq $false){$ouOUNameDrop.Enabled = $true}})
$rbSearchOUWide.Text = "Search within OU"
$form.Controls.Add($rbSearchOUWide)


$OulableBox = new-object System.Windows.Forms.Label
$OulableBox.Location = new-object System.Drawing.Size(220,60)
$OulableBox.size = new-object System.Drawing.Size(120,20)
$OulableBox.Text = "Select OU Name : "
$form.controls.Add($OulableBox)

# Add OU Drop Down
$ouOUNameDrop = new-object System.Windows.Forms.ComboBox
$ouOUNameDrop.Location = new-object System.Drawing.Size(360,60)
$ouOUNameDrop.Size = new-object System.Drawing.Size(350,30)
$ouOUNameDrop.Enabled = $false
$root = [ADSI]'LDAP://RootDSE'
$dfDefaultRootPath = "LDAP://" + $root.DefaultNamingContext.tostring()
$dfRoot = [ADSI]$dfDefaultRootPath
$gfGALQueryFilter = "(objectClass=organizationalUnit)"
$dfsearcher = new-object System.DirectoryServices.DirectorySearcher($dfRoot)
$dfsearcher.Filter = $gfGALQueryFilter
$srSearchResult = $dfsearcher.FindAll()
foreach ($emResult in $srSearchResult) {
$OUobject = New-Object System.DirectoryServices.directoryentry
$OUobject = $emResult.GetDirectoryEntry()
$ouOUNameDrop.Items.Add($OUobject.distinguishedName.ToString())
}
$form.Controls.Add($ouOUNameDrop)

$ProxyAddresslableBox = new-object System.Windows.Forms.Label
$ProxyAddresslableBox.Location = new-object System.Drawing.Size(20,100)
$ProxyAddresslableBox.size = new-object System.Drawing.Size(320,20)
$ProxyAddresslableBox.Text = "Proxy Address to Search (eg @proxydomain.com)"
$form.controls.Add($ProxyAddresslableBox)

$ouOUCheckBox = new-object System.Windows.Forms.CheckBox
$ouOUCheckBox.Location = new-object System.Drawing.Size(750,60)
$ouOUCheckBox.Size = new-object System.Drawing.Size(200,20)
$ouOUCheckBox.Checked = $true
$ouOUCheckBox.Text = "Search in Sub OU's"
$form.Controls.Add($ouOUCheckBox)


# Add ProxyDomain Text Box
$pnProxyAddress = new-object System.Windows.Forms.TextBox
$pnProxyAddress.Location = new-object System.Drawing.Size(350,100)
$pnProxyAddress.size = new-object System.Drawing.Size(300,20)
$form.controls.Add($pnProxyAddress)

$exButton1 = new-object System.Windows.Forms.Button
$exButton1.Location = new-object System.Drawing.Size(700,100)
$exButton1.Size = new-object System.Drawing.Size(125,20)
$exButton1.Text = "Search"
$exButton1.Add_Click({SearchforProxy})
$form.Controls.Add($exButton1)

# Add Export Grid Button

$exButton2 = new-object System.Windows.Forms.Button
$exButton2.Location = new-object System.Drawing.Size(10,760)
$exButton2.Size = new-object System.Drawing.Size(125,20)
$exButton2.Text = "Export Grid"
$exButton2.Add_Click({ExportGrid})
$form.Controls.Add($exButton2)

# Add Export Grid Button

$exButton3 = new-object System.Windows.Forms.Button
$exButton3.Location = new-object System.Drawing.Size(250,760)
$exButton3.Size = new-object System.Drawing.Size(300,20)
$exButton3.Text = "Deleted Select Proxy Addresses"
$exButton3.Add_Click({DeletedSelected})
$form.Controls.Add($exButton3)

$cmfCheckBox = new-object System.Windows.Forms.CheckBox
$cmfCheckBox.Location = new-object System.Drawing.Size(600,760)
$cmfCheckBox.Size = new-object System.Drawing.Size(200,20)
$cmfCheckBox.Checked = $true
$cmfCheckBox.Text = "Confirm Each removal"
$form.Controls.Add($cmfCheckBox)


# Add DataGrid View

$dgDataGrid = new-object System.windows.forms.DataGridView
$dgDataGrid.Location = new-object System.Drawing.Size(10,145)
$dgDataGrid.size = new-object System.Drawing.Size(1000,600)
$dgDataGrid.AutoSizeRowsMode = "AllHeaders"
$form.Controls.Add($dgDataGrid)

$form.Text = "Proxy Address Search and Remove Form"
$form.size = new-object System.Drawing.Size(1200,750)
$form.autoscroll = $true
$form.Add_Shown({$form.Activate()})
$form.ShowDialog()

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Graph is limited to a m

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You need to authorize it in you tenant (eg build a small ap

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.