是否可以使用带有 PowerShell 的 microsoft graph Api 来检索发送给 senditems 内多个特定收件人的电子邮件

问题描述 投票:0回答:1

我需要能够检索sentitems中特定多个收件人的电子邮件,当我运行我的代码时,我得到

error bad request error 400
。我循环遍历每个需要检索电子邮件的收件人。我的目标是输入我发送电子邮件的多个收件人电子邮件地址,然后检索电子邮件并保存到 csv 中。

错误出现在请求端点(我的端点)中。

$nextLink = "https://graph.microsoft.com/v1.0/users/${mailuser}/mailFolders/SentItems/messages?`$filter=(sentDateTime ge ${startDate})&`$search='to:${recipientUser}'&`$select=sentDateTime,subject,toRecipients,bodyPreview,ccRecipients"

以下是完整代码。

# Azure AD app registration details
$appId = ""
$appSecret = ""
$tenantId = ""

# User to retrieve mail sent messages(your email, since you're the sender)
$mailuser = "[email protected]"

# User email addresses that you sent emails to, separated by commas
$recipientUsers = "[email protected]", "[email protected]"

# Date range for filtering emails
$startDate = '2022-01-01T00:00:00Z'

# Define the folder path to store the results
$tempFolderPath = "C:\Temp"

# Check if the folder exists, if not, create it
if (-not (Test-Path $tempFolderPath)) {
    New-Item -Path $tempFolderPath -ItemType Directory -Force
}

# Define CSV file path
$csvFilePath = "$tempFolderPath\sentitemsemail_data2.csv"

# Define API endpoint and parameters for authentication
$authEndpoint = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
$authBody = @{
    client_id = $appId
    client_secret = $appSecret
    scope = "https://graph.microsoft.com/.default"
    grant_type = "client_credentials"
}

# Define function to get request headers for Graph API
function Get-RequestHeaders {
    $tokenResponse = Invoke-RestMethod -Method Post -Uri $authEndpoint -Body $authBody
    $accessToken = $tokenResponse.access_token

    @{
        'Authorization' = "Bearer $($accessToken)"
        'Accept' = 'application/json'
    }
}

# Define function to retrieve messages
function Get-Messages {
    param (
        [string]$url
    )

    $requestHeaders = Get-RequestHeaders
    Invoke-RestMethod -Method Get -Uri $url -Headers $requestHeaders
}

# Define function to export email object to CSV
function Export-EmailObjectToCSV {
    param (
        [object]$emailObject
    )

    $emailObject | Export-Csv -Path $csvFilePath -Append -NoTypeInformation
}

# Loop through each recipient user
foreach ($recipientUser in $recipientUsers) {
    # Initialize $nextLink variable with the initial endpoint URL, from $startDate to current date
    $nextLink = "https://graph.microsoft.com/v1.0/users/${mailuser}/mailFolders/SentItems/messages?`$filter=(sentDateTime ge ${startDate})&`$search='to:${recipientUser}'&`$select=sentDateTime,subject,toRecipients,bodyPreview,ccRecipients"

    # Loop until all pages are fetched
    do {
        # Retrieve messages for the current page
        $messages = Get-Messages -url $nextLink

        # Loop through each message
        foreach ($message in $messages.value) {
            $subject = $message.subject
            $sentdatetime = $message.sentDateTime
            $bodyPreview = $message.bodyPreview

            # Initialize an array to store all CC recipient addresses
            $ccRecipientAddresses = New-Object System.Collections.ArrayList

            foreach ($recipient in $message.toRecipients) {
                $recipientName = $recipient.emailAddress.name
                $recipientAddress = $recipient.emailAddress.address

                # Loop through each CC recipient and store their address
                foreach ($torecipient in $message.ccRecipients){
                    [void]$ccRecipientAddresses.Add($torecipient.emailAddress.address)
                }

                # Create a new object to store the data
                $emailObject = [PSCustomObject]@{
                    Subject = $subject
                    RecipientName = $recipientName
                    RecipientAddress = $recipientAddress
                    ToRecipientAddresses = $ccRecipientAddresses -join ", "
                    SentDateTime = $sentdatetime
                    BodyPreview = $bodyPreview
                }

                # Export each email object to the CSV file
                Export-EmailObjectToCSV -emailObject $emailObject
            }
        }

        # Check if there are more pages to fetch
        if ($messages."@odata.nextLink") {
            $nextLink = $messages."@odata.nextLink"
        }
        else {
            $nextLink = $null  # If no more pages, exit the loop
        }
    } while ($nextLink)
}


powershell microsoft-graph-api office365
1个回答
0
投票

您不能将

$filter
$search
查询参数组合用于
messages

过滤器可以移至

$search
查询参数。搜索值必须用双引号引起来。

GET /v1.0/users/${mailuser}/mailFolders/SentItems/messages?$search="to:${recipientUser} AND sent>=${startDate}"&$select=sentDateTime,subject,toRecipients,bodyPreview,ccRecipients

PS

$nextLink = "https://graph.microsoft.com/v1.0/users/${mailuser}/mailFolders/SentItems/messages?&`$search=""to:${recipientUser} AND sent>=${startDate}""&`$select=sentDateTime,subject,toRecipients,bodyPreview,ccRecipients"
© www.soinside.com 2019 - 2024. All rights reserved.