问题描述
我正在下载电子邮件正文及其附件。我想确保每个发件人在文件名上都附有数字,以区别于其他文档。计数器应按每个发送方启动。例如 发件人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
}
}
}
解决方法
我建议使用一个计数变量,该变量会在保存文件之前立即增加。在foreach电子邮件循环$x=0
的开始处将其设置为0,然后将$x++
放在Out-File
命令之前。保存文件后,可以将其附加到文件名。