none
Exporting Outlook Contacts with Exchange Powershell RRS feed

  • Question

  • Hi

    The following ps1 code is a fucntion to export contacts details from users mailboxes. The bold part in the function are the fields that are exporting ok - First name and surname. However I also want to export the following and can't find the correct syntax:

    Title
    Phone 2 Selected
    Phone 4 Selected
    Phone 1 Selected
    Home Address Street
    Home Address City
    Home Address State
    Home Address Country/Region
    Home Address Postal Code
    Spouse/Partner
    Primary Phone
    E-mail Selected
    E-mail 2

    This is from a custom form but the fields are default contact fields as far as I'm aware. Are these extended fields - how do i access? Can you help please?

    $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.0\Microsoft.Exchange.WebServices.dll"
    [void][Reflection.Assembly]::LoadFile($dllpath)
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)

    $windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
    $sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">"
    $aceuser = [ADSI]$sidbind

    $service.AutodiscoverUrl($aceuser.mail.ToString())
    #Header rows
    "[user that created the booking]"
    #traverse mailbox and pull out all appointments

    function getcontacts ([string]$mailboxname)

    {
    $ContactsFolderid  = new-object  Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Contacts,$mailboxname)
    $view = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)
    $findResults = $Service.FindItems($ContactsFolderid,$view)
    "---" + $MailboxName+ "----"
     
     foreach($item in  $findResults)
     {

    $($item.GivenName)+","+$($item.Surname)

    }
    }

    Friday, June 24, 2011 8:41 AM

Answers

  • You may want to look at the objects your using in an Object browser (you can download visual studio express for free) which will help out in powershell you need to access the particular collections your after eg in powershell something like
     	$($item.GivenName)+","+$($item.Surname)
     	$item.PhysicalAddresses[[Microsoft.Exchange.WebServices.Data.PhysicalAddressKey]::Home].City
    	$item.EmailAddresses[[Microsoft.Exchange.WebServices.Data.EmailAddressKey]::EmailAddress1].Address
    	$item.EmailAddresses[[Microsoft.Exchange.WebServices.Data.EmailAddressKey]::EmailAddress1].Name
    	$item.PhoneNumbers[[Microsoft.Exchange.WebServices.Data.PhoneNumberKey]::PrimaryPhone]
    

    Some properties you may need to use extended properties for i would suggest you look at using a Mapi editor like Outlook Spy or MFCMapi32 to view the objects you working with so you can work out what particular properties you need to access. If you seach you'll find plenty examples of using extendedpropertydefinition with powershell
    Cheers
    Glen
     
    • Marked as answer by Ben Ashdown Monday, July 4, 2011 1:00 PM
    Friday, June 24, 2011 1:18 PM
  • What you should do is check if the collection is null then use TryGetProperty eg

    	$HomeAddress = $null
    	if($item.PhysicalAddresses -ne $null){
    		if($item.PhysicalAddresses.TryGetValue([Microsoft.Exchange.WebServices.Data.PhysicalAddressKey]::Home,[ref]$HomeAddress)){
    	 		$HomeAddress.City
    		}
    	}
    

    Cheers
    Glen

    • Marked as answer by Ben Ashdown Monday, July 4, 2011 1:00 PM
    Friday, July 1, 2011 3:52 AM

All replies

  • You may want to look at the objects your using in an Object browser (you can download visual studio express for free) which will help out in powershell you need to access the particular collections your after eg in powershell something like
     	$($item.GivenName)+","+$($item.Surname)
     	$item.PhysicalAddresses[[Microsoft.Exchange.WebServices.Data.PhysicalAddressKey]::Home].City
    	$item.EmailAddresses[[Microsoft.Exchange.WebServices.Data.EmailAddressKey]::EmailAddress1].Address
    	$item.EmailAddresses[[Microsoft.Exchange.WebServices.Data.EmailAddressKey]::EmailAddress1].Name
    	$item.PhoneNumbers[[Microsoft.Exchange.WebServices.Data.PhoneNumberKey]::PrimaryPhone]
    

    Some properties you may need to use extended properties for i would suggest you look at using a Mapi editor like Outlook Spy or MFCMapi32 to view the objects you working with so you can work out what particular properties you need to access. If you seach you'll find plenty examples of using extendedpropertydefinition with powershell
    Cheers
    Glen
     
    • Marked as answer by Ben Ashdown Monday, July 4, 2011 1:00 PM
    Friday, June 24, 2011 1:18 PM
  • Thanks - I got an error when i added the City for those whose City was BLANK:

    Unable to index into an object of type Microsoft.Exchange.WebServices.Data.PhysicalAddressDictionary.
    At C:\CustomContactFormExtraction.ps1:33 char:31
    + $City=$item.PhysicalAddresses[[ <<<< Microsoft.Exchange.WebServices.Data.PhysicalAddressKey]::Home].City

    How do i parse this out?

    Many thanks

     

     

    Thursday, June 30, 2011 1:51 PM
  • What you should do is check if the collection is null then use TryGetProperty eg

    	$HomeAddress = $null
    	if($item.PhysicalAddresses -ne $null){
    		if($item.PhysicalAddresses.TryGetValue([Microsoft.Exchange.WebServices.Data.PhysicalAddressKey]::Home,[ref]$HomeAddress)){
    	 		$HomeAddress.City
    		}
    	}
    

    Cheers
    Glen

    • Marked as answer by Ben Ashdown Monday, July 4, 2011 1:00 PM
    Friday, July 1, 2011 3:52 AM
  • Many thanks you're a star!
    Monday, July 4, 2011 1:00 PM