admin管理员组文章数量:1345452
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"
本文标签: powershellPS script that is supposed to print attachments from unread emailsStack Overflow
版权声明:本文标题:powershell - PS script that is supposed to print attachments from unread emails - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743757940a2533850.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论