Friday, March 23, 2018

Using the Office365/Exchange 2016 REST API in Powershell Contacts

Within a Mailbox from an API perspective the big 3 things you deal with are first Messaging, second Calendaring and then Contacts. Up until now I've had just some basic support for Contacts in the Exch-Rest module https://www.powershellgallery.com/packages/Exch-Rest but from version 3.4 I've add lot more cmdlets to fill out this functionally. I've taken a guide from my EWSContacts Module and taken advantage of some of the new stuff you can do with the Graph API which was hard or not possible in EWS like getting Mail Enabled Contacts (AD Contacts).

I've added a documentation page with Examples on GitHub for most of the new cmdlets https://github.com/gscales/Exch-Rest/blob/master/Samples/ContactsExamples.md

To give a little taste of what you can do with these new cmldets

Creating Contacts

The New-EXRContact cmdlet can be used to create a contact in any mailbox specifying the most common properties
New-EXRContact -MailboxName mec@datarumble.com -FirstName "FirstName" -LastName "Surname of Contact" -EmailAddress "EmailAddress@domain.com" -MobilePhone 1111-222-333
If you want to also upload a photo as part of the contact you can use -photo switch to specify a file-name that contains the contact photo you want to be used for that contact eg
New-EXRContact -MailboxName mec@datarumble.com -FirstName "FirstName" -LastName "Surname of Contact" -EmailAddress "EmailAddress@domain.com" -MobilePhone 1111-222-333 -photo 'c:\photo\Johnsmith.jpg'
To create a contact in a contact Folder other then the default in the Mailbox use
New-EXRContact -MailboxName mec@datarumble.com -FirstName "FirstName" -LastName "Surname of Contact" -EmailAddress "EmailAddress@domain.com" -MobilePhone 1111-222-333 -ContactFolder SubContactFolder
Modifying existing Contacts

Eg modifying the department filed of an existing contact first find the contact based on the Email address and then update the Department field
$ExistingContact = Search-EXRContacts -MailboxName user@domain -emailaddressKQL 'myaddress@domain.com'
Set-EXRContact -Id $ExistingContact.id -Department "New Department"
Uploading a Contact Photo of an existing contact
$ExistingContact= Search-EXRContacts -MailboxName user@domain -emailaddressKQL 'myaddress@domain.com'
Set-EXRContactPhoto -Id $ExistingContact.id -FileName c:\photos\mycont.jpg
Export an existing Contact to a VCard

$ExistingContact = Search-EXRContacts -MailboxName user@domain -emailaddressKQL 'myaddress@domain.com'
Export-EXRContactToVcard -Id $ExistingContact.id -SaveAsFileName c:\photos\mycont.vcf

Deleting a Contact 

$ExistingContact = Search-EXRContacts -MailboxName user@domain -emailaddressKQL 'myaddress@domain.com'
Invoke-DeleteEXRContactd -Id $ExistingContact.id
 
Enumerating all Contacts in a Contacts Folder

The Default Contacts folder in a Mailbox

Get-EXRContacts -MailboxName user@domain
Other Contact Folders 

Get-EXRContacts -MailboxName user@domain -ContactsFolderName FolderName

Directory (AD) Contacts

Enumerating Directory Contacts

Get-EXRDirectoryContacts
Export a Directory Contact to Vcard


$dirContact = Get-EXRDirectoryContacts | Where-Object {$_.mail -eq 'glenscales@yahoo.com'}
Export-EXRDirectoryContactToVcard -Id $ExistingContact.id -SaveAsFileName c:\photos\mycont.vcf

Some things that are still missing in the Graph API is the ability to manage private distribution lists which will probably come along at a later date. The source for all the new cmldets are available on GitHub https://github.com/gscales/Exch-Rest/tree/master/functions/contacts

Friday, March 09, 2018

EWS-FAI Module for browsing and updating Exchange Folder Associated Items from PowerShell

Folder Associated Items are hidden Items in Exchange Mailbox folders that are commonly used to hold configuration settings for various Mailbox Clients and services that use Mailboxes. Some common examples of FAI's are Categories,OWA Signatures and WorkHours there is some more detailed documentation in the https://msdn.microsoft.com/en-us/library/cc463899(v=exchg.80).aspx protocol document. In EWS these configuration items can be accessed via the UserConfiguration operation https://msdn.microsoft.com/en-us/library/office/dd899439(v=exchg.150).aspx which will give you access to either the RoamingDictionary, XMLStream or BinaryStream data properties that holds the configuration depending on what type of FAI data is being stored.

I've written a number of scripts over the years that target particular FAI's (eg this one that reads the workhours http://gsexdev.blogspot.com.au/2015/11/finding-timezone-being-used-in-mailbox.html is a good example ) but I didn't have a generic script that could allow you to browse and read the data from any FAI in a Mailbox which would be useful when you are trying to peer into FAI's when debugging or reporting on different clients and services that use these FAI's.

So what I've done for this post in publish a really basic Powershell Module to the PowerShell Gallery https://www.powershellgallery.com/packages/EWS-FAI that can do this. The script requires that you have the EWS Managed API installed and then it has two Cmdlets. The source for the module can be found on GitHub here https://github.com/gscales/Powershell-Scripts/tree/master/EWS-FAI/Module

Invoke-ListFAIItems

The cmdlet will browse and display a list of any configuration FAI Items in a Folder eg to show the Items in the Non_IPM_SubTree use

Invoke-ListFAIItems -MailboxName mailbox@domain.com -Folder Root 


it will produce an output such as


Other folders such as the Inbox and Calendar contain some of the more useful FAI's eg


A quick rundown on these

AvailiablityOptions is a newie
Calendar Contains the calendar processing information more of intrest when looking at a room mailbox
CategoryList Is the Master Cateogry List of a Mailbox (XML)
WorkHours Is the workhours setting for a Mailbox (XML)


Get-FAIItem

If you see an FAI that you want to view the contents of for example the OWA.UserOptions this the cmdlet you can use to do this

Get-FAIItem -MailboxName mailbox@domain.com -Folder Root -ConfigItemName OWA.UserOptions
eg


Because this FAI is a Roaming Directory this is what is returned to the pipeline.

If you have an FAI the contains XML like the Category list the cmdlet will return XML back to you which you may or may not be able to work with depending on your skill level in PowerShell. But if you just want to look at the Content in plain text at the cmdlet line you can do the following


Get-FAIItem -MailboxName mailbox@domain.com -Folder Calendar -ConfigItemName CategoryList | Select-Object InnerXML | FL

The other optional parameter for this cmdlet is the -ReturnConfigObject switch which will return the actual UserConfiguration Typed object from the EWS Managed API which is useful if you want to update the FAI in question or your dealing with the BinaryStream which this module doesn't handle. Eg you can use the following script to turn off the FocusedInbox in OWA for a user

$OwaOptions = Get-FAIItem -MailboxName user@domain.com -Folder Root -ConfigItemName OWA.UserOptions -ReturnConfigObject
if($OwaOptions.Dictionary.ContainsKey("IsFocusedInboxEnabled")){  $OwaOptions.Dictionary["IsFocusedInboxEnabled"] = $false  $OwaOptions.Update() }
One last one because it was the topic of a recent conversation is if you want to look to see the user configuration status for the  Focused Inbox in Outlook you can use the following to read the FAI that stores this information.


Get-FAIItem -MailboxName mailbox@domain.com -Folder Inbox -ConfigItemName AccountPrefs
eg this will return something like