Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- 0365 API Tools
- .Example
- $global:o365creds = Get-Credential
- Get-O365Message -Searchstring 'attachment:report' |
- ForEach-Object {
- $_ | Get-O365Attachment -SaveTo E:\Attachments
- $_ | Move-O365Message -movetofolder Reports -verbose
- }
- # Gets Messages from the inbox from the username provided in the $o365creds.
- # For each message it saves the Attachment and moves the message to the x folder.
- #>
- Function Get-O365Folder {
- <#
- .SYNOPSIS
- Gets folders from an O365 Mailbox
- .EXAMPLE
- Get-O365Folder -folderName Report
- #>
- Param (
- [pscredential] $Credentials
- ,
- $folderName
- ,
- [switch] $getChild
- )
- Begin {
- If ($global:o365creds) {
- $credentials = $global:o365creds
- } ElseIf (!$Credentials){
- Throw 'No Credentials detected'
- }
- }
- Process {
- $url = 'https://outlook.office365.com/api/v1.0/me/folders'
- $top = '$top=50'
- $folders = ( Invoke-RestMethod "$url`?$top" -Method Get -Credential $credentials).Value
- If (!$folderName) {
- Return $folders
- }
- #If ($foldername -notin @('Inbox', 'Drafts', 'SentItems', 'DeletedItems')) {
- $folder = ($folders | Where-Object {$_.displayname -eq $folderName})
- #} Else {
- # $folder = $foldername
- #}
- $Output = $folder
- If ($getChild) {
- $Childs = (Invoke-RestMethod "$url/$($folder.id)/childfolders" -Method Get -Credential $credentials).Value
- $Output | Add-Member -MemberType NoteProperty -Name ChildFolders -Value $Childs
- }
- $Output
- }
- }
- Function Get-O365Message {
- <#
- .SYNOPSIS
- Gets O365 Mail Messages
- .Example
- $Ocreds = Get-Credential
- Get-O365Message -HasAttachments
- Get-O365Message -Folder CARE -Credentials $Ocreds
- Get-O365Message -Searchstring 'attachment:vgs_check'
- #>
- [cmdletbinding()]
- Param(
- [pscredential] $Credentials
- ,
- [string] $FromFolder
- ,
- [string] $fromDate = $((Get-Date).AddMonths(-1)).toString("yyyy-MM-ddT00:00:00Z")
- ,
- [switch] $HasAttachments
- ,
- [string] $searchstring
- )
- Begin {
- If ($global:o365creds) {
- $credentials = $global:o365creds
- } ElseIf (!$Credentials){
- Throw 'No Credentials detected'
- }
- }
- Process {
- $url = 'https://outlook.office365.com/api/v1.0/me/folders'
- If ($FromFolder) {
- $folder = Get-0365Folder -folderName $fromFolder
- } Else {
- $folder = 'inbox' #default
- }
- If ($searchstring) {
- $searchqry = "&`$search=""$searchstring"""
- } ElseIf ($HasAttachments) {
- $filterqry += ' and HasAttachments eq true'
- }
- If ($filterqry) { $filterqry = "&`$filter=DateTimeReceived gt $fromDate$filterqry&`$orderby=DateTimeReceived desc" }
- try {
- $finalurl = "$url/$folder/messages?`$top=50&`$select=DateTimeReceived,Sender,Subject$filterqry$searchqry"
- $Response = (Invoke-RestMethod $finalurl -Method Get -Credential $credentials).Value
- } catch {
- Write-Error $_
- Write-Error $finalurl
- }
- ForEach ($item in $Response) {
- $Output = [pscustomobject]@{
- DateTimeReceived = $item.DateTimeReceived
- Sender = $item.Sender.EmailAddress
- Subject = $item.Subject
- MessageId = $item.Id
- }
- Write-Output $Output
- }
- }
- }
- Function Get-O365Attachment {
- <#
- .SYNOPSIS
- Gets Attachments from an O365 Mail Message
- .EXAMPLE
- Get-O365Message -HasAttachments | Get-O365Attachment -SaveTo E:\Attachments
- #>
- [cmdletbinding()]
- Param (
- [pscredential] $Credentials
- ,
- [Parameter(ValueFromPipelineByPropertyName)]
- $MessageId
- ,
- [string] $SaveTo
- ,
- [switch] $All
- )
- Begin {
- If ($global:o365creds) {
- $credentials = $global:o365creds
- } ElseIf (!$Credentials){
- Throw 'No Credentials detected'
- }
- $url = 'https://outlook.office365.com/api/v1.0/me/messages'
- If ( (Test-Path $saveTo) -eq $true) {
- Write-Verbose "Saving files to $SaveTo"
- } Else {
- Throw 'Save location could not be found'
- }
- }
- Process {
- $query = $url + "/" +$MessageId + "/attachments"
- $attachments = Invoke-RestMethod $query -Credential $Credentials
- foreach ($attachment in $attachments.value) {
- # weed out unwanted attachments
- # Use the All siwtch to include them
- If (!$All) {
- If ($attachment.Size -lt 100000 -and $attachment.contentType -like '*image*') {
- #images from less then 100kb
- continue
- } ElseIf ($attachment.contentType -eq 'application/octet-stream' ) {
- #bin files
- continue
- } ElseIf ($attachment.IsContactPhoto) {
- #contact photos
- continue
- }
- }
- If ( $SaveTo ) {
- $timestamp = $attachment.DateTimeLastModified.Replace(':','')
- $path = "$SaveTo\$timestamp`_$($attachment.name)"
- $Content = [System.Convert]::FromBase64String($attachment.ContentBytes)
- Set-Content -Path $path -Value $Content -Encoding Byte
- }
- $Output = [pscustomobject]@{
- Name = $attachment.name
- Size = $attachment.size
- ContentType = $attachment.Contenttype
- Content = @{ContentBytes = $attachment.ContentBytes}
- }
- Write-Output $Output
- }
- }
- }
- Function Move-O365Message {
- <#
- .EXAMPLE
- Get-O365Message -Searchstring 'attachment:vgs_check' | Move-O365Message -ToFolder vgs_work -verbose
- #>
- [cmdletbinding()]
- Param (
- [pscredential] $Credentials
- ,
- [Parameter(ValueFromPipelineByPropertyName)]
- $MessageId
- ,
- [string] $ToFolder
- )
- Begin {
- If ($global:o365creds) {
- $credentials = $global:o365creds
- } ElseIf (!$Credentials){
- Throw 'No Credentials detected'
- }
- $url = 'https://outlook.office365.com/api/v1.0/me/messages'
- $folder = Get-O365Folder -foldername $ToFolder
- }
- Process {
- $query = $url + "/" + $MessageId + "/move"
- $body = "{""DestinationId"":""$($folder.id)""}"
- Invoke-RestMethod $query -Body $body -ContentType "application/json" -Method post -Credential $Credentials | Out-null
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement