Sunday, November 30, 2008

Syncronizing contacts in Exchange 2007 with other things - helper class

Copying a Contact in Exchange is one thing synchronizing is another this can be one of the great contradictions and issues that faces anybody looking at synchronizing contacts between different storage mediums. Whether that be a Database, CSV file or other Mail System underlying its the same information stored and accessed in a different way. Because Exchange isn't your normal flat file database more a relatively complex property store that uses some complex datatypes doing property level synchronization does provide a unique set of challenges. Exchange Web Service presents some of these properties in more workable format but apart from this doesn't give a lot of help is solving this fundamental issue. The SyncFolderItem and notification operations only provide an Item level ability to spot changes on an item so the brave souls who want to embark on a greater level of synchronization must take this challenge unto themselves.

If your going to compare two contacts it first helps to have them in the same format there are a few methods of doing this one way you might go about this if your using Exchange 2007 and SQL 2005 is using something like The path I've gone down it to convert them into the ContactType class that's used in EWS. This may or may not be the best method but it does have a flexibility that I'm after. With flexibility comes a great ability to do some really cool things.

So what I've come up with is a method that takes two ContactType objects as parameters and then returns a generic list of SetItemFieldType updates that can then be used direct in a UpdateItem Operation. With EWS you need to have a separate SetItemFieldType for each property of an item you want to update. If you have a large number of properties you are updating on an Item especially if they are different datatypes then these requests can get lengthy and complex. The first contact object represents the source object which has the new information you want synchronized and the second is destination object which you want updated. The changes that are returned are the differences on the source object when compared against the destination object. Because Contacts are made up of complex datatypes the compare class needs to deal differently depending on which property you are looking at.

String Properties:
For string properties a simple comparison is done to work out if one property is different from the other.

String Arrays: For String arrays the elements of the arrays are joined and then the strings of the joined array is compared.

Email Addresses, Phone Numbers, Street Addresses: These are special indexed arrays so to compare the index valued two hashtables are used. The Hashtables allow the ability to do string comparison of the values based on the on the indexed values. With Street Addresses because of the way the indexing works with these properties nested hash tables are used.

Extended Properties:Extended Mapi properties are one of the things that make objects in Exchange hard to deal with. The class will go through any that are set in the source contact and that are retrieved in the destination object and and will do a comparison based on either the propertyID or propertyTag. Because there is no real good method to get all the Extended properties that have been set on a item unless you use ExMapi this is one area where you can and will loose fidelity on items you might be synchronizing because each of these properties must be explicitly specified if you want to synchronize them.

Because the SetItemUpdate will be different for each of these types there is a separate method that handles building the update.

Whats missing - This helper class will look for whats been added or changed in a contact and produce an update for those properties. But it wont produce
DeleteItemFieldType to delete properties that may have been removed in the source contact.

How to use this : Watch this space i'll have a few sample over the next couple of weeks on how to use this but i just wanted to introduce the code in a seperate post. I've put a download of the code I've talked about here the actual code is too large to post.

Sunday, November 16, 2008

Find Unused Mailbox Powershell Gui Exchange 2007

Finding unused mailboxes is one of those mind numbing tasks a Sys Admin must perform routinely in Large Exchange organizations. Many of the scripts on this blog help to show ways to tackle different issues around unused or disabled mailboxes such as removing disabled users accounts from groups or finding broken delegate forwarding rules etc. I've also posted a few scripts before that showed some methods to track down unused mailboxes by looking at the number of unread email and the last time a mailbox sent a message. Well this script puts some of these methods together in a powershell GUI script that uses Exchange Web Services and some Exchange Management Shell cmdlets to look at all mailboxes on a server and show us information about when a mailbox was logged into, how big it is, how many unread email there is and when the last sent and/or received email was. It presents all this information back to you as a GUI with data grid and also has a button that allows exporting the result to a CSV file.

How it works?

