Email 我可以将Office 365的电子邮件附件保存到PowerShell的文件共享中吗?

Email 我可以将Office 365的电子邮件附件保存到PowerShell的文件共享中吗?,email,powershell,fileshare,Email,Powershell,Fileshare,我见过几种使用PowerShell自动化本地Outlook客户端的解决方案,但我希望它在服务器端运行:使用给定的帐户访问服务器,检查未读邮件,将所有附件保存到文件共享,并将邮件标记为已读。您是否在Exchange环境中?如果是这样,Exchange Web服务将向网络公开,这是一个选项 这需要安装 $ewsPath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"

我见过几种使用PowerShell自动化本地Outlook客户端的解决方案,但我希望它在服务器端运行:使用给定的帐户访问服务器,检查未读邮件,将所有附件保存到文件共享,并将邮件标记为已读。

您是否在Exchange环境中?如果是这样,Exchange Web服务将向网络公开,这是一个选项

这需要安装

$ewsPath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
Add-Type -Path $ewsPath

$ews = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$cred = (Get-Credential).GetNetworkCredential()
$ews.Credentials = New-Object System.Net.NetworkCredential -ArgumentList $cred.UserName, $cred.Password, $cred.Domain
$ews.AutodiscoverUrl( "user@domain.com", {$true} )
$results = $ews.FindItems(
    "Inbox",
    ( New-Object Microsoft.Exchange.WebServices.Data.ItemView -ArgumentList 100 )
)
$MailItems = $results.Items | where hasattachments

foreach ($MailItem in $MailItems){

    $MailItem.Load()

    foreach($Attachment in $MailItem.Attachments){
        $Attachment.Load()
        $File = new-object System.IO.FileStream(("C:\Temp\” + $attachment.Name.ToString()), [System.IO.FileMode]::Create)
        $File.Write($attachment.Content, 0, $attachment.Content.Length)
        $File.Close()
    }
}

我们处于一个混合Exchange环境中。我正在努力阅读EWS,但如果你有任何快速和肮脏的PowerShell连接并从邮箱中删除电子邮件,那将是一个很大的帮助…哦,嘿,我找到了一些有效的代码。现在看看我能不能去掉附件。谢谢。这很有效。您能否详细介绍一下如何只保存具有给定扩展名的文件附件以及在特定日期和时间之后到达的电子邮件(或者说每15分钟轮询一次电子邮件)?