在两个EWS服务器之间复制项目 - Powershell

时间:2015-08-07 11:17:13

标签: powershell exchangewebservices

我试图进入EWS,我的目标是在两个不同位置的两个Exchange邮箱之间复制数据。

我绑定了两个收件箱文件夹,每个文件夹位于不同的邮箱中 然后我获取Source inbox文件夹中的项目,并尝试将此项目复制到目标收件箱文件夹,这里是代码。

Add-Type -Path "C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"  

## Remote Connection ##

$MailboxName="User@Domain.com"
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1
$Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials("User","Password","Domain")
$exchService = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
$exchService.Credentials = $Credentials

$exchService.Url = "https://Domain.com/EWS/Exchange.asmx"

## Bind Remote Inbox Folder

$FolderName="Inbox" 
$folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::$FolderName,$MailboxName)  
$MailFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchservice,$folderid)

## Local Connection ##

$LocalMailboxName = "LocalUser@Domain.local"
$LocalExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
$LocalExchService = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($LocalExchangeVersion)
$LocalExchService.UseDefaultCredentials = $true
$LocalExchService.AutodiscoverUrl($LocalMailboxName)
$LocalExchService.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $LocalMailboxName) 
$LocalFolderName="Inbox" 
$Localfolderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::$LocalFolderName,$LocalMailboxName)  
$LocalMailFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($LocalExchService,$Localfolderid)

## Get the items of the source mailbox "Inbox"

$ivItemView =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(100)
$fiItems = $exchservice.FindItems($MailFolder.id,$ivItemView)

## The Copy Section

foreach ($item in $fiItems) 
{
$item.Copy($LocalMailFolder.Id)
}

然后我收到此错误:

Exception calling "Copy" with "1" argument(s): "No mailbox with such guid."
At line:1 char:30
+ foreach ($item in $fiItems) {$item.Copy($LocalMailFolder.Id)}
+                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ServiceResponseException

感谢任何帮助,谢谢

1 个答案:

答案 0 :(得分:2)

您无法使用复制在Exchange Services之间复制邮件,该错误只是意味着远程服务不知道本地文件夹ID - 因为该文件夹(具有该ID)在远程服务上不存在。

您需要加载项目,在本地文件夹中创建一个新项目,将旧项目中的所有属性复制到新项目,然后保存新项目。

由于我不知道Powershell,这里有一个C#列表应该让你入门,而不是:

foreach ($item in $fiItems) 
{
$item.Copy($LocalMailFolder.Id)
}

- >

foreach (var remoteItem in fiItems) 
{ 
   var localItem = new Item(LocalExchService); 
   localItem.Subject = remoteItem.Subject;
   localItem.Body = remoteItem.Body;
   // ... and so on. need to cast to the correct type, to access ALL properties, too.
   localItem.Save(LocalMailFolder);
}