【问题标题】:How to export mail message to EML or MSG file with PowerShell and EWS part 2如何使用 PowerShell 和 EWS 将邮件消息导出到 EML 或 MSG 文件第 2 部分
【发布时间】:2017-03-23 21:44:00
【问题描述】:

我正在尝试导出 Exchange 2010 中特定帐户的收件箱中的所有邮件。我在另一篇帖子 How to export mail message to EML or MSG file with PowerShell and EWS 中找到了这个解决方案,看起来很有希望,但我在运行脚本时遇到错误。我是使用 EWS 的新手,我有点迷茫。

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

add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010

$strMailboxName = "Mailbox@domain.com"
$strSaveLocation = "\\server\share"

$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.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:\PowershellScripts\Exchange-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:\PowershellScripts\Exchange-SaveEMailAsFile.ps1:27 char:19
+ if ($mail.ToString <<<< () -eq "Microsoft.Exchange.WebServices.Data.EmailMessage") {
    + CategoryInfo          : InvalidOperation: (ToString:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

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

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

我们将不胜感激。谢谢。

【问题讨论】:

  • 错误和代码并不完全适合我。你的代码中有$view,我在你的问题中看到$results
  • 抱歉,我发布了另一个运行的输出。我正在移动一些变量,看看它是否有任何不同。我已经用正确的错误消息编辑了我的原始帖子。我很抱歉。
  • 好的.. 所以$results 没有任何价值,所以会产生你的错误。我认为您仍然混淆了变量。
  • 我完全同意,但是这个脚本是从我原始帖子的链接中复制的,并且没有任何错误的 cmets。我真的希望我可以评论那个原始帖子,因为那些是编写和使用脚本的人。除了 $results 当前为空值这一事实之外,我对 EWS 还不够熟悉,无法遵循此脚本的逻辑。

标签: powershell exchangewebservices exchange-server-2010 eml


【解决方案1】:

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

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

此代码可能会更好,因为我对此的经验也有限。由于这并不难并且需要进行良好的测试,因此我用一些正则表达式解决了这个评论:#TODO: clean up $mailSubject so it's filesystem friendly没有在这里完成的一件事是对具有空白主题的文件进行说明的逻辑。目前,它们将相互覆盖,您最终将得到一个与处理后的最后一个文件相同的“.eml”文件。

# Folder that will contain the eml files. 
$destinationFolder = "E:\temp\test"

# 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's blog on the EWS API。很多很好的信息和例子。当我挣扎时,这让我对 EWS API 的工作原理有了坚实的基础。他也是这里的用户,我时常看到。

【讨论】:

  • 谢谢,我一定会去看看那个博客。感谢您的帮助。
猜你喜欢
  • 2017-07-27
  • 2014-02-28
  • 2011-09-11
  • 2014-10-25
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
  • 2010-11-18
  • 2013-05-09
相关资源
最近更新 更多