by

EWS Contact Vcard Export script

EWS Contact Vcard Export script A few months in the past I posted a Vcard export script for exporting the World Handle listing from Alternate 2013/ExchangeOnline utilizing EWS’s findPeople operation. To spherical this script out I assumed I would submit one other script that lets you export from a Mailbox’s Contacts Folder utilizing EWS’s in-built capability to export the Contact’s MimeData as a Vcard as defined in http://msdn.microsoft.com/en-us/library/workplace/dn672317(v=exchg.150).aspx .

This script is fairly easy it simply 
  • Binds to the contacts folder of the Goal mailbox
  • Enumerates the contacts within the contacts folders
  • Will get the MimeContent of every of the contacts and saves that as a vcard
To run this script simply cross the primarySMTPAddress of the mailbox you need it to run towards and the listing to export to eg
./exportVcardEWS.ps1 mailbox@area.com c:vcardexports
I’ve put a download of this script right here the code seems like
  1. ## Get the Mailbox to Entry from the 1st commandline argument  
  2.   
  3. $MailboxName = $args[0]  
  4. $exportFolder = $args[1]  
  5.   
  6. ## Load Managed API dll    
  7. Add-Sort -Path “C:Program FilesMicrosoftExchangeWeb Services2.0Microsoft.Exchange.WebServices.dll”    
  8.     
  9. ## Set Alternate Model    
  10. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Alternate2010_SP2    
  11.     
  12. ## Create Alternate Service Object    
  13. $service = New-Object Microsoft.Alternate.WebServices.Information.ExchangeService($ExchangeVersion)    
  14.     
  15. ## Set Credentials to use two choices are availible Option1 to use explict credentials or Choice 2 use the Default (logged On) credentials    
  16.     
  17. #Credentials Choice 1 utilizing UPN for the home windows Account    
  18. $psCred = Get-Credential    
  19. $creds = New-Object System.Web.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())    
  20. $service.Credentials = $creds        
  21.     
  22. #Credentials Choice 2    
  23. #service.UseDefaultCredentials = $true    
  24.     
  25. ## Select to ignore any SSL Warning points prompted by Self Signed Certificates    
  26.     
  27. ## Code From http://poshcode.org/624  
  28. ## Create a compilation surroundings  
  29. $Supplier=New-Object Microsoft.CSharp.CSharpCodeProvider  
  30. $Compiler=$Supplier.CreateCompiler()  
  31. $Params=New-Object System.CodeDom.Compiler.CompilerParameters  
  32. $Params.GenerateExecutable=$False  
  33. $Params.GenerateInMemory=$True  
  34. $Params.IncludeDebugInformation=$False  
  35. $Params.ReferencedAssemblies.Add(“System.DLL”) | Out-Null  
  36.   
  37. $TASource=@ 
  38.   namespace Native.ToolkitExtensions.Web.CertificatePolicy{ 
  39.     public class TrustAll : System.Web.ICertificatePolicy { 
  40.       public TrustAll() {  
  41.       } 
  42.       public bool CheckValidationResult(System.Web.ServicePoint sp, 
  43.         System.Safety.Cryptography.X509Certificates.X509Certificates cert,  
  44.         System.Web.WebRequest req, int drawback) { 
  45.         return true; 
  46.       } 
  47.     } 
  48.   } 
  49. @   
  50. $TAResults=$Supplier.CompileAssemblyFromSource($Params,$TASource)  
  51. $TAAssembly=$TAResults.CompiledAssembly  
  52.   
  53. ## We now create an occasion of the TrustAll and connect it to the ServicePointManager  
  54. $TrustAll=$TAAssembly.CreateInstance(“Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll”)  
  55. [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll  
  56.   
  57. ## finish code from http://poshcode.org/624  
  58.     
  59. ## Set the URL of the CAS (Consumer Entry Server) to use two choices are availbe to use Autodiscover to discover the CAS URL or Hardcode the CAS to use    
  60.     
  61. #CAS URL Choice 1 Autodiscover    
  62. $service.AutodiscoverUrl($MailboxName,{$true})    
  63. “Using CAS Server : “ + $Service.url     
  64.      
  65. #CAS URL Choice 2 Hardcoded    
  66.     
  67. #$uri=[system.URI] “https://casservername/ews/exchange.asmx”    
  68. #$service.Url = $uri      
  69.     
  70. ## Optionally available part for Alternate Impersonation    
  71.     
  72. #$service.ImpersonatedUserId = new-object Microsoft.Alternate.WebServices.Information.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)   
  73.   
  74. # Bind to the Contacts Folder  
  75.   
  76. Perform Take away-InvalidFileNameChars {  
  77.   param(  
  78.     [Parameter(Obligatory=$true,  
  79.       Place=0,  
  80.       ValueFromPipeline=$true,  
  81.       ValueFromPipelineByPropertyName=$true)]  
  82.     [String]$Identify  
  83.   )  
  84.   
  85.   $invalidChars = [IO.Path]::GetInvalidFileNameChars() -join   
  86.   $re = “[{0}]” -f [RegEx]::Escape($invalidChars)  
  87.   return ($Identify -replace $re)  
  88. }  
  89.   
  90. $folderid= new-object Microsoft.Alternate.WebServices.Information.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Contacts,$MailboxName)     
  91. $Contacts = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)  
  92.   
  93. #Outline ItemView to retrive simply 50 Gadgets      
  94. $ivItemView =  New-Object Microsoft.Alternate.WebServices.Information.ItemView(50)      
  95. $fiItems = $null      
  96. do{      
  97.     $fiItems = $service.FindItems($Contacts.Id,$ivItemView)      
  98.     $psPropset= new-object Microsoft.Alternate.WebServices.Information.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)    
  99.     $psPropset.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::MimeContent);  
  100.     [Void]$service.LoadPropertiesForItems($fiItems,$psPropset)    
  101.     foreach($Merchandise in $fiItems.Gadgets){  
  102.         if($Merchandise -is [Microsoft.Exchange.WebServices.Data.Contact]){  
  103.             $fileDisplay = $Merchandise.Topic  
  104.             $fileDisplay = Take away-InvalidFileNameChars($fileDisplay)  
  105.             $fileName =  $exportFolder + “” + $Merchandise.Topic + “” + [Guid]::NewGuid().ToString() + “.vcf 
  106.             [System.IO.File]::WriteAllBytes($fileName,$Merchandise.MimeContent.Content material) 
  107.             “Exported ” + $fileName  
  108.         }  
  109.     }      
  110.     $ivItemView.Offset += $fiItems.Gadgets.Depend      
  111. }whereas($fiItems.MoreAvailable -eq $true)