powershell - PS script that is supposed to print attachments from unread emails - Stack Overflow

I'm working on a PS script that connects with Microsoft Graph that prints attachments from unread

I'm working on a PS script that connects with Microsoft Graph that prints attachments from unread emails in the inbox folder. Everything is working except the part where it only prints from unread emails. I've italicized the line I believe is the issue. I have tried playing around with the line in question, but nothing I've tried seems to work.

# Install Microsoft Graph PowerShell module if not already installed
if (-not (Get-Module -ListAvailable -Name Microsoft.Graph)) {
    Install-Module Microsoft.Graph -Scope CurrentUser -Force
}

# Define Azure App Credentials
$tenantId = "0000000000000000000000000000"
$clientId = "000000000000000000000000000000000"
$clientSecret = "00000000000000000000000000000000000"
$sharedMailbox = "[email protected]"

# Get Access Token
$body = @{
    grant_type    = "client_credentials"
    client_id     = $clientId
    client_secret = $clientSecret
    scope         = "/.default"
} 

$tokenResponse = Invoke-RestMethod -Uri "/$tenantId/oauth2/v2.0/token" -Method Post -Body $body
$accessToken = $tokenResponse.access_token

# Fetch Emails from Shared Mailbox
$headers = @{
    Authorization = "Bearer $accessToken"
    "Content-Type" = "application/json"
}

*$mailUrl = ".0/users/$sharedMailbox/mailFolders/inbox/messages?$filter=isRead -eq $false"*
$mails = Invoke-RestMethod -Uri $mailUrl -Headers $headers -Method Get

# Loop through emails and process attachments
foreach ($mail in $mails.value) {
    $mailId = $mail.id
    $attachmentsUrl = ".0/users/$sharedMailbox/messages/$mailId/attachments"
    $attachments = Invoke-RestMethod -Uri $attachmentsUrl -Headers $headers -Method Get

    foreach ($attachment in $attachments.value) {
        if ($attachment."@odata.type" -eq "#microsoft.graph.fileAttachment") {
            $fileName = $attachment.name
            $fileContent = [System.Convert]::FromBase64String($attachment.contentBytes)
            $filePath = "$env:TEMP\$fileName"

            # Save attachment
            [System.IO.File]::WriteAllBytes($filePath, $fileContent)
            Write-Output "Downloaded: $filePath"

            # Print the file
            Start-Process -FilePath $filePath -Verb Print

            
        }
    }

    


    $markAsReadUrl = ".0/users/$sharedMailbox/messages/$mailId"
    $markAsReadBody = @{ isRead = $true } | ConvertTo-Json
    Invoke-RestMethod -Uri $markAsReadUrl -Headers $headers -Method Patch -Body $markAsReadBody

}

Write-Host "Processing Completed"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743757941a4502100.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信