To get the information about when the mailbox was last logged onto the EMS cmdlet get-mailbox is used and also get-Mailboxstatistics is also used to get the mailbox size in MB. To get the number of unread email in a mailbox and to get the last time a mail was sent from the sent items folder Exchange Web Services is used to query the mailbox. To do this from Powershell and the EMS I’ve used my EWSUtil library which has some routine in there that will perform a finditem operation with the nessasary restrictions so it will only show Unread messages. The library will handle Autodiscover and allow for both delegate and impersonation access method. The Form allows for all of these combinations and also allows you to specify overrides for both authentication and the URL for the CAS server to use (you need to use the full url to the EWS if you going to use eg https://servername/ews/exchange.asmx). The rest of the script just builds the form elements for the GUI and adds some simple function that does a export of the data shown in the GRID.

To use this script you need to have the EwsUtil.dll in c:\temp you can download the library from You can download the actual script from here I've include a few other scripts in the download that show simpler examples of read the unread email from mailboxes as well (Just for fun). I've put the download of the script here the Code itself looks like

$form = new-object System.Windows.Forms.form
$mbcombCollection = @()
$Emailhash = @{ }

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() + "," + $row[6].ToString())


function Getinfo(){

$mbcombCollection = @()

Get-Mailbox -server $snServerNameDrop.SelectedItem.ToString() -ResultSize Unlimited | foreach-object{
if ($Emailhash.containskey($_.ExchangeGuid) -eq $false){

get-mailboxstatistics -server $snServerNameDrop.SelectedItem.ToString() | foreach-object{
$mbcomb = "" | select DisplayName,EmailAddress,Last_Logon,MailboxSize,
$mbcomb.DisplayName = $_.DisplayName.ToString()
if ($Emailhash.ContainsKey($_.MailboxGUID.ToString())){
$mbcomb.EmailAddress = $Emailhash[$_.MailboxGUID.ToString()]
if ($_.LastLogonTime -ne $null){
$mbcomb.Last_Logon = $_.LastLogonTime.ToString()
$mbcomb.MailboxSize = $_.TotalItemSize.Value.ToMB()

"Mailbox : " + $mbcomb.EmailAddress
if ($mbcomb.EmailAddress -ne $null){
$mbMailboxEmail = $mbcomb.EmailAddress
if ($unCASUrlTextBox.text -eq ""){ $casurl = $null}
else { $casurl= $unCASUrlTextBox.text}
$useImp = $false
if ($seImpersonationCheck.Checked -eq $true) {
$useImp = $true
if ($seAuthCheck.Checked -eq $true) {
$ewc = new-object EWSUtil.EWSConnection($mbMailboxEmail,$useImp, $unUserNameTextBox.Text, $unPasswordTextBox.Text, $unDomainTextBox.Text,$casUrl)
$ewc = new-object EWSUtil.EWSConnection($mbMailboxEmail,$useImp, "", "", "",$casUrl)

$drDuration = new-object EWSUtil.EWS.Duration
$drDuration.StartTime = [DateTime]::UtcNow.AddDays(-365)
$drDuration.EndTime = [DateTime]::UtcNow

$dTypeFld = new-object EWSUtil.EWS.DistinguishedFolderIdType
$dTypeFld.Id = [EWSUtil.EWS.DistinguishedFolderIdNameType]::inbox
$dTypeFld2 = new-object EWSUtil.EWS.DistinguishedFolderIdType
$dTypeFld2.Id = [EWSUtil.EWS.DistinguishedFolderIdNameType]::sentitems

$mbMailbox = new-object EWSUtil.EWS.EmailAddressType
$mbMailbox.EmailAddress = $mbMailboxEmail
$dTypeFld.Mailbox = $mbMailbox
$dTypeFld2.Mailbox = $mbMailbox

$fldarry = new-object EWSUtil.EWS.BaseFolderIdType[] 1
$fldarry[0] = $dTypeFld
$msgList = $ewc.FindUnread($fldarry, $drDuration, $null, "")
$mbcomb.Inbox_Number_Unread = $msgList.Count
if ($msgList.Count -ne 0){
$mbcomb.Inbox_Unread_LastRecieved = $msgList[0].DateTimeSent.ToLocalTime().ToString()

$fldarry = new-object EWSUtil.EWS.BaseFolderIdType[] 1
$fldarry[0] = $dTypeFld2
$msgList = $ewc.FindItems($fldarry, $drDuration, $null, "")
if ($msgList.Count -ne 0){
$mbcomb.Sent_Items_LastSent = $msgList[0].DateTimeSent.ToLocalTime().ToString()
$mbcombCollection += $mbcomb}
$dgDataGrid.DataSource = $msTable

$msTable = New-Object System.Data.DataTable
$msTable.TableName = "Mailbox Info"
$msTable.Columns.Add("Last Logon Time",[DateTime])
$msTable.Columns.Add("Mailbox Size(MB)",[int64])
$msTable.Columns.Add("Inbox Number Unread",[int64])
$msTable.Columns.Add("Inbox unread Last Recieved",[DateTime])
$msTable.Columns.Add("Sent Item Last Sent",[DateTime])

# Add Server DropLable
$snServerNamelableBox = new-object System.Windows.Forms.Label
$snServerNamelableBox.Location = new-object System.Drawing.Size(10,20)
$snServerNamelableBox.size = new-object System.Drawing.Size(80,20)
$snServerNamelableBox.Text = "ServerName"

# Add Server Drop Down
$snServerNameDrop = new-object System.Windows.Forms.ComboBox
$snServerNameDrop.Location = new-object System.Drawing.Size(90,20)
$snServerNameDrop.Size = new-object System.Drawing.Size(150,30)
get-mailboxserver | ForEach-Object{$snServerNameDrop.Items.Add($_.Name)}

# Add Export Grid Button

$exButton2 = new-object System.Windows.Forms.Button
$exButton2.Location = new-object System.Drawing.Size(250,20)
$exButton2.Size = new-object System.Drawing.Size(125,20)
$exButton2.Text = "Execute"

# Add Export Grid Button

$exButton1 = new-object System.Windows.Forms.Button
$exButton1.Location = new-object System.Drawing.Size(10,610)
$exButton1.Size = new-object System.Drawing.Size(125,20)
$exButton1.Text = "Export Grid"

# Add Impersonation Clause

$esImpersonationlableBox = new-object System.Windows.Forms.Label
$esImpersonationlableBox.Location = new-object System.Drawing.Size(10,75)
$esImpersonationlableBox.Size = new-object System.Drawing.Size(130,20)
$esImpersonationlableBox.Text = "Use EWS Impersonation"

$seImpersonationCheck = new-object System.Windows.Forms.CheckBox
$seImpersonationCheck.Location = new-object System.Drawing.Size(150,70)
$seImpersonationCheck.Size = new-object System.Drawing.Size(30,25)

# Add Auth Clause

$esAuthlableBox = new-object System.Windows.Forms.Label
$esAuthlableBox.Location = new-object System.Drawing.Size(10,105)
$esAuthlableBox.Size = new-object System.Drawing.Size(130,20)
$esAuthlableBox.Text = "Specify Credentials"

$seAuthCheck = new-object System.Windows.Forms.CheckBox
$seAuthCheck.Location = new-object System.Drawing.Size(140,100)
$seAuthCheck.Size = new-object System.Drawing.Size(30,25)
$seAuthCheck.Add_Click({if ($seAuthCheck.Checked -eq $true){
$unUserNameTextBox.Enabled = $true
$unPasswordTextBox.Enabled = $true
$unDomainTextBox.Enabled = $true
$unUserNameTextBox.Enabled = $false
$unPasswordTextBox.Enabled = $false
$unDomainTextBox.Enabled = $false}})

# Add UserName Box
$unUserNameTextBox = new-object System.Windows.Forms.TextBox
$unUserNameTextBox.Location = new-object System.Drawing.Size(230,100)
$unUserNameTextBox.size = new-object System.Drawing.Size(100,20)

# Add UserName Lable
$unUserNamelableBox = new-object System.Windows.Forms.Label
$unUserNamelableBox.Location = new-object System.Drawing.Size(170,105)
$unUserNamelableBox.size = new-object System.Drawing.Size(60,20)
$unUserNamelableBox.Text = "UserName"
$unUserNameTextBox.Enabled = $false

# Add Password Box
$unPasswordTextBox = new-object System.Windows.Forms.TextBox
$unPasswordTextBox.PasswordChar = "*"
$unPasswordTextBox.Location = new-object System.Drawing.Size(400,100)
$unPasswordTextBox.size = new-object System.Drawing.Size(100,20)

# Add Password Lable
$unPasswordlableBox = new-object System.Windows.Forms.Label
$unPasswordlableBox.Location = new-object System.Drawing.Size(340,105)
$unPasswordlableBox.size = new-object System.Drawing.Size(60,20)
$unPasswordlableBox.Text = "Password"
$unPasswordTextBox.Enabled = $false

# Add Domain Box
$unDomainTextBox = new-object System.Windows.Forms.TextBox
$unDomainTextBox.Location = new-object System.Drawing.Size(550,100)
$unDomainTextBox.size = new-object System.Drawing.Size(100,20)

# Add Domain Lable
$unDomainlableBox = new-object System.Windows.Forms.Label
$unDomainlableBox.Location = new-object System.Drawing.Size(510,105)
$unDomainlableBox.size = new-object System.Drawing.Size(50,20)
$unDomainlableBox.Text = "Domain"
$unDomainTextBox.Enabled = $false

# Add CASUrl Box
$unCASUrlTextBox = new-object System.Windows.Forms.TextBox
$unCASUrlTextBox.Location = new-object System.Drawing.Size(280,75)
$unCASUrlTextBox.size = new-object System.Drawing.Size(400,20)
$unCASUrlTextBox.text = $strRootURI

# Add CASUrl Lable
$unCASUrllableBox = new-object System.Windows.Forms.Label
$unCASUrllableBox.Location = new-object System.Drawing.Size(200,75)
$unCASUrllableBox.size = new-object System.Drawing.Size(50,20)
$unCASUrllableBox.Text = "CASUrl"

# Add DataGrid View

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

$form.Text = "Exchange 2007 Unused Mailbox Form"
$form.size = new-object System.Drawing.Size(1200,700)
$form.autoscroll = $true

Tuesday, November 04, 2008

Dealing with Non Delivery Reports with Exchange Web Services

If there was one thing i would change if I could redesign email from the ground up it would have to be NDR's. While these delivery reports are functional in what they do from a user and even an administrative prospective they are horribly disfunctional. While there isn't much you can do around the underlying design of NDR's with a little help from your favourite Exchange API there are certain tasks that can be made more livable.

NDR Overload

If your users are sending and receiving a lot of email they you'll probably find that a number of NDR's are being generated for a vast number of different reasons. A lot of the times the loop will get closed where your user having tryed and failed to decipher what the NDR says gives up and calls the helpdesk. If your sending a copy of NDR's to a central mailbox what can be usefull for everybody concerned is to have a digest list of all the NDR's that have been recieved in a certain time frame this can allow you to do some analysis on why these NDR's are being generated in the first place and be a little proactive if you have a problems that a causing these NDR's such as certain DNS records or maybe your server has being blacklisted or just having the ability to have at your finger tips the NDR that a user received without then having to forward it to you.

Using Some Code

Here's the fun part to get a list of NDR's using EWS you need to use a few different operations. The first is you need to use a FindItem operations with a restriction on the MessageClass to limit the items returns to those of type REPORT.IPM.Note.NDR. To get the body of the NDR which will contain the NDR reason a GetItem request that includes the messagebody is nessasary and then the reason can be parsed out of the body its sperated with # characters so it pretty easy. Other important pieces of information can be pulled at the same time like the original subject ,recipient and senders by using the appropriate Extended mapi properties for those items.

To make this a little more flexbile the code i wrote only checks for unread NDR's and then marks them as read once they have been included in the Digest. The Digest mail is then sent using EWS.

Proccessing Attachments on NDR's

If you have attachments on messages that are being sent and then bounced and included in the NDR and you want to process those attachments this can get a little confusing to deal with but its kind of logical when you see it in action. To download any attachment using EWS you need to know the attachmentID. To get an embeeded attachment its no different but to find that attachment you first need to get the orignial message using a GetAttachment request and then go through the attachment collection on that original message and use a Getattachment again from this embeeded message. I include a routine to do this and download any filebased attachments to the C#.

I've put a download of all these functions here some of the code look like

static List GetNDRs(ExchangeServiceBinding esb, BaseFolderIdType[] biArray)

List intFiFolderItems = new List();

BasePathToElementType[] beAdditionproperties = new BasePathToElementType[5];

PathToExtendedFieldType osOriginalSenderEmail = new PathToExtendedFieldType();
osOriginalSenderEmail.PropertyTag = "0x0067";
osOriginalSenderEmail.PropertyType = MapiPropertyTypeType.String;

PathToExtendedFieldType osOriginalSenderAdrType = new PathToExtendedFieldType();
osOriginalSenderAdrType.PropertyTag = "0x0066";
osOriginalSenderAdrType.PropertyType = MapiPropertyTypeType.String;

PathToExtendedFieldType osOriginalSenderName = new PathToExtendedFieldType();
osOriginalSenderName.PropertyTag = "0x005A";
osOriginalSenderName.PropertyType = MapiPropertyTypeType.String;

PathToExtendedFieldType osOriginalSubject = new PathToExtendedFieldType();
osOriginalSubject.PropertyTag = "0x0049";
osOriginalSubject.PropertyType = MapiPropertyTypeType.String;

PathToExtendedFieldType osOriginalRecp = new PathToExtendedFieldType();
osOriginalRecp.PropertyTag = "0x0074";
osOriginalRecp.PropertyType = MapiPropertyTypeType.String;

beAdditionproperties[0] = osOriginalSenderAdrType;
beAdditionproperties[1] = osOriginalSenderEmail;
beAdditionproperties[2] = osOriginalSenderName;
beAdditionproperties[3] = osOriginalRecp;
beAdditionproperties[4] = osOriginalSubject;

FindItemType fiFindItemRequest = new FindItemType();
fiFindItemRequest.ParentFolderIds = biArray;
fiFindItemRequest.Traversal = ItemQueryTraversalType.Shallow;
ItemResponseShapeType ipItemProperties = new ItemResponseShapeType();
ipItemProperties.BaseShape = DefaultShapeNamesType.AllProperties;
ipItemProperties.AdditionalProperties = beAdditionproperties;
fiFindItemRequest.ItemShape = ipItemProperties;
RestrictionType ffRestriction = new RestrictionType();

IsEqualToType ieToTypeClass = new IsEqualToType();
PathToUnindexedFieldType itItemType = new PathToUnindexedFieldType();
itItemType.FieldURI = UnindexedFieldURIType.itemItemClass;
ieToTypeClass.Item = itItemType;
FieldURIOrConstantType constantType = new FieldURIOrConstantType();
ConstantValueType constantValueType = new ConstantValueType();
constantValueType.Value = "REPORT.IPM.Note.NDR";
constantType.Item = constantValueType;
ieToTypeClass.Item = itItemType;
ieToTypeClass.FieldURIOrConstant = constantType;

IsEqualToType ieToTypeRead = new IsEqualToType();
PathToUnindexedFieldType rsReadStatus = new PathToUnindexedFieldType();
rsReadStatus.FieldURI = UnindexedFieldURIType.messageIsRead;
ieToTypeRead.Item = rsReadStatus;
FieldURIOrConstantType constantType1 = new FieldURIOrConstantType();
ConstantValueType constantValueType1 = new ConstantValueType();
constantValueType1.Value = "0";
constantType1.Item = constantValueType1;
ieToTypeRead.Item = rsReadStatus;
ieToTypeRead.FieldURIOrConstant = constantType1;

AndType raRestictionAnd = new AndType();
raRestictionAnd.Items = new SearchExpressionType[2];
raRestictionAnd.Items[0] = ieToTypeClass;
raRestictionAnd.Items[1] = ieToTypeRead;

ffRestriction.Item = raRestictionAnd;
fiFindItemRequest.Restriction = ffRestriction;

FindItemResponseType frFindItemResponse = esb.FindItem(fiFindItemRequest);
if (frFindItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success)
foreach (FindItemResponseMessageType firmtMessage in frFindItemResponse.ResponseMessages.Items)
Console.WriteLine("Number of Items Found : " + firmtMessage.RootFolder.TotalItemsInView);
if (firmtMessage.RootFolder.TotalItemsInView > 0)
foreach (ItemType miMailboxItem in ((ArrayOfRealItemsType)firmtMessage.RootFolder.Item).Items)
GetItemType giGetItem = new GetItemType();
giGetItem.ItemIds = new BaseItemIdType[1] { miMailboxItem.ItemId };
giGetItem.ItemShape = new ItemResponseShapeType();
giGetItem.ItemShape.AdditionalProperties = beAdditionproperties;
giGetItem.ItemShape.BaseShape = DefaultShapeNamesType.Default;
giGetItem.ItemShape.BodyType = BodyTypeResponseType.Text;
giGetItem.ItemShape.BodyTypeSpecified = true;
GetItemResponseType giResponse = esb.GetItem(giGetItem);
if (giResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Error)
Console.WriteLine("Error Occured");
ItemInfoResponseMessageType rmResponseMessage = giResponse.ResponseMessages.Items[0] as ItemInfoResponseMessageType;
// Create an object of update item type
UpdateItemType updateItemType = new UpdateItemType();
updateItemType.ConflictResolution = ConflictResolutionType.AlwaysOverwrite;
updateItemType.MessageDisposition = MessageDispositionType.SaveOnly;
updateItemType.MessageDispositionSpecified = true;
updateItemType.ItemChanges = new ItemChangeType[1];
ItemChangeType changeType = new ItemChangeType();

changeType.Item = rmResponseMessage.Items.Items[0].ItemId;
changeType.Updates = new ItemChangeDescriptionType[1];

// Create a set item field to identify the type of update
SetItemFieldType setItemEmail = new SetItemFieldType();

PathToUnindexedFieldType epExPath = new PathToUnindexedFieldType();
epExPath.FieldURI = UnindexedFieldURIType.messageIsRead;

MessageType mtMessage = new MessageType();
mtMessage.IsRead = true;
mtMessage.IsReadSpecified = true;

setItemEmail.Item = epExPath;
setItemEmail.Item1 = mtMessage;

changeType.Updates[0] = setItemEmail;
updateItemType.ItemChanges[0] = changeType;
// Send the update item request and receive the response
UpdateItemResponseType updateItemResponse = esb.UpdateItem(updateItemType);
if (updateItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success)
Console.WriteLine("Update Successful");



Console.WriteLine("Error During FindItem request : " + frFindItemResponse.ResponseMessages.Items[0].MessageText.ToString());

catch (Exception exException)
// return exException.ToString();
return intFiFolderItems;

catch (Exception exException)
// return exException.ToString();
return intFiFolderItems;