Skip to main content

Export the GAL or Address list with EWS to Vcards in 2013 MEC sample 1

This is the first of the sample scripts I included in my MEC talk last Monday.  This script uses 3 of the new operations in EWS in Exchange 2013 to export all the address list entries of a particular Addresslist to individual Vcards (including the user photo) using just EWS. None of these operations are included in the EWS Managed API, so to use these operations I'm using some Raw SOAP and posting the EWS request using the standard httpwebrequest class in .Net and processing the response in XML.

The FindPeople Operation is used firstly to page through all the entries in an Address list in groups of 100 entries at a time. To use this operation you first need to get the GUID for the address list you want to export and hard code this in the $ABGUID variable eg.

$ABGUID = "6c118670-2f72-4213-944c-ab1e97d63f9b";

To get the GUID for your Global Address List you need to use the Get-GlobalAddressList Cmdlet http://technet.microsoft.com/en-us/library/aa996579%28v=exchg.150%29.aspx or if you want to export one of the other Address lists you can use the Get-AddressList cmdlet http://technet.microsoft.com/en-us/library/aa996782(v=exchg.150).aspx .

eg



To use these cmdlet's in Office365 you need to make sure you are assigned the Address Lists Role in RBAC

eg run

New-ManagementRoleAssignment -Role 'Address Lists' -User username

