Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##-----------------------------------------------------##
- ## PICK AUTH Method ##
- ##-----------------------------------------------------##
- ## HARD CODING PSW ##
- $password = ConvertTo-SecureString "XXX" -AsPlainText -Force
- $cred = New-Object System.Management.Automation.PSCredential "XXX@XXX.com",$password
- ## USER PROMPT PSW ##
- #$cred = Get-Credential
- ##-----------------------------------------------------##
- ## END PICK
- ##-----------------------------------------------------##
- $sharedMailbox = "XXX@XXX.com"
- $url = "https://outlook.office365.com/api/v1.0/users/$sharedMailbox/messages"
- ## Get all messages that have attachments where received date is greater than $date
- $messageQuery = $url + "?`$select=Id&`$filter=HasAttachments eq true and IsRead eq false"
- $messages = Invoke-RestMethod $messageQuery -Credential $cred
- ## Loop through each results
- foreach ($message in $messages.value){
- # get attachments and save to file system
- $query = $url + "/" + $message.Id + "/attachments"
- $attachments = Invoke-RestMethod $query -Credential $cred
- # in case of multiple attachments in email
- foreach ($attachment in $attachments.value){
- $attachment.Name
- $path = "c:\Temp\" + $attachment.Name
- $Content = [System.Convert]::FromBase64String($attachment.ContentBytes)
- Set-Content -Path $path -Value $Content -Encoding Byte
- }
- # Move processed email to a subfolder
- $query = $url + "/" + $message.Id + "/move"
- $body="{""DestinationId"":""AAMkAGRiZmVmOTFlLWJmNjctNDVmZi1iZDkyLTZhOTEzZjI4MGJkNQAuAAAAAAAAkEHub27VS7X8pWwWnKIcAQCxICvUWGkmS6kBXjFB5cP/AADk/q7pAAA=""}"
- Invoke-RestMethod $query -Body $body -ContentType "application/json" -Method post -Credential $cred
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement