我可以使用PowerShell将电子邮件附件从Office 365保存到文件共享吗? [英] Can I save an email attachment from Office 365 to a file share with PowerShell?

查看:52
本文介绍了我可以使用PowerShell将电子邮件附件从Office 365保存到文件共享吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've seen several solutions that use PowerShell to automate the local Outlook client but I want this to run server-side: Reach out to the server with a given account, check for unread messages, save any attachments to a file share and mark the message read.

推荐答案

这需要要安装的Exchange托管服务API .

$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()
    }
}

这篇关于我可以使用PowerShell将电子邮件附件从Office 365保存到文件共享吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