【发布时间】:2016-03-09 05:21:08
【问题描述】:
我需要从 Outlook 365 邮箱中提取附件。
我在这里关注这个例子(Powershell)
https://gallery.technet.microsoft.com/office/Export-Email-Messages-from-1419bbe9
我运行良好。此示例提取整个消息并保存到 .EML 文件中。
要获取附件,我可以在之后在 .EML 文件上运行一些其他脚本(我真的不想这样做),或者我可以使用 Powershell 直接提取附件
所以我正在尝试使用 powershell,但是正在使用的 API 上的 doco 比我习惯的要稀疏
基本上我遇到的问题是 sample 代码通过调用它来保存电子邮件(简化代码)
# not sure if this is relevant. I think so
$itemPropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet(
[Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties,
[Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::MimeContent)
$emailMsg.Load($itemPropertySet)
# This actually saves it
$emailContent = $emailMsg.MimeContent.Content
[System.IO.File]::WriteAllBytes("$Path\$fileName.eml",$emailContent)
但是当我对附件尝试相同类型的代码时,MimeContent.Content 结果为 NULL
我认为是因为我需要使用正确的参数调用Attachment.Load 来将附件拖到本地?但是文档没有帮助。
所以无论如何这里有一些经过简化的清理代码。
此代码打印出附件名称,但对于每个附件,我都会收到这些错误
使用“1”参数调用“Load”的异常:“空路径名不合法。
这是因为我没有正确调用 Load
Export-OSCEXOEmailAttachment:使用“2”参数调用“WriteAllBytes”的异常:“值不能为空。
这是因为用于保存附件字节的变量为空。
还有谁能告诉我 - 每次我更改代码时,我是否需要 Remove-Module 和 Import-Module 来刷新?我怀疑有一种方法可以直接运行脚本而无需导入它
代码如下
Function Export-OSCEXOEmailAttachment
{
[cmdletbinding()]
Param
(
#Define parameters
[Parameter(Mandatory=$true,Position=1,ValueFromPipeline=$true)]
[Microsoft.Exchange.WebServices.Data.SearchFolder]$SearchFolder,
[Parameter(Mandatory=$true,Position=2)]
[string]$Path,
[Parameter(Mandatory=$false,Position=3)]
[int]$PageSize=100,
[Parameter(Mandatory=$false)]
[switch]$AllowOverwrite,
[Parameter(Mandatory=$false)]
[switch]$KeepSearchFolder
)
Begin
{
#Verify the existence of exchange service object
#This bit of code (removed)
#validates that variable $exService is initialised
#Load necessary properties for email messages
#Not certain what this is for. Does this indicate which particular properties are loaded?
$itemPropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet(`
[Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties,
[Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::MimeContent)
#Load properties for attachments. Do we need to do this to get Mime.Content??
$attachmentPropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet(`
[Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties,
[Microsoft.Exchange.WebServices.Data.Attachment]::MimeContent)
}
Process
{
#Define the view settings in a folder search operation.
$itemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView($PageSize)
#Iterate each item in the search folder
do
{
$findResults = $SearchFolder.FindItems($itemView)
foreach ($findResult in $findResults) {
#Bind each email with a small set of PropertySet
$emailMsg = [Microsoft.Exchange.WebServices.Data.EmailMessage]::Bind(`
$exService,$findResult.Id)
$emailMsg.Load($itemPropertySet)
# Addition to original function: now iterate through attachments
foreach ($attachment in $emailMsg.Attachments) {
$ext = [System.IO.Path]::GetExtension($attachment.Name)
if($ext -eq ".xlsx") {
$attachment.Load($attachmentPropertySet)
$exportPath=$Path + "\" + $attachment.Name
Write-Host $exportPath
$attachmentContent = $attachment.MimeContent.Content
#Export attachment
Try
{
[System.IO.File]::WriteAllBytes($exportPath,$attachmentContent)
}
Catch
{
$PSCmdlet.WriteError($_)
}
}
}
}
} while ($findResults.MoreAvailable)
}
End
}
这是使用此处https://msdn.microsoft.com/EN-US/library/office/dn726695(v=exchg.150).aspx的 API 在 C# 中保存附件的代码示例
public static void SaveEmailAttachment(ExchangeService service, ItemId itemId)
{
// Bind to an existing message item and retrieve the attachments collection.
// This method results in an GetItem call to EWS.
EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(ItemSchema.Attachments));
foreach (Attachment attachment in message.Attachments)
{
if (attachment is ItemAttachment)
{
ItemAttachment itemAttachment = attachment as ItemAttachment;
itemAttachment.Load(ItemSchema.MimeContent);
string fileName = "C:\\Temp\\" + itemAttachment.Item.Subject + ".eml";
// Write the bytes of the attachment into a file.
File.WriteAllBytes(fileName, itemAttachment.Item.MimeContent.Content);
Console.WriteLine("Email attachment name: "+ itemAttachment.Item.Subject + ".eml");
}
}
}
所有这些“模式”是怎么回事?问题就在那里,但我不明白
【问题讨论】:
标签: powershell outlook office365api