Friday, June 04, 2010

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 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

$form = new-object System.Windows.Forms.form

function SearchforProxy(){
if ($pnProxyAddress.Text -match "@"){
$root = [ADSI]'LDAP://RootDSE'
if ($rbSearchDWide.Checked -eq $true){
$dfDefaultRootPath = "LDAP://" + $root.DefaultNamingContext.tostring()
$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()}

$dgDataGrid.DataSource = $msTable
$msgstring = "Address format incorrect it must contain @ sign"
$a = new-object -comobject
$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
if ($exFileName.FileName -ne ""){
$logfile = new-object IO.StreamWriter($exFileName.FileName,$true)
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())


function DeletedSelected(){
if ($dgDataGrid.SelectedRows.Count -eq 0){
$msgstring = "Now Rows Selected"
$a = new-object -comobject
$b = $a.popup($msgstring,0,"Warning",1)
$return = ""
$return = []::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"
$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 = []::Show($Message,"",$buttons)
if ($mreturn -eq [Windows.Forms.DialogResult]::Yes){
$userObject.PutEx(4, 'proxyAddresses', @("$prxaddress"))
$userObject.PutEx(4, 'proxyAddresses', @("$prxaddress"))
$lcLoopCount += 1
else {[]::Show("Not proceeding with delete") }



$msTable = New-Object System.Data.DataTable
$msTable.TableName = "ProxyAddress"

# 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}})

$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"

$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 : "

# 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()

$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"

$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"

# 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)

$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"

# 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"

# 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"

$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"

# Add DataGrid View

$dgDataGrid = new-object
$dgDataGrid.Location = new-object System.Drawing.Size(10,145)
$dgDataGrid.size = new-object System.Drawing.Size(1000,600)
$dgDataGrid.AutoSizeRowsMode = "AllHeaders"

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