Skip to main content

Recipient \ Email Address Policy GUI and browser for Exchange 2007 / 2010

Recipient policies where first introduced in Exchange 2003 and later became email address policies in Exchange 2007 where we where all introduced to the joys and sorrows of Opath. While policies are a simple idea and a good method to do this type of thing the complexities of what happens underneath can be hard to get your head around and test and this can at times lead to the odd mistake. While there is a preview box when your modifying an Address list policy if you wanted to look at the bigger picture of the filter that the address policy represents as it is and could be applied to active directory objects, this is where it can be useful to use a script. The other thing to take into account when looking a policies is the objects where the application of the address policies have been disabled. So what I've done is build a GUI to help this the first thing this script does is enumerate all the policy objects and retrieve the following properties that are associated with these object.

GatewayProxy - This contains the proxyaddresses that will be applied by the policy

msExchQueryFilter - This is the Opath filter for the policy

msExchPolicyOrder - This is the priority of the address policy

msExchPurportedSearchUI - This is a little bit of cheat as this property is used for the UI but it does contain a LDAP filter that can be used directly in ADSI so this is the property that I've used to search active directory for objects that the policy relates to.

GUID this is stored so it can be compared to the the msexchPoliciesIncluded property to work out if a policy has been applied to a specific object.

To work out if a policy has been applied to an object and also work out if policies have been disabled two properties are important. If polices have been disabled via the "Automatically update e-mail addresses based on e-mail address policy" then the msExchPoliciesExcluded will be set to 26491CFC-9E50-4857-861B-0CB8DF22B5D7.

Once a policy has been applied to an AD object the msexchpoliciesincluded will be updated with the GUID of the policy that has been applied. So when it comes time to enumerating applied polices using a script these a properties that you can put to use.

What happens when you hit the search button is the script does a ADSI query of active directory based on the LDAP filter and will return the objects that match this filter and tell you want policis has been applied to these objects. The radio button let you filter on all object, just the ones where the policy has been applied or disabled.

Some other things this script can be used for are backing up the current proxyaddress setting of accounts before you make any changes.

I've put a download of this 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
$raCollection = @()
$usrCollection = @()
$policyhash = @{ }
# $policyhash.Add("26491CFC-9E50-4857-861B-0CB8DF22B5D7","Disabled")

