Saving messages to exchange online script error

Hello All,

I am working on a script to Upload EML messages to Exchange online, the script works fine if you are going to upload single EML message,  but if you tried to loop through the EML messages (10,000) you will get an error  

"Exception calling "Save" with "1" argument(s): "This operation can't be performed because this service object already has an ID. To update this service object, use the Update() method instead."


and if you used the Update() method you will get this error

"Cannot convert argument "conflictResolutionMode", with value: "AAMkADQzYzJlYmY4LWQ0MTktNDI1OC1hMzk5LTU1NjY4MzEyZDllYQAuAAAAAAC9KBW4BKndR6JlMKNDzhBDAQB8rggXOc1YSrfw79e+XiMYAAAAxmsZAAA=", for "Update" to type
"Microsoft.Exchange.WebServices.Data.ConflictResolutionMode": "Cannot convert the "AAMkADQzYzJlYmY4LWQ0MTktNDI1OC1hMzk5LTU1NjY4MzEyZDllYQAuAAAAAAC9KBW4BKndR6JlMKNDzhBDAQB8rggXOc1YSrfw79e+XiMYAAAAxmsZAAA=" value of type
"Microsoft.Exchange.WebServices.Data.FolderId" to type "Microsoft.Exchange.WebServices.Data.ConflictResolutionMode".


I tried to make 1 save action and then update but also i failed

could you help please


$UserName = "user@domain.com"
$Password = "password"
$secpassword = ConvertTo-SecureString $Password -AsPlainText -Force
$adminCredential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName,$secpassword   

If(Get-PSSession | where-object {$_.ConfigurationName -eq "Microsoft.Exchange"}){
	write-host "Session Exists"
}
else{
	$rpRemotePowershell = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -credential $adminCredential  -Authentication Basic -AllowRedirection 
	$importresults = Import-PSSession $rpRemotePowershell 
} 


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

$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">"
$aceuser = [ADSI]$sidbind
$service.Credentials = New-Object System.Net.NetworkCredential($username,$password)
$service.AutodiscoverUrl("user@tenant.onmicrosoft.com" ,{$true})
$MailboxName="taheito@mydomain.com"
$folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)     
$Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)  
$emUploadEmail = new-object Microsoft.Exchange.WebServices.Data.EmailMessage($service)  
$EMLsFiles = Get-ChildItem -Path "D:\Temp_Files\" -Recurse  
Foreach ($EMLItem in $EMLsFiles){
[byte[]]$bdBinaryData1 = [System.IO.File]::ReadAllBytes($EMLItem.FullName)  
$emUploadEmail.MimeContent = new-object Microsoft.Exchange.WebServices.Data.MimeContent("us-ascii", $bdBinaryData1); 
$PR_Flags = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(3591, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);  
$emUploadEmail.SetExtendedProperty($PR_Flags,"1")  
$emUploadEmail.Save($Inbox.Id)


 }
 


March 14th, 2015 4:28am

Hi,

According to your description, I understand that failed to batch import XML file to Exchange Online, however it works fine when import only single one XML file.
If I misunderstand your concern, please do not hesitate to let me know.

We can use EWS to batch import .eml and .ics file to Exchange server. More details about Import items by using EWS in Exchange, for your reference:
https://msdn.microsoft.com/en-us/library/office/dn672319(v=exchg.150).aspx#sectionSection3
For your question, it may be related to script of Exchange server. I recommend you to contact Scripting for Exchange Server Team so that you can get more professional suggestion, please refer to:
https://technet.microsoft.com/en-us/scriptcenter/dd742246.aspx?f=255&MSPPError=-2147217396

Thanks

Free Windows Admin Tool Kit Click here and download it now
March 17th, 2015 2:12am

Hi,

According to your description, I understand that failed to batch import EML file to Exchange Online, however it works fine when import only single one EML file.
If I misunderstand your concern, please do not hesitate to let me know.

We can use EWS to batch import .eml and .ics file to Exchange server. More details about Import items by using EWS in Exchange, for your reference:
https://msdn.microsoft.com/en-us/library/office/dn672319(v=exchg.150).aspx#sectionSection3
For your question, it may be related to script of Exchange server. I recommend you to contact Scripting for Exchange Server Team so that you can get more professional suggestion, please refer to:
https://technet.microsoft.com/en-us/scriptcenter/dd742246.aspx?f=255&MSPPError=-2147217396

March 17th, 2015 6:10am

Hello,

actually i was trying to import EML files not XML and yes i fail when i try to import it as a bulk

however i found a workaround but it took time around 30% more

i will post it there

thnx

Free Windows Admin Tool Kit Click here and download it now
March 17th, 2015 10:35am

Hi,

Any updates about this questions? Please share the result about your workaround.

Thanks

April 6th, 2015 10:14pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics