none
Create contacts in mailboxes via Powershell?

    Question

  • Hi

    I have a problem, some of you may have a solution on.

    We have a customerdb in SQL which contains all our customers, all 16K! of them

    These customers have to be populated into a new folder, under Contacts, in approx 30 mailboxes. (Exch2013)

    I have managed to read the data from SQL and create the Contact-older in each mailbox, by using EWS.

    But they problem is that it takes approx. 1 hour(57min) to populate the ContactFolder per Mailbox. (and with only 24h per day, it cannot run each day in the state it is today)

    I was wondering if it is possible to solve this another way?

    I could dump the contacts into a csv-file, but I cannot find a script or an exchangecommand, that could import this csv other, than using EWS that I have already done.

    I dont have the possibility to have each user import the csv from their Outlook directly, it have to be centralized.

    Anyone that have a suggestion, or a link to an exchangecommand/powershellscript that may solve this?

    Thanks in advance,.

    /Peter



    • Edited by Peter_Moe Wednesday, June 13, 2018 1:42 PM
    Wednesday, June 13, 2018 1:42 PM

All replies

  • If you already have the contacts imported into one mailbox and its going to be the same folder in every mailbox you want to import then into. Probably the quickest method would be to use new-mailboxexportrequest and export just the folder from the Source Mailbox into a PST and the new-mailboximportrequest to import that into each of the target there an example of the syntax needed in https://social.technet.microsoft.com/Forums/en-US/27edcba8-dbfc-4b99-ba52-823961f2f8ca/how-to-copy-items-from-one-mailbox-folder-to-another-mailbox-folder-using-powershell?forum=exchangesvradmin

    Your EWS code is probably slow because your just creating one contact at a time, EWS has batching operations so you can create multiple contacts in one call (with contacts probably between 20-50 in a batch is reasonable depending on the number of properties you setting). Probably still won't be as quick as new-mailboximportrequest but should be a lot quicker.

    Cheers
    Glen

    Wednesday, June 13, 2018 11:31 PM
  • Thanks Glen

    How do I usebatch-operations with EWS?

    My code is nearly identical to this: https://gallery.technet.microsoft.com/office/Importing-Global-Address-dc52a0ce

    I have looked here: https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-process-contacts-in-batches-by-using-ews-in-exchange

    But this code is in C, and I cannot make heads and tails on how to port this to powershell

    Anyway you may help me out here or point me in another direction?

    I'll look into the mailboxexportrequest-method aswell

    Thanks in advance.

    Peter


    • Edited by Peter_Moe Thursday, June 14, 2018 9:12 AM
    Thursday, June 14, 2018 9:02 AM
  • Where you have $contact.save() at the moment you will need to rewrite this and work that logic so you build a batch of items using a Generic list and then use the CreateItems method of the ExchangeService object https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.exchangeservice.createitems(v=exchg.80).aspx

    Because your submitting the creates in batches you should then write code to process the results and handling throttling if necessary as you may find that the batch got half way and the server throttled the response etc so then some of the items in the batch need to be retried. If your going to be doing this on a regular basis then it worth investing the time to make it robust.

    Cheers
    Glen

    Friday, June 15, 2018 2:48 AM
  • Thanks alot Glen.

    One more(hopefully the last),

    I'm trying to batch just 10 contacts to try out this CreateItems function, but I get an odd error:

    .....
    $ContactsFolder = [Microsoft.Exchange.WebServices.Data.ContactsFolder]::Bind($service,$ContactsFolderSearch.Id);
    
    $p0=$ContactsListe[0..10]
    $myarray = New-Object 'System.Collections.Generic.List[Microsoft.Exchange.WebServices.Data.Item]'
    
    foreach ($t in $p0) {
     $myarray.add($t)
     }
     $service.CreateItems($myarray, $ContactsFolder.Id, $null, $null);
    
    =>
    Exception calling "CreateItems" with 4 arguments: "This operation can't be performed because at least one item already has an ID."

    I have search on Google, but havent found anything regarding this error, do you have a clue what this mean?

    Thanks in advance,

    Peter

    Friday, June 15, 2018 12:57 PM