function UpdateListBoxPolicy(){
foreach ($prxobj in $raCollection){
if ($prxobj.PolicyName -eq $policyNameDrop.SelectedItem.ToString()){
$lbListView.clear()
$lbListView.Columns.Add("Property",80)
$lbListView.Columns.Add("Value",220)
$item1 = new-object System.Windows.Forms.ListViewItem("GUID")
$item1.SubItems.Add($prxobj.GUID.ToString())
$lbListView.items.add($item1)
$item1 = new-object System.Windows.Forms.ListViewItem("OpathFilter")
$item1.SubItems.Add($prxobj.OpathFilter.ToString())
$lbListView.items.add($item1)
$item1 = new-object System.Windows.Forms.ListViewItem("gatewayProxy")
if ($prxobj.gatewayProxy -is [system.array]){
$item1.SubItems.Add(([string]::join(";", $prxobj.gatewayProxy)))
}
else{
$item1.SubItems.Add($prxobj.gatewayProxy.ToString())
}
$lbListView.items.add($item1)
$item1 = new-object System.Windows.Forms.ListViewItem("LDAPFilter")
$item1.SubItems.Add($prxobj.LDAPFilter.ToString())
$lbListView.items.add($item1)
$item1 = new-object System.Windows.Forms.ListViewItem("msExchPolicyOrder")
$item1.SubItems.Add($prxobj.msExchPolicyOrder.ToString())
$lbListView.items.add($item1)
}
}
}

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("DisplayName,PrimaryEmailAdrress,ProxyAddresses,RecipientPolicy")
foreach($row in $msTable.Rows){
$logfile.WriteLine("`"" + $row[0].ToString() + "`"," + $row[1].ToString() + "," + $row[2].ToString() + "," + $row[3].ToString())
}
$logfile.Close()
}



}

function SearchforObjects(){
$policyobject = ""
foreach ($prxobj in $raCollection){
if ($prxobj.PolicyName -eq $policyNameDrop.SelectedItem.ToString()){
$policyobject = $prxobj.LDAPFilter.ToString()
}
}
$msTable.clear()
$dfDefaultRootPath = "LDAP://" + $root.DefaultNamingContext.tostring()
$dfRoot = [ADSI]$dfDefaultRootPath
$gfGALQueryFilter = $policyobject
$dfsearcher = new-object System.DirectoryServices.DirectorySearcher($dfRoot)
$dfsearcher.Filter = $gfGALQueryFilter
$dfsearcher.PageSize = 1000
$dfsearcher.PropertiesToLoad.Add("msexchPoliciesIncluded")
$dfsearcher.PropertiesToLoad.Add("proxyAddresses")
$dfsearcher.PropertiesToLoad.Add("mail")
$dfsearcher.PropertiesToLoad.Add("displayName")
$dfsearcher.PropertiesToLoad.Add("distinguishedName")
$dfsearcher.PropertiesToLoad.Add("msExchPoliciesExcluded")
$srSearchResult = $dfsearcher.FindAll()
foreach ($emResult in $srSearchResult) {
$emProps = $emResult.Properties
$DisplayName = ""
$PrimaryEmailAdrress = ""
$ProxyAddresses = ""
$RecipientPolicy = ""
if($emProps.msexchpoliciesincluded -ne $null){
$polarray = $emProps.msexchpoliciesincluded[0].Split(",")
foreach($pol in $polarray){
$pol = $pol.ToString().Replace("{","").Replace("}","")
if ($policyhash.ContainsKey($pol.ToString())){
$RecipientPolicy = $policyhash[$pol.ToString()]

}
}
}
if ($emProps.msexchpoliciesexcluded -ne $null){
foreach($pol in $emProps.msexchpoliciesexcluded){
$pol = $pol.ToString().Replace("{","").Replace("}","")
$pol.ToString()
if ($pol.ToString() -eq "26491CFC-9E50-4857-861B-0CB8DF22B5D7"){
$RecipientPolicy = "Disabled"

}
}
}
$DisplayName = $emResult.Properties["displayname"][0]
$PrimaryEmailAdrress = $emResult.Properties["mail"][0]
$ProxyAddresses = [string]::join(";",$emProps.proxyaddresses)
if($rbinc.Checked -eq $true){
$msTable.rows.add($DisplayName,$PrimaryEmailAdrress,$ProxyAddresses,$RecipientPolicy)
}
if($rbinc1.Checked -eq $true -band $RecipientPolicy -eq $policyNameDrop.SelectedItem.ToString()){
$msTable.rows.add($DisplayName,$PrimaryEmailAdrress,$ProxyAddresses,$RecipientPolicy)
}
if($rbinc2.Checked -eq $true -band $RecipientPolicy -eq "Disabled"){
$msTable.rows.add($DisplayName,$PrimaryEmailAdrress,$ProxyAddresses,$RecipientPolicy)
}

}
$dgDataGrid.Datasource = $msTable
}

$root = [ADSI]'LDAP://RootDSE'
$cfConfigRootpath = "LDAP://" + $root.ConfigurationNamingContext.tostring()
$configRoot = [ADSI]$cfConfigRootpath
$searcher = new-object System.DirectoryServices.DirectorySearcher($configRoot)
$searcher.Filter = "(objectClass=msexchRecipientPolicy)"
$searchresults = $searcher.FindAll()
foreach ($searchresult in $searchresults){
$plcobj = "" | select PolicyName,GUID,gatewayProxy,OpathFilter,LDAPFilter,msExchPolicyOrder
$Policyobject = New-Object System.DirectoryServices.directoryentry
$Policyobject = $searchresult.GetDirectoryEntry()
$plcobj.PolicyName = $Policyobject.Name.Value
$plcobj.GUID = [GUID]$Policyobject.ObjectGUID.Value
$plcobj.OpathFilter = $Policyobject.msExchQueryFilter.Value
$plcobj.gatewayProxy = $Policyobject.GatewayProxy.Value
$plcobj.msExchPolicyOrder = $Policyobject.msExchPolicyOrder.Value
foreach ($val in $Policyobject.msExchPurportedSearchUI){
if($val -match "Microsoft.PropertyWell_QueryString="){
$plcobj.LDAPFilter = $val.substring(35,($val.length-35))
}
}
$raCollection += $plcobj
$policyhash.add($plcobj.GUID.ToString(),$plcobj.PolicyName)
}


$msTable = New-Object System.Data.DataTable
$msTable.TableName = "ProxyAddress"
$msTable.Columns.Add("DisplayName")
$msTable.Columns.Add("PrimaryEmailAdrress")
$msTable.Columns.Add("ProxyAddresses")
$msTable.Columns.Add("RecipientPolicy")


$PolicylableBox = new-object System.Windows.Forms.Label
$PolicylableBox.Location = new-object System.Drawing.Size(10,20)
$PolicylableBox.size = new-object System.Drawing.Size(120,20)
$PolicylableBox.Text = "Select Policy Name : "
$form.controls.Add($PolicylableBox)

# Add Policy Drop Down
$policyNameDrop = new-object System.Windows.Forms.ComboBox
$policyNameDrop.Location = new-object System.Drawing.Size(130,20)
$policyNameDrop.Size = new-object System.Drawing.Size(200,30)
$policyNameDrop.Enabled = $true
foreach ($prxobj in $raCollection){
$policyNameDrop.Items.Add($prxobj.PolicyName)
}
$policyNameDrop.Add_SelectedValueChanged({UpdateListBoxPolicy})
$form.Controls.Add($policyNameDrop)

$policySettingslableBox = new-object System.Windows.Forms.Label
$policySettingslableBox.Location = new-object System.Drawing.Size(340,20)
$policySettingslableBox.size = new-object System.Drawing.Size(80,20)
$policySettingslableBox.Text = "Policy Settings"
$form.controls.Add($policySettingslableBox)

$lbListView = new-object System.Windows.Forms.ListView
$lbListView.Location = new-object System.Drawing.Size(450,20)
$lbListView.size = new-object System.Drawing.Size(350,100)
$lbListView.LabelEdit = $True
$lbListView.AllowColumnReorder = $True
$lbListView.CheckBoxes = $False
$lbListView.FullRowSelect = $True
$lbListView.GridLines = $True
$lbListView.View = "Details"
$lbListView.Sorting = "Ascending"
$form.controls.Add($lbListView)

# Add RadioButtons
$rbinc = new-object System.Windows.Forms.RadioButton
$rbinc.Location = new-object System.Drawing.Size(150,50)
$rbinc.size = new-object System.Drawing.Size(220,17)
$rbinc.Checked = $true
$rbinc.Text = "All objects Policy Query matches"
$form.Controls.Add($rbinc)

$rbinc1 = new-object System.Windows.Forms.RadioButton
$rbinc1.Location = new-object System.Drawing.Size(150,70)
$rbinc1.size = new-object System.Drawing.Size(220,17)
$rbinc1.Checked = $false
$rbinc1.Text = "Only objects with Apply policy enabled"
$form.Controls.Add($rbinc1)

$rbinc2 = new-object System.Windows.Forms.RadioButton
$rbinc2.Location = new-object System.Drawing.Size(150,90)
$rbinc2.size = new-object System.Drawing.Size(220,17)
$rbinc2.Checked = $false
$rbinc2.Text = "Only objects with Apply policy disabled"
$form.Controls.Add($rbinc2)



$exButton1 = new-object System.Windows.Forms.Button
$exButton1.Location = new-object System.Drawing.Size(10,70)
$exButton1.Size = new-object System.Drawing.Size(125,20)
$exButton1.Text = "Search"
$exButton1.Add_Click({SearchforObjects})
$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 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 = "Address Policy GUI"
$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.