Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##-----------------------------------------------------##
  2. ##        PICK AUTH Method                             ##
  3. ##-----------------------------------------------------##
  4.  
  5. ## HARD CODING PSW    ##
  6. $password = ConvertTo-SecureString "XXX" -AsPlainText -Force
  7. $cred = New-Object System.Management.Automation.PSCredential "XXX@XXX.com",$password
  8.  
  9. ## USER PROMPT PSW    ##
  10. #$cred = Get-Credential
  11.  
  12. ##-----------------------------------------------------##
  13. ##    END PICK
  14. ##-----------------------------------------------------##
  15.  
  16.  
  17. $sharedMailbox = "XXX@XXX.com"
  18. $url = "https://outlook.office365.com/api/v1.0/users/$sharedMailbox/messages"
  19.  
  20.  
  21. ## Get all messages that have attachments where received date is greater than $date
  22. $messageQuery = $url + "?`$select=Id&`$filter=HasAttachments eq true and IsRead eq false"
  23. $messages = Invoke-RestMethod $messageQuery -Credential $cred
  24.  
  25. ## Loop through each results
  26. foreach ($message in $messages.value){
  27.  
  28.     # get attachments and save to file system
  29.     $query = $url + "/" + $message.Id + "/attachments"
  30.     $attachments = Invoke-RestMethod $query -Credential $cred
  31.  
  32.     # in case of multiple attachments in email
  33.     foreach ($attachment in $attachments.value){
  34.         $attachment.Name
  35.         $path = "c:\Temp\" + $attachment.Name
  36.    
  37.         $Content = [System.Convert]::FromBase64String($attachment.ContentBytes)
  38.         Set-Content -Path $path -Value $Content -Encoding Byte
  39.     }
  40.  
  41.     # Move processed email to a subfolder
  42.     $query = $url + "/" + $message.Id + "/move"
  43.     $body="{""DestinationId"":""AAMkAGRiZmVmOTFlLWJmNjctNDVmZi1iZDkyLTZhOTEzZjI4MGJkNQAuAAAAAAAAkEHub27VS7X8pWwWnKIcAQCxICvUWGkmS6kBXjFB5cP/AADk/q7pAAA=""}"
  44.     Invoke-RestMethod $query -Body $body -ContentType "application/json" -Method post -Credential $cred
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement