最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin6浏览0评论

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"
发布评论

评论列表(0)

  1. 暂无评论