我一直试图找到这个问题的解决方案已经有一段时间了,我发现的任何东西似乎都没有用。我希望使用这个脚本:
##-----------------------------------------------------##
## PICK AUTH Method ##
##-----------------------------------------------------##
## HARD CODING PSW ##
#$password = ConvertTo-SecureString "xxx" -AsPlainText -Force
#$cred = New-Object System.Management.Automation.PSCredential "xxx@xxx.onmicrosofot.com",$password
## USER PROMPT PSW ##
#$cred = Get-Credential
##-----------------------------------------------------##
## END PICK
##-----------------------------------------------------##
$url = "https://outlook.office365.com/api/v1.0/me/messages"
$date = "2014-11-21"
## Get all messages that have attachments where received date is greater than $date
$messageQuery = "" + $url + "?`$select=Id&`$filter=HasAttachments eq true and DateTimeReceived ge " + $date
$messages = Invoke-RestMethod $messageQuery -Credential $cred
## Loop through each results
foreach ($message in $messages.value)
{
# get attachments and save to file system
$query = $url + "/" + $message.Id + "/attachments"
$attachments = Invoke-RestMethod $query -Credential $cred
# in case of multiple attachments in email
foreach ($attachment in $attachments.value)
{
$attachment.Name
$path = "c:\Temp\" + $attachment.Name
$Content = [System.Convert]::FromBase64String($attachment.ContentBytes)
Set-Content -Path $path -Value $Content -Encoding Byte
}
}
我从链接中找到了 - TechNet Office 365 Scripts
我可以让脚本正常工作,它连接到邮箱并下载日期变量后指定的电子邮件的附件。问题是我希望按小时计划运行此操作并删除以前处理过的电子邮件,以便在连续运行时不会删除它们的附件。
我尝试了很多不同的命令来删除电子邮件但没有成功,并尝试更改$ messageQuery以仅包含特定日期范围内的项目但无法正常工作。
任何人都可以在powershell中为delete命令提供正确的语法,并在此上下文中提供如何使用它的示例吗?
由于
富
更新 - 我认为加入:
$query2 = $url + "/" + $message.Id
和
Invoke-RestMethod $query2 -Credential $cred -Method Delete
让我靠近但现在我的手术时间已经过了。
更新 - 根据需要提供完整的代码:
##-----------------------------------------------------##
## PICK AUTH Method ##
##-----------------------------------------------------##
## HARD CODING PSW ##
$password = ConvertTo-SecureString "password" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential "email@address.co.uk",$password
## USER PROMPT PSW ##
##$cred = Get-Credential
##-----------------------------------------------------##
## END PICK
##-----------------------------------------------------##
#$url = "https://outlook.office365.com/api/v1.0/me/messages"
$url = "https://outlook.office365.com/EWS/ODATA/me/messages"
$today = Get-Date -format yyyy-MM-dd
$date = (Get-Date).AddDays(-2).ToString("yyyy-MM-dd")
## Set date and query
$messageQuery = "" + $url + "?`$select=Id&`$filter=HasAttachments eq true and DateTimeReceived lt " + $today #+ " and DateTimeReceived gt" + $date
$messages = Invoke-RestMethod $messageQuery -Credential $cred
## Loop through each results
foreach ($message in $messages.value)
{
# get attachments and save to file system
$query = $url + "/" + $message.Id + "/attachments"
$query2 = $url + "/" + $message.Id
$attachments = Invoke-RestMethod $query -Credential $cred
# in case of multiple attachments in email
foreach ($attachment in $attachments.value)
{
$attachment.Name
$path = "\\SomePath\" + $attachment.Name
$Content = [System.Convert]::FromBase64String($attachment.ContentBytes)
Set-Content -Path $path -Value $Content -Encoding Byte
}
Invoke-RestMethod $query2 -Credential $cred -Method Delete -TimeoutSec 100
}
#Move pdf files to another folder
#Move-Item '\\SomePath\*.pdf' '\\SomePath'
#Then delete any existing files from directory
#Remove-Item '\\SomePath\*'
我尝试更改日期部分以允许开始日期和结束日期,因此脚本可以每天运行一次,执行提取但不会重新处理前几天的附件,但遇到了解决语法的问题以更改$ messageQuery。
运行脚本的响应是:
Invoke-RestMethod:操作已超时。 在C:\ Scripts \ EmailExtractServiceContract365.ps1:26 char:13 + $ messages = Invoke-RestMethod $ messageQuery -Credential $ cred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation:(System.Net.HttpWebRequest:HttpWebRequest)[Invoke-RestMethod],WebException + FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
答案 0 :(得分:0)
不是答案
使用Fiddler捕获请求。使用https://outlook.office365.com/api/v1.0/me/messages端点。另一个已被弃用(我认为)。在此处发布已清理的请求/响应。
您的脚本可能不是问题
我让脚本工作正常;它找到一封带附件的电子邮件,保存附件,然后删除电子邮件。在我看到请求之前,我不确定您的环境中发生了什么(如果您发布请求,请删除auth标头)。我今天只更改了$ today值以捕获带附件的电子邮件。
$password = ConvertTo-SecureString "password" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential "user@contoso.com",$password
$url = "https://outlook.office365.com/api/v1.0/me/messages"
$today = (Get-Date).AddDays(1).ToString("yyyy-MM-dd")
$date = (Get-Date).AddDays(-2).ToString("yyyy-MM-dd")
## Set date and query
$messageQuery = "" + $url + "?`$select=Id&`$filter=HasAttachments eq true and DateTimeReceived lt " + $today #+ " and DateTimeReceived gt" + $date
$messages = Invoke-RestMethod $messageQuery -Credential $cred
## Loop through each results
foreach ($message in $messages.value)
{
# get attachments and save to file system
$query = $url + "/" + $message.Id + "/attachments"
$query2 = $url + "/" + $message.Id
$attachments = Invoke-RestMethod $query -Credential $cred
# in case of multiple attachments in email
foreach ($attachment in $attachments.value)
{
$attachment.Name
$path = "C:\temp\" + $attachment.Name
$Content = [System.Convert]::FromBase64String($attachment.ContentBytes)
Set-Content -Path $path -Value $Content -Encoding Byte
}
Invoke-RestMethod $query2 -Credential $cred -Method Delete -TimeoutSec 100
}
请求强>
以下是删除电子邮件的请求和响应。
DELETE https://outlook.office365.com/api/v1.0/me/messages/AAMkADBjYXXXXXXXXXX= HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) WindowsPowerShell/5.0.10586.0
Authorization: XXXXXXXXXXXXXXXXXXXXXXXXX
Host: outlook.office365.com
Cookie: ClientId=CQA
Content-Length: 0
<强> RESPONSE 强>
HTTP/1.1 204 No Content