如何使用PowerShell和EWS将邮件导出到EML或MSG文件第2部分



我正在尝试导出Exchange 2010中特定帐户收件箱中的所有邮件。我在另一篇文章中找到了这个解决方案,如何使用PowerShell和EWS将邮件导出到EML或MSG文件,看起来很有前途,但是在运行脚本时出现错误。我是使用 EWS 的新手,我有点迷茫。

这是我当前使用的代码的副本:

add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
$strMailboxName = "Mailbox@domain.com"
$strSaveLocation = "\servershare"
$dllpath = "C:Program FilesMicrosoftExchangeWeb Services1.2Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($dllpath)
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)
$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">"
$aceuser = [ADSI]$sidbind
$service.AutodiscoverUrl($aceuser.mail.ToString())
$MailboxName = get-mailbox -Identity $strMailboxName
$folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName.PrimarySmtpAddress.ToString())
$rootfolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, $folderidcnt)

$offset = 0;
$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(10000, $offset)
$response = $service.LoadPropertiesForItems($results, [Microsoft.Exchange.WebServices.Data.PropertySet]::FirstClassProperties)
foreach ($mail in $results){
if ($mail.ToString() -eq "Microsoft.Exchange.WebServices.Data.EmailMessage") {
$mailSubject = $mail.Subject
$mailProps = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::MimeContent)
$mail.Load($mailProps)
#TODO: clean up $mailSubject so it's filesystem friendly
$fStream = New-Object System.IO.FileStream("$strSaveLocatoin$mailSubject.eml", [System.IO.FileMode]::Create)
$fStream.Write($mail.MimeContent.Content, 0, $mail.MimeContent.Content.Length)
$fStream.Close()
}
}

我收到以下错误:

Exception calling "LoadPropertiesForItems" with "2" argument(s): "Value cannot be null.
Parameter name: items"
At C:PowershellScriptsExchange-SaveEMailAsFile.ps1:23 char:44
+ $response = $service.LoadPropertiesForItems <<<< ($results, [Microsoft.Exchange.WebServices.Data.PropertySet]::FirstC
lassProperties)
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
You cannot call a method on a null-valued expression.
At C:PowershellScriptsExchange-SaveEMailAsFile.ps1:27 char:19
+ if ($mail.ToString <<<< () -eq "Microsoft.Exchange.WebServices.Data.EmailMessage") {
+ CategoryInfo          : InvalidOperation: (ToString:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

我已经在脚本开头对 2 个$str变量进行了匿名化,但除此之外,代码与我发布的那样。

我本来想在原始帖子中添加评论,但我还没有足够的声誉。

任何协助将不胜感激。谢谢。

LoadPropertiesForItems期望传递的第一个变量包含该 items 对象,并且$result未填充在该代码中,因此这就是您收到错误的原因。您可以从 MSDN 博客中看到此示例。

我已经测试了以下内容,这些方法可用于从连接到名为$exchangeService的Exchange服务的邮箱的收件箱中获取每封邮件。您需要更改此代码中的几个变量以匹配您的变量。这是故意这样做的,以便您可以看到事情是如何工作的。

这段代码可能会更好,因为我在这方面的经验也很有限。由于它并不难并且需要完成良好的测试,因此我用一些正则表达式解决了此评论:#TODO:清理$mailSubject因此文件系统友好。这里没有做的一件事是解释具有空白主题的文件的逻辑。目前,它们将相互覆盖,您最终将得到最后一个处理过的最后一个".eml"文件的单个".eml"文件。

# Folder that will contain the eml files. 
$destinationFolder = "E:temptest"
# Create the view. Set for 1000 paged items
$pageSize = 1000
$itemView = [Microsoft.Exchange.WebServices.Data.ItemView]::New($pageSize)
$itemView.PropertySet =  New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$mimeView = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::MimeContent)
# Bind to the root folder
$rootFolderID = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
$boundRootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchangeService,$rootFolderID)
# regex to remove illegal characters in the subject that are not allowed in the ntfs file system
$regex = ([char[]]'<>:"/|?*' | ForEach-Object{[regex]::Escape($_)})-join"|"
# Loop through paging results
do{
# Get the next batch of results
$filterResults = $boundRootFolder.FindItems($itemView)
# We need to process each mail in this set of results
$filterResults.Items | ForEach-Object{
# Get the subject before we change the view
$mailSubject = $_.Subject -replace $regex
# Load the properties for the view create earlier
$_.Load($mimeView)
# Using a file stream to export this single messages mime content 
write-host "$destinationFolder$mailSubject.eml" -ForegroundColor Green
$stream = New-Object System.IO.FileStream("$destinationFolder$mailSubject.eml", [System.IO.FileMode]::Create)
$stream.Write($_.MimeContent.Content, 0, $_.MimeContent.Content.Length)
$stream.Close()
}
# Loop if there are still more results in the filter. Adjust the filter offset. 
$itemView.Offset += $filterResults.Items.Count
} while($filterResults.MoreAvailable)

我的第一个问题是我应该能够同时加载所需的属性,但我不知道该怎么做,因此从技术上讲有两个加载操作。首先在初始查找期间,然后再次循环以获取 MIME 内容。这可能是必须完成的方式,因为如果您尝试一次完成所有操作,则会出现错误

The property MimeContent can't be used in FindItem requests.

我会好好看看 Glen 关于 EWS API 的博客。很多好的信息和例子。当我苦苦挣扎时,这让我对 EWS API 的工作原理有了坚实的基础。他也是我不时看到的用户。

相关内容

  • 没有找到相关文章

最新更新