powershell 图形API:获取电子邮件收件人地址

nhaq1z21  于 2023-03-23  发布在  Shell
关注(0)|答案(1)|浏览(114)

我使用以下命令解析我的电子邮件:

$EmailQuery = "$GraphURI/users/myemail@example.com/mailboxFolders/MyFolder/messages"
$EmailResponseFirst = Invoke-RestMethod -Method Get -Uri $EmailQuery -Headers @{Authorization = "Bearer $AccessToken"} -ContentType "application/json"
$EmailResponse = $EmailResponseFirst.value | Out-String

foreach ($Email in $EmailResponse) {
$RecipientAddress = $Email.toRecipients.emailAddress[0].emailAddress
Write-Host $RecipientAddress

它什么都不输出,但是如果我在脚本中附加以下内容:

Write-Host "($Email.from | ConvertFrom-Json).odata.etag"

我得到这个输出:

@odata.etag                : W/"CQAAABYA"
id                         : AAMkADQ5M
createdDateTime            : 2023-03-06T10:22:22Z
lastModifiedDateTime       : 2023-03-06T10:22:24Z
changeKey                  : CQA
categories                 : {}
receivedDateTime           : 2023-03-06T10:22:22Z
sentDateTime               : 2023-03-06T10:22:18Z
hasAttachments             : False
internetMessageId          : <1BC4@sender.com>
subject                    : Test
bodyPreview                : Test
importance                 : normal
parentFolderId             : AAMkADQ
conversationId             : AAQ
conversationIndex          : AQHZ
isDeliveryReceiptRequested : 
isReadReceiptRequested     : False
isRead                     : True
isDraft                    : False
webLink                    : https://outlook.office365.com/owa/?ItemID=AAMk3D&exvsurl=1&viewmodel=ReadMessageItem
inferenceClassification    : focused
body                       : @{contentType=html; content=<html><head>
                         <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body>Test</body></html>}
sender                     : @{emailAddress=}
from                       : @{emailAddress=}
toRecipients               : {@{emailAddress=}}
ccRecipients               : {}
bccRecipients              : {}
replyTo                    : {}
flag                       : @{flagStatus=notFlagged

如何从输出中获取“toRecipients”字段的值?

umuewwlo

umuewwlo1#

$response.value是所有消息的数组。迭代所有消息,foreach消息迭代所有toRecipients并编写它们的邮件

$response = Invoke-RestMethod -Method Get -Uri $EmailQuery -Headers @{Authorization = "Bearer $AccessToken"} -ContentType "application/json"
$messages = $response.value

foreach ($message in $messages) {
    foreach ($recipient in $message.toRecipients){
        Write-Host $recipient.emailAddress.address
    }
}

相关问题