Sunday, September 19, 2010

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

$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()){
$item1 = new-object System.Windows.Forms.ListViewItem("GUID")
$item1 = new-object System.Windows.Forms.ListViewItem("OpathFilter")
$item1 = new-object System.Windows.Forms.ListViewItem("gatewayProxy")
if ($prxobj.gatewayProxy -is [system.array]){
$item1.SubItems.Add(([string]::join(";", $prxobj.gatewayProxy)))
$item1 = new-object System.Windows.Forms.ListViewItem("LDAPFilter")
$item1 = new-object System.Windows.Forms.ListViewItem("msExchPolicyOrder")

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


function SearchforObjects(){
$policyobject = ""
foreach ($prxobj in $raCollection){
if ($prxobj.PolicyName -eq $policyNameDrop.SelectedItem.ToString()){
$policyobject = $prxobj.LDAPFilter.ToString()
$dfDefaultRootPath = "LDAP://" + $root.DefaultNamingContext.tostring()
$dfRoot = [ADSI]$dfDefaultRootPath
$gfGALQueryFilter = $policyobject
$dfsearcher = new-object System.DirectoryServices.DirectorySearcher($dfRoot)
$dfsearcher.Filter = $gfGALQueryFilter
$dfsearcher.PageSize = 1000
$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("}","")
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){
if($rbinc1.Checked -eq $true -band $RecipientPolicy -eq $policyNameDrop.SelectedItem.ToString()){
if($rbinc2.Checked -eq $true -band $RecipientPolicy -eq "Disabled"){

$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

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

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

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

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

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

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

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

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

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

# 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 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 = "Address Policy GUI"
$form.size = new-object System.Drawing.Size(1200,750)
$form.autoscroll = $true

1 comment:

Anonymous said...

Awesome Script - Thanks :) Just having to tidy up 20odd policies down to a couple so really helpful to see who has what applied to them.