【发布时间】: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