The FindPeople operation returns the PersonaId for each of the AddressList entries so the next part of the script uses the GetPersona Operation to get all the Address list entries details. Personas are a different way of representing contact data this is outlined in http://msdn.microsoft.com/en-us/library/office/jj190895(v=exchg.150).aspx . This particular script just uses this operation to grab the contact details stored in Active Directory (or Azure AD) for the particular persona (or Address list Entry in question). Currently I haven't worked out a way to batch this operation in EWS (if anyone knows please ping me) so its executing one OP per entry(which is kind of slow).
The last operation the script uses is the GerUserPhoto operation which is just a REST endpoint so it's nice and easy to use. It returns a ByteArray which then gets converted to a Base64 string with LineBreaks so its properly formatted in the VCard file.
To run this script you need to pass it in a Mailbox to run as and as I mentioned before you need to hardcode the GUID of the AddressList you want to export. While none of the operations used to do the export are in the Managed API, I've included in the usually Managed API Autodiscovery routines.  You can also set the location of the export directory in 
$exportFolder = "c:\temp\"
I've put a download of this script here the code itself looks like


  1. ## Get the Mailbox to Access from the 1st commandline argument  
  2.   
  3. $MailboxName = $args[0]  
  4. $exportFolder = "c:\temp\" 
  5.  
  6. $ABGUID = "6c118670-2f72-4213-944c-ab1e97d63f9b"; 
  7.  
  8. ## Load Managed API dll   
  9. Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"   
  10.    
  11. ## Set Exchange Version   
  12. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2   
  13.    
  14. ## Create Exchange Service Object   
  15. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)   
  16.    
  17. ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials   
  18.    
  19. #Credentials Option 1 using UPN for the windows Account   
  20. $psCred = Get-Credential   
  21. $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())   
  22. $service.Credentials = $creds       
  23.    
  24. #Credentials Option 2   
  25. #service.UseDefaultCredentials = $true   
  26.    
  27. ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates   
  28.    
  29. ## Code From http://poshcode.org/624 
  30. ## Create a compilation environment 
  31. $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider 
  32. $Compiler=$Provider.CreateCompiler() 
  33. $Params=New-Object System.CodeDom.Compiler.CompilerParameters 
  34. $Params.GenerateExecutable=$False 
  35. $Params.GenerateInMemory=$True 
  36. $Params.IncludeDebugInformation=$False 
  37. $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null 
  38.  
  39. $TASource=@' 
  40.   namespace Local.ToolkitExtensions.Net.CertificatePolicy{ 
  41.     public class TrustAll : System.Net.ICertificatePolicy { 
  42.       public TrustAll() {  
  43.       } 
  44.       public bool CheckValidationResult(System.Net.ServicePoint sp, 
  45.         System.Security.Cryptography.X509Certificates.X509Certificate cert,  
  46.         System.Net.WebRequest req, int problem) { 
  47.         return true; 
  48.       } 
  49.     } 
  50.   } 
  51. '@  
  52. $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource) 
  53. $TAAssembly=$TAResults.CompiledAssembly 
  54.  
  55. ## We now create an instance of the TrustAll and attach it to the ServicePointManager 
  56. $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll") 
  57. [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll 
  58.  
  59. ## end code from http://poshcode.org/624 
  60.    
  61. ## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use   
  62.    
  63. #CAS URL Option 1 Autodiscover   
  64. $service.AutodiscoverUrl($MailboxName,{$true})   
  65. "Using CAS Server : " + $Service.url    
  66.  
  67.  
  68.     
  69. #CAS URL Option 2 Hardcoded   
  70.    
  71. #$uri=[system.URI] "https://casservername/ews/exchange.asmx"   
  72. #$service.Url = $uri     
  73.    
  74. ## Optional section for Exchange Impersonation   
  75.    
  76. #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)  
  77.  
  78. function getPeopleRequest($offset){ 
  79.  
  80. $request = @"  
  81. <?xml version="1.0" encoding="utf-8"?>  
  82. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
  83. <soap:Header>  
  84. <RequestServerVersion Version="Exchange2013" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" />  
  85. </soap:Header><soap:Body>  
  86. <FindPeople xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"><PersonaShape>  
  87. <BaseShape xmlns="http://schemas.microsoft.com/exchange/services/2006/types">Default</BaseShape>  
  88. </PersonaShape><IndexedPageItemView MaxEntriesReturned="100" Offset="$offset" BasePoint="Beginning" />  
  89. <ParentFolderId>  
  90. <AddressListId Id="$ABGUID" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" />  
  91. </ParentFolderId></FindPeople></soap:Body></soap:Envelope>  
  92. "@ 
  93. return $request 
  94. } 
  95.  
  96. function getPersonaRequest($PersonalId){ 
  97. $request = @"  
  98. <?xml version="1.0" encoding="utf-8"?>  
  99. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"  
  100.                xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">  
  101.   <soap:Header>  
  102.     <t:RequestServerVersion Version="Exchange2013"/>  
  103.   </soap:Header>  
  104.   <soap:Body xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">  
  105.     <GetPersona>  
  106.       <PersonaId Id="$PersonalId"/>  
  107.     </GetPersona>  
  108.   </soap:Body>  
  109. </soap:Envelope>  
  110. "@ 
  111. return $request 
  112. } 
  113.  
  114. function AutoDiscoverPhotoURL{ 
  115.        param ( 
  116.               $EmailAddress="$( throw 'Email is a mandatory Parameter' )", 
  117.               $Credentials="$( throw 'Credentials is a mandatory Parameter' )" 
  118.               ) 
  119.        process{ 
  120.               $version= [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013 
  121.               $adService= New-Object Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService($version); 
  122.               $adService.Credentials = $Credentials 
  123.               $adService.EnableScpLookup=$false; 
  124.               $adService.RedirectionUrlValidationCallback= {$true} 
  125.               $adService.PreAuthenticate=$true; 
  126.               $UserSettings= new-object Microsoft.Exchange.WebServices.Autodiscover.UserSettingName[] 1 
  127.               $UserSettings[0] = [Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::ExternalPhotosUrl 
  128.               $adResponse=$adService.GetUserSettings($EmailAddress, $UserSettings) 
  129.               $PhotoURI= $adResponse.Settings[[Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::ExternalPhotosUrl] 
  130.               return $PhotoURI.ToString() 
  131.        } 
  132. } 
  133.  
  134. $Script:PhotoURL = AutoDiscoverPhotoURL -EmailAddress $MailboxName  -Credentials $creds 
  135. Write-host ("Photo URL : " + $Script:PhotoURL)  
  136.  
  137. function ProcessPersona($PersonalId){ 
  138.  
  139.  
  140.     $personalRequest = getPersonaRequest ($PersonalId) 
  141.     $mbMailboxFolderURI = New-Object System.Uri($service.url)   
  142.     $wrWebRequest = [System.Net.WebRequest]::Create($mbMailboxFolderURI)   
  143.     $wrWebRequest.CookieContainer =  New-Object System.Net.CookieContainer    
  144.     $wrWebRequest.KeepAlive = $false;   
  145.     $wrWebRequest.Headers.Set("Pragma", "no-cache");   
  146.     $wrWebRequest.Headers.Set("Translate", "f");   
  147.     $wrWebRequest.Headers.Set("Depth", "0");   
  148.     $wrWebRequest.ContentType = "text/xml";   
  149.     $wrWebRequest.ContentLength = $expRequest.Length;   
  150.     $wrWebRequest.Timeout = 90000;   
  151.     $wrWebRequest.Method = "POST";   
  152.     $wrWebRequest.Credentials = $creds   
  153.     $wrWebRequest.UserAgent = "EWS Script" 
  154.      
  155.     $bqByteQuery = [System.Text.Encoding]::ASCII.GetBytes($personalRequest);   
  156.     $wrWebRequest.ContentLength = $bqByteQuery.Length;   
  157.     $rsRequestStream = $wrWebRequest.GetRequestStream();   
  158.     $rsRequestStream.Write($bqByteQuery, 0, $bqByteQuery.Length);   
  159.     $rsRequestStream.Close();   
  160.     $wrWebResponse = $wrWebRequest.GetResponse();   
  161.     $rsResponseStream = $wrWebResponse.GetResponseStream()   
  162.     $sr = new-object System.IO.StreamReader($rsResponseStream);   
  163.     $rdResponseDocument = New-Object System.Xml.XmlDocument   
  164.     $rdResponseDocument.LoadXml($sr.ReadToEnd());   
  165.     $Persona =@($rdResponseDocument.getElementsByTagName("Persona"))  
  166.     $Persona 
  167.     $DisplayName = ""; 
  168.     if($Persona.DisplayName -ne $null){ 
  169.         $DisplayName = $Persona.DisplayName."#text"  
  170.     }   
  171.     $fileName =  $exportFolder + $DisplayName + "-" + [Guid]::NewGuid().ToString() + ".vcf" 
  172.     add-content -path $filename "BEGIN:VCARD" 
  173.     add-content -path $filename "VERSION:2.1" 
  174.     $givenName = "" 
  175.     if($Persona.GivenName -ne $null){ 
  176.         $givenName = $Persona.GivenName."#text"  
  177.     }  
  178.     $surname = "" 
  179.     if($Persona.Surname -ne $null){ 
  180.         $surname = $Persona.Surname."#text"  
  181.     }  
  182.     add-content -path $filename ("N:" + $surname + ";" + $givenName) 
  183.     add-content -path $filename ("FN:" + $Persona.DisplayName."#text")  
  184.     $Department = ""; 
  185.     if($Persona.Department -ne $null){ 
  186.         $Department = $Persona.Department."#text"  
  187.     }  
  188.     if($Persona.EmailAddress -ne $null){  
  189.         add-content -path $filename ("EMAIL;PREF;INTERNET:" + $Persona.EmailAddress.EmailAddress) 
  190.     } 
  191.     $CompanyName = ""; 
  192.     if($Persona.CompanyName -ne $null){ 
  193.         $CompanyName = $Persona.CompanyName."#text"  
  194.     }  
  195.     add-content -path $filename ("ORG:" + $CompanyName + ";" + $Department)  
  196.     if($Persona.Titles -ne $null){ 
  197.         add-content -path $filename ("TITLE:" + $Persona.Titles.StringAttributedValue.Value) 
  198.     } 
  199.     if($Persona.MobilePhones -ne $null){ 
  200.         add-content -path $filename ("TEL;CELL;VOICE:" + $Persona.MobilePhones.PhoneNumberAttributedValue.Value.Number)      
  201.     } 
  202.     if($Persona.HomePhones -ne $null){ 
  203.         add-content -path $filename ("TEL;HOME;VOICE:" + $Persona.HomePhones.PhoneNumberAttributedValue.Value.Number)        
  204.     } 
  205.     if($Persona.BusinessPhoneNumbers -ne $null){ 
  206.         add-content -path $filename ("TEL;WORK;VOICE:" + $Persona.BusinessPhoneNumbers.PhoneNumberAttributedValue.Value.Number)      
  207.     } 
  208.     if($Persona.WorkFaxes -ne $null){ 
  209.         add-content -path $filename ("TEL;WORK;FAX:" + $Persona.WorkFaxes.PhoneNumberAttributedValue.Value.Number) 
  210.     } 
  211.     if($Persona.BusinessHomePages -ne $null){ 
  212.         add-content -path $filename ("URL;WORK:" + $Persona.BusinessHomePages.StringAttributedValue.Value) 
  213.     } 
  214.     if($Persona.BusinessAddresses -ne $null){ 
  215.         $Country = $Persona.BusinessAddresses.PostalAddressAttributedValue.Value.Country 
  216.         $City = $Persona.BusinessAddresses.PostalAddressAttributedValue.Value.City 
  217.         $Street = $Persona.BusinessAddresses.PostalAddressAttributedValue.Value.Street 
  218.         $State = $Persona.BusinessAddresses.PostalAddressAttributedValue.Value.State 
  219.         $PCode = $Persona.BusinessAddresses.PostalAddressAttributedValue.Value.PostalCode 
  220.         $addr =  "ADR;WORK;PREF:;" + $Country + ";" + $Street + ";" +$City + ";" + $State + ";" + $PCode + ";" + $Country 
  221.         add-content -path $filename $addr 
  222.     } 
  223.     try{ 
  224.         $PhotoSize = "HR96x96 
  225.         $PhotoURL= $Script:PhotoURL + "/GetUserPhoto?email="  + $Persona.EmailAddress.EmailAddress + "&size=" + $PhotoSize; 
  226.         $wbClient = new-object System.Net.WebClient 
  227.         $wbClient.Credentials = $creds 
  228.         $photoBytes = $wbClient.DownloadData($PhotoURL); 
  229.         add-content -path $filename "PHOTO;ENCODING=BASE64;TYPE=JPEG:" 
  230.         $ImageString = [System.Convert]::ToBase64String($photoBytes,[System.Base64FormattingOptions]::InsertLineBreaks) 
  231.         add-content -path $filename $ImageString 
  232.         add-content -path $filename "`r`n"   
  233.     } 
  234.     catch{ 
  235.  
  236.     } 
  237.     add-content -path $filename "END:VCARD" 
  238.  
  239. } 
  240.  
  241. $peopleCollection = @() 
  242. $offset = 0; 
  243.  
  244. do{ 
  245.     $mbMailboxFolderURI = New-Object System.Uri($service.url)   
  246.     $wrWebRequest = [System.Net.WebRequest]::Create($mbMailboxFolderURI)   
  247.     $wrWebRequest.CookieContainer =  New-Object System.Net.CookieContainer    
  248.     $wrWebRequest.KeepAlive = $false;   
  249.     $wrWebRequest.Useragent = "EWS Script" 
  250.     $wrWebRequest.Headers.Set("Pragma", "no-cache");   
  251.     $wrWebRequest.Headers.Set("Translate", "f");   
  252.     $wrWebRequest.Headers.Set("Depth", "0");   
  253.     $wrWebRequest.ContentType = "text/xml";   
  254.     $wrWebRequest.ContentLength = $expRequest.Length;   
  255.     $wrWebRequest.Timeout = 60000;   
  256.     $wrWebRequest.Method = "POST";   
  257.     $wrWebRequest.Credentials = $creds   
  258.  
  259.     $fpRequest = getPeopleRequest ($offset) 
  260.     $bqByteQuery = [System.Text.Encoding]::ASCII.GetBytes($fpRequest);   
  261.     $wrWebRequest.ContentLength = $bqByteQuery.Length;   
  262.     $rsRequestStream = $wrWebRequest.GetRequestStream();   
  263.     $rsRequestStream.Write($bqByteQuery, 0, $bqByteQuery.Length);   
  264.     $rsRequestStream.Close();   
  265.     $wrWebResponse = $wrWebRequest.GetResponse();   
  266.     $rsResponseStream = $wrWebResponse.GetResponseStream()   
  267.     $sr = new-object System.IO.StreamReader($rsResponseStream);   
  268.     $rdResponseDocument = New-Object System.Xml.XmlDocument   
  269.     $rdResponseDocument.LoadXml($sr.ReadToEnd());   
  270.     $totalCount = @($rdResponseDocument.getElementsByTagName("TotalNumberOfPeopleInView"))  
  271.     $Personas =@($rdResponseDocument.getElementsByTagName("Persona"))   
  272.     Write-Host ("People Count : " + $Personas.Count) 
  273.     $offset += $Personas.Count 
  274.     foreach($persona in $Personas){ 
  275.         if($persona.PersonaType -eq "Person"){ 
  276.             ProcessPersona($persona.PersonaId.Id.ToString())  
  277.         } 
  278.     } 
  279.     [Int32]$tc = $totalCount."#text"  
  280.     Write-Host ("Offset: " + $offset) 
  281.     Write-Host ("Total count: " + $tc)  
  282.   
  283. }while($tc -gt $offset)  

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.