Guest User

O365

a guest
Jan 4th, 2019
873
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.     .SYNOPSIS
  3.         0365 API Tools
  4.     .Example
  5.         $global:o365creds = Get-Credential
  6.         Get-O365Message -Searchstring 'attachment:report' |
  7.             ForEach-Object {
  8.                 $_ | Get-O365Attachment -SaveTo E:\Attachments
  9.                 $_ | Move-O365Message -movetofolder Reports -verbose
  10.             }
  11.  
  12.         # Gets Messages from the inbox from the username provided in the $o365creds.
  13.         # For each message it saves the Attachment and moves the message to the x folder.
  14. #>
  15.  
  16.  
  17. Function Get-O365Folder {
  18.     <#
  19.         .SYNOPSIS
  20.             Gets folders from an O365 Mailbox
  21.         .EXAMPLE
  22.             Get-O365Folder -folderName Report        
  23.     #>
  24.     Param (
  25.         [pscredential] $Credentials
  26.         ,
  27.         $folderName
  28.         ,
  29.         [switch] $getChild
  30.     )
  31.     Begin {
  32.         If ($global:o365creds) {
  33.             $credentials = $global:o365creds
  34.         } ElseIf (!$Credentials){
  35.             Throw 'No Credentials detected'
  36.         }
  37.     }
  38.     Process {
  39.  
  40.         $url = 'https://outlook.office365.com/api/v1.0/me/folders'
  41.         $top = '$top=50'
  42.  
  43.         $folders = ( Invoke-RestMethod "$url`?$top" -Method Get -Credential $credentials).Value
  44.         If (!$folderName) {
  45.             Return $folders
  46.         }
  47.         #If ($foldername -notin @('Inbox', 'Drafts', 'SentItems', 'DeletedItems')) {
  48.        
  49.             $folder = ($folders | Where-Object {$_.displayname -eq $folderName})    
  50.                        
  51.         #} Else {
  52.  
  53.         #    $folder = $foldername
  54.         #}
  55.        
  56.         $Output = $folder
  57.         If ($getChild) {
  58.                 $Childs = (Invoke-RestMethod "$url/$($folder.id)/childfolders" -Method Get -Credential $credentials).Value
  59.                 $Output | Add-Member -MemberType NoteProperty -Name ChildFolders -Value $Childs
  60.         }
  61.  
  62.         $Output
  63.      }  
  64. }
  65.  
  66. Function Get-O365Message {
  67.     <#
  68.         .SYNOPSIS
  69.             Gets O365 Mail Messages
  70.         .Example
  71.             $Ocreds = Get-Credential
  72.             Get-O365Message -HasAttachments
  73.             Get-O365Message -Folder CARE -Credentials $Ocreds
  74.             Get-O365Message -Searchstring 'attachment:vgs_check'
  75.     #>
  76.     [cmdletbinding()]
  77.     Param(
  78.         [pscredential] $Credentials
  79.         ,
  80.         [string] $FromFolder
  81.         ,
  82.         [string] $fromDate = $((Get-Date).AddMonths(-1)).toString("yyyy-MM-ddT00:00:00Z")
  83.         ,
  84.         [switch] $HasAttachments
  85.         ,
  86.         [string] $searchstring        
  87.     )
  88.     Begin {
  89.         If ($global:o365creds) {
  90.             $credentials = $global:o365creds
  91.         } ElseIf (!$Credentials){
  92.             Throw 'No Credentials detected'
  93.         }
  94.     }
  95.     Process {
  96.         $url = 'https://outlook.office365.com/api/v1.0/me/folders'
  97.  
  98.         If ($FromFolder) {        
  99.             $folder = Get-0365Folder -folderName $fromFolder
  100.         } Else {
  101.             $folder = 'inbox' #default
  102.         }
  103.    
  104.         If ($searchstring) {
  105.  
  106.             $searchqry = "&`$search=""$searchstring"""
  107.  
  108.         } ElseIf ($HasAttachments) {
  109.  
  110.             $filterqry += ' and HasAttachments eq true'
  111.  
  112.         }
  113.  
  114.         If ($filterqry) { $filterqry = "&`$filter=DateTimeReceived gt $fromDate$filterqry&`$orderby=DateTimeReceived desc" }    
  115.  
  116.         try {
  117.        
  118.             $finalurl = "$url/$folder/messages?`$top=50&`$select=DateTimeReceived,Sender,Subject$filterqry$searchqry"
  119.             $Response = (Invoke-RestMethod $finalurl -Method Get -Credential $credentials).Value
  120.  
  121.         } catch {
  122.  
  123.             Write-Error $_
  124.             Write-Error $finalurl
  125.  
  126.         }
  127.      
  128.         ForEach ($item in $Response) {
  129.             $Output = [pscustomobject]@{
  130.                 DateTimeReceived = $item.DateTimeReceived          
  131.                 Sender = $item.Sender.EmailAddress
  132.                 Subject = $item.Subject
  133.                 MessageId = $item.Id
  134.             }
  135.             Write-Output $Output
  136.         }
  137.     }
  138. }
  139.  
  140. Function Get-O365Attachment {
  141.     <#
  142.         .SYNOPSIS
  143.             Gets Attachments from an O365 Mail Message
  144.         .EXAMPLE
  145.             Get-O365Message -HasAttachments | Get-O365Attachment -SaveTo E:\Attachments
  146.     #>
  147.     [cmdletbinding()]
  148.     Param (
  149.         [pscredential] $Credentials
  150.         ,
  151.         [Parameter(ValueFromPipelineByPropertyName)]
  152.         $MessageId
  153.         ,
  154.         [string] $SaveTo
  155.         ,
  156.         [switch] $All
  157.     )
  158.     Begin {
  159.  
  160.         If ($global:o365creds) {
  161.             $credentials = $global:o365creds
  162.         } ElseIf (!$Credentials){
  163.             Throw 'No Credentials detected'
  164.         }
  165.  
  166.         $url = 'https://outlook.office365.com/api/v1.0/me/messages'
  167.         If ( (Test-Path $saveTo) -eq $true) {
  168.             Write-Verbose "Saving files to $SaveTo"
  169.         } Else {
  170.             Throw 'Save location could not be found'
  171.         }
  172.     }
  173.     Process {
  174.  
  175.         $query = $url + "/" +$MessageId + "/attachments"
  176.         $attachments = Invoke-RestMethod $query -Credential $Credentials
  177.  
  178.         foreach ($attachment in $attachments.value) {
  179.             # weed out unwanted attachments
  180.             # Use the All siwtch to include them
  181.             If (!$All) {
  182.                 If ($attachment.Size -lt 100000 -and $attachment.contentType -like '*image*') {
  183.                     #images from less then 100kb
  184.                     continue
  185.                 } ElseIf ($attachment.contentType -eq 'application/octet-stream' ) {
  186.                     #bin files
  187.                     continue
  188.                 } ElseIf ($attachment.IsContactPhoto) {
  189.                     #contact photos
  190.                     continue
  191.                 }
  192.             }
  193.                                
  194.             If ( $SaveTo ) {
  195.                 $timestamp = $attachment.DateTimeLastModified.Replace(':','')        
  196.                 $path = "$SaveTo\$timestamp`_$($attachment.name)"
  197.    
  198.                 $Content = [System.Convert]::FromBase64String($attachment.ContentBytes)
  199.                 Set-Content -Path $path -Value $Content -Encoding Byte
  200.                
  201.             }
  202.             $Output = [pscustomobject]@{
  203.                 Name = $attachment.name
  204.                 Size = $attachment.size
  205.                 ContentType = $attachment.Contenttype
  206.                 Content = @{ContentBytes = $attachment.ContentBytes}
  207.             }
  208.             Write-Output $Output
  209.            
  210.         }
  211.     }
  212. }
  213. Function Move-O365Message {
  214.     <#
  215.         .EXAMPLE
  216.               Get-O365Message -Searchstring 'attachment:vgs_check' | Move-O365Message -ToFolder vgs_work -verbose  
  217.     #>
  218.     [cmdletbinding()]
  219.     Param (
  220.         [pscredential] $Credentials
  221.         ,
  222.         [Parameter(ValueFromPipelineByPropertyName)]
  223.         $MessageId
  224.         ,
  225.         [string] $ToFolder
  226.     )
  227.     Begin {
  228.         If ($global:o365creds) {
  229.             $credentials = $global:o365creds
  230.         } ElseIf (!$Credentials){
  231.             Throw 'No Credentials detected'
  232.         }
  233.         $url = 'https://outlook.office365.com/api/v1.0/me/messages'
  234.         $folder = Get-O365Folder -foldername $ToFolder
  235.     }  
  236.     Process {  
  237.         $query = $url + "/" + $MessageId + "/move"
  238.         $body = "{""DestinationId"":""$($folder.id)""}"
  239.         Invoke-RestMethod $query -Body $body -ContentType "application/json" -Method post -Credential $Credentials | Out-null
  240.     }
  241. }
RAW Paste Data