我正在尝试导出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变量进行了匿名化,但除此之外,代码就像我发布的那样。
我本来希望在原帖中添加评论,但我还没有足够的声誉。
任何帮助将不胜感激。谢谢。
答案 0 :(得分:0)
LoadPropertiesForItems
期望传递的第一个变量包含items对象,并且该代码中没有填充$result
,这就是您收到错误的原因。您可以从MSDN blog找到相关示例。
我测试了以下哪些内容,用于从连接到名为$exchangeService
的Exchange服务的邮箱的收件箱中获取每封邮件。您需要在此代码中更改几个变量以匹配您的变量。这是故意这样做的,这样你就可以看到事情是如何运作的。
这段代码可能会更好,因为我的经验也很有限。由于并不是很难并且需要完成一个好的测试,我已经用一些正则表达式解决了这个评论: #TODO:清理$ mailSubject以便它是文件系统友好的。这里不完成的一件事是用于计算具有空白主题的文件的逻辑。目前,他们将覆盖彼此,你最终会得到一个像处理过的最后一个“.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的运作方式有了坚实的基础。他也是我不时见过的用户。