我正在尝试编写一个Powershell脚本,它将下载所有附件,当我只下载前10个附件时

时间:2018-02-05 18:10:19

标签: powershell

    $password = ConvertTo-SecureString "xxx" -AsPlainText -Force 
$cred = New-Object System.Management.Automation.PSCredential "xxx@xxx.onmicrosoft.com",$password 

$url = "https://outlook.office365.com/api/v1.0/me/messages" 
$today = (Get-Date).AddDays(1).ToString("yyyy-MM-dd")

## Set date and query  
$messageQuery = "" + $url + "?`$select=Id&`$filter=HasAttachments eq true and DateTimeReceived lt " + $today
$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:\path\" + $attachment.Name 

        $Content = [System.Convert]::FromBase64String($attachment.ContentBytes) 
        Set-Content -Path $path -Value $Content -Encoding Byte 

    } 
}

运行此功能时,只下载10个附件然后停止。 我想找出它停在10点的原因,以便我可以继续下载整个收件箱。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我认为它只能根据附件大小一次处理X数量。当我成功使用这些行复制附件时,我删除了我的邮箱,因为我收集附件后不需要电子邮件。

这个(我的代码的其余部分)每5分钟在任务计划程序中运行,所以如果它没有在第一次运行中捕获所有邮件,它们将被处理,我想在后续10次运行。

if($ DeleteEmail -eq" Y"){
    $ query2 = $ url +" /" + $ message.Id
    Invoke-RestMethod $ query2 -Credential $ Credentials -Method删除
}