我正在创建一个PowerShell脚本,用于从邮箱下载所有带有附件的电子邮件,并在成功下载后从邮箱中删除它们。
我目前使用的是https://outlook.office365.com/EWS/ODATA/me/messages端点。打开浏览器并登录总是成功和快速的(不到1秒的响应)。
但是,试图运行以下PowerShell会返回调用-RestMethod和WebClient的超时。
$cred = Get-Credential
$url = "https://outlook.office365.com/EWS/ODATA/me/messages"
Invoke-RestMethod $url -Credential $cred -Method GET -TimeoutSec 100
$wc = New-Object System.Net.WebClient
$wc.Credentials = $cred
$wc.DownloadString("https://outlook.office365.com/EWS/ODATA/me/messages")我在几个测试租户中尝试过这一点,还有一些生产租户也有相同的错误。我也尝试过https://outlook.office365.com/api/v1.0/me/messages,但成功甚至更少(在浏览器中不起作用,PowerShell也不起作用)。
而且,不仅消息端点存在此错误,我还尝试了具有相同结果的https://outlook.office365.com/ews/odata/Me/Calendar/。
有人用这个API成功了吗?
发布于 2015-01-26 15:23:46
听起来你可能遇到了一个已知的问题。如果您增加了超时值,它最终会返回吗?如果是这样的话,并且重复请求,它返回的速度会更快吗?
另外,使用此cmdlet调用端点使用基本身份验证,该身份验证目前在其他端点上启用,但在将来的某个时候将被禁用。
发布于 2015-01-28 13:35:40
我决定跳过使用这个API,而是使用EWS托管API。下面是有兴趣的人的代码示例:
# Destination folder
$destinationFolder = "C:\Users\username\Downloads\Attachment Downloader"
# replace with your email address
$email = "email@office365tenant.onmicrosoft.com"
$username = "email@office365tenant.onmicrosoft.com"
$password = "Password123"
# load the assembly
Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
# Create Exchange Service object
$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1)
$s.Credentials = New-Object Net.NetworkCredential($username, $password)
# $s.TraceEnabled = $true
Write-Host "Trying AutoDiscover... "
$s.AutodiscoverUrl($email, {$true})
if(!$s.Url) {
Write-Error "AutoDiscover failed"
return;
} else {
Write-Host -ForegroundColor Green "AutoDiscover succeeded - $($s.Url)"
}
# Create destination folder
$destinationFolder = "{0}\{1}" -f $destinationFolder, (Get-Date -Format "yyyyMMdd HHmmss")
mkdir $destinationFolder | Out-Null
# get a handle to the inbox
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
#create a property set (to let us access the body & other details not available from the FindItems call)
$psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;
# Find the items
Write-Host "Searching for items in mailbox... " -NoNewline
$items = $inbox.FindItems(500)
Write-Host -ForegroundColor Green "found $($items.items.Count)"
$inc = 0;
foreach ($item in $items.Items)
{
# Create mail folder
$inc += 1
$mailFolder = "{0}\{1}" -f $destinationFolder, $inc;
mkdir $mailFolder | Out-Null
# load the property set to allow us to get to the body
$item.load($psPropertySet)
Write-Host ("$inc - $($item.Subject)") -ForegroundColor Yellow
# save the metadata to a file
$item | Export-Clixml ("{0}\metadata.xml" -f $mailFolder)
# save all attachments
foreach($attachment in $item.Attachments) {
if($attachment.ContentType -notlike "message/*") {
Write-Host " - $($attachment.Name) ($($attachment.ContentType))"
$fileName = "{0}\{1}" -f $mailFolder, $attachment.Name
$attachment.Load($fileName)
}
}
# delete the mail item
$item.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete, $true)
}https://stackoverflow.com/questions/28147970
复制相似问题