A couple of months ago I posted a Vcard export script for exporting the Global Address list from Exchange 2013/ExchangeOnline using EWS's findPeople operation. To round this script out I thought I'd post another script that allows you to export from a Mailbox's Contacts Folder using EWS's built in ability to export the Contact's MimeData as a Vcard as explained in http://msdn.microsoft.com/en-us/library/office/dn672317(v=exchg.150).aspx .
This script is pretty simple it just
- Binds to the contacts folder of the Target mailbox
- Enumerates the contacts in the contacts folders
- Gets the MimeContent of each of the contacts and saves that as a vcard
To run this script just pass the primarySMTPAddress of the mailbox you want it to run against and the directory to export to eg
./exportVcardEWS.ps1 mailbox@domain.com c:\vcardexports
I've put a download of this script here the code looks like
- ## Get the Mailbox to Access from the 1st commandline argument
- $MailboxName = $args[0]
- $exportFolder = $args[1]
- ## Load Managed API dll
- Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
- ## Set Exchange Version
- $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
- ## Create Exchange Service Object
- $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
- ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials
- #Credentials Option 1 using UPN for the windows Account
- $psCred = Get-Credential
- $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
- $service.Credentials = $creds
- #Credentials Option 2
- #service.UseDefaultCredentials = $true
- ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates
- ## Code From http://poshcode.org/624
- ## Create a compilation environment
- $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
- $Compiler=$Provider.CreateCompiler()
- $Params=New-Object System.CodeDom.Compiler.CompilerParameters
- $Params.GenerateExecutable=$False
- $Params.GenerateInMemory=$True
- $Params.IncludeDebugInformation=$False
- $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null
- $TASource=@'
- namespace Local.ToolkitExtensions.Net.CertificatePolicy{
- public class TrustAll : System.Net.ICertificatePolicy {
- public TrustAll() {
- }
- public bool CheckValidationResult(System.Net.ServicePoint sp,
- System.Security.Cryptography.X509Certificates.X509Certificate cert,
- System.Net.WebRequest req, int problem) {
- return true;
- }
- }
- }
- '@
- $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
- $TAAssembly=$TAResults.CompiledAssembly
- ## We now create an instance of the TrustAll and attach it to the ServicePointManager
- $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
- [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
- ## end code from http://poshcode.org/624
- ## 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
- #CAS URL Option 1 Autodiscover
- $service.AutodiscoverUrl($MailboxName,{$true})
- "Using CAS Server : " + $Service.url
- #CAS URL Option 2 Hardcoded
- #$uri=[system.URI] "https://casservername/ews/exchange.asmx"
- #$service.Url = $uri
- ## Optional section for Exchange Impersonation
- #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
- # Bind to the Contacts Folder
- Function Remove-InvalidFileNameChars {
- param(
- [Parameter(Mandatory=$true,
- Position=0,
- ValueFromPipeline=$true,
- ValueFromPipelineByPropertyName=$true)]
- [String]$Name
- )
- $invalidChars = [IO.Path]::GetInvalidFileNameChars() -join ''
- $re = "[{0}]" -f [RegEx]::Escape($invalidChars)
- return ($Name -replace $re)
- }
- $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Contacts,$MailboxName)
- $Contacts = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
- #Define ItemView to retrive just 50 Items
- $ivItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(50)
- $fiItems = $null
- do{
- $fiItems = $service.FindItems($Contacts.Id,$ivItemView)
- $psPropset= new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
- $psPropset.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::MimeContent);
- [Void]$service.LoadPropertiesForItems($fiItems,$psPropset)
- foreach($Item in $fiItems.Items){
- if($Item -is [Microsoft.Exchange.WebServices.Data.Contact]){
- $fileDisplay = $Item.Subject
- $fileDisplay = Remove-InvalidFileNameChars($fileDisplay)
- $fileName = $exportFolder + "\" + $Item.Subject + "-" + [Guid]::NewGuid().ToString() + ".vcf"
- [System.IO.File]::WriteAllBytes($fileName,$Item.MimeContent.Content)
- "Exported " + $fileName
- }
- }
- $ivItemView.Offset += $fiItems.Items.Count
- }while($fiItems.MoreAvailable -eq $true)