如何在提供的 powershell 脚本示例中使用 office 365 REST API 删除邮件项目

How to delete mail items using office 365 REST API in provided powershell script sample

我已经尝试找到这个问题的解决方案已经有一段时间了,但我发现似乎没有任何效果。我希望使用这个脚本:

##-----------------------------------------------------## 
##        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 
    } 
} 

我从 link - TechNet Office 365 Scripts

中找到的

我可以让脚本正常工作,它连接到邮箱并下载日期变量后指定的电子邮件附件。问题是我想 运行 按小时计划执行此操作并删除以前处理过的电子邮件,这样它们的附件就不会在连续 运行 时被删除。

我尝试了很多不同的命令来删除电子邮件,但都没有成功,并且还尝试更改 $messageQuery 以仅包含特定日期内的项目 运行ge 但无法使任何工作正常。

任何人都可以提供 powershell 中删除命令的正确语法以及如何在这种情况下使用它的示例吗?

谢谢

富有


更新 - 我想加入:

$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 = 调用-RestMethod $messageQuery -Credential $cred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation:(System.Net.HttpWebRequest:HttpWebRequest)[Invoke-RestMethod],WebException + FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

没有答案

使用Fiddler to capture the request. Use the https://outlook.office365.com/api/v1.0/me/messages端点。另一个已被弃用(我认为)。 Post 您已在此处 request/response 进行了清理。

你的脚本应该不是问题

我的脚本运行良好;它找到一封带附件的电子邮件,保存附件,然后删除电子邮件。在看到请求之前,我不确定您的环境中发生了什么(如果您 post 请求,请删除 auth headers )。我只更改了 $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

回应

HTTP/1.1 204 No Content