【问题标题】:Email Attachment and Body Naming电子邮件附件和正文命名
【发布时间】:2021-01-01 08:01:17
【问题描述】:

我正在下载电子邮件正文及其附件。我想确保每个发件人的文件名都附有数字,以区别于其他文档。计数器应该从每个发件人开始。例如 发件人1 Sender1-Emailbody-0 Sender1-pdf 附件-1 sender1-jpg附件-2

发件人2 Sender2-Emailbody-0 Sender2-pdf附件-1 sender2-jpg附件-2

我不知道该怎么做。任何帮助,请。谢谢

$emailQuery1 = $url + "?`$select=Id,Sender,DateTimeReceived&`$filter=HasAttachments eq true and DateTimeReceived ge " + $datetime
$emails = Invoke-RestMethod -Uri $emailQuery1 -Headers $head
## Loop through each results
Foreach ($email in $emails.value)

{
  #get attachments and save to file system
   $query = $url + "/" + $email.Id + "/attachments"
   $body = $url + "/" + $email.Id + "/body"
   $receivedtime = "/" + $email.id + "/DateTimeReceived"
   $attachments = Invoke-RestMethod -Uri $query -Headers $head
   $emailbody = Invoke-RestMethod -Uri $body -Headers $head

  Foreach($em in $emailbody)
  {
     $bodyitems = $em.content
  Foreach ($attachment in $attachments.value)
  {
     $attachment.Name
     $path = $DFSPath + $recvtime +"_" + $email.Sender.EmailAddress.Address + "_" + $attachment.Name 
     $Content = [System.Convert]::FromBase64String($attachment.ContentBytes)
     $recvtime = $email.DateTimeReceived | get-date -Format yyyyMMddHHmmss 
     $bodyitems | out-file -FilePath ($DFSPath +$recvtime +"_" + $email.Sender.EmailAddress.Address` 
     +"_emailbody-" +$Counter+".html")
     Set-Content -Path $path -Value $Content -Encoding Byte
    }
  }
}

【问题讨论】:

    标签: powershell outlook automation batch-rename


    【解决方案1】:

    我建议在保存文件之前使用一个计数变量。在 foreach 电子邮件循环的开头将其设置为 0,$x=0,然后将 $x++ 放在 Out-File 命令之前。然后,您可以在保存文件时将其附加到文件名中。

    【讨论】:

      猜你喜欢
      • 2013-08-10
      • 1970-01-01
      • 2018-07-24
      • 2015-05-28
      • 1970-01-01
      • 2018-11-09
      • 2017-12-12
      • 1970-01-01
      • 2013-03-18
      相关资源
      最近更新 更多