Forum Discussion
How to read Office 365 Outlook attachments using powershell script?
Hi Vasil,
Here is my script, it is working fine but I want to use Date Range instead of ArgumentList in FindItems method (highlighted in script). May I know which property I should use for Date Range in FindItems?
$mail="xxxx@xxxx.xxx"
$password="xxxxxx"
# Set the path to your copy of EWS Managed API
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
# Load the Assembly
[void][Reflection.Assembly]::LoadFile($dllpath)
# Create a new Exchange service object
$service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService
#These are your O365 credentials
$Service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($mail,$password)
# this TestUrlCallback is purely a security check
$TestUrlCallback = {
param ([string] $url)
if ($url -eq "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml") {$true} else {$false}
}
# Autodiscover using the mail address set above
$service.AutodiscoverUrl($mail,$TestUrlCallback)
$results = $service.FindItems(
"Inbox",
( New-Object Microsoft.Exchange.WebServices.Data.ItemView -ArgumentList 20)
)
$MailItems = $results.Items | where hasattachments
foreach ($MailItem in $MailItems){
$MailItem.Load()
foreach($Attachment in $MailItem.Attachments){
$Attachment.Load()
$File = new-object System.IO.FileStream(("C:\Temp\Attachments\" + $Attachment.Name.ToString()),[System.IO.FileMode]::Create)
$File.Write($attachment.Content, 0, $attachment.Content.Length)
$File.Close()
}
}
Thank you,
AA
You will have to use a Search filter for that: https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-use-search-filters-with-ews-in-exchange
- AA_007Feb 07, 2019Copper Contributor
Thank you Vasil. I used search filter to get Date Range results.
- AllfieFeb 07, 2022Copper ContributorHi AA_007 , VasilMichev , matloe ,
You guys know a way to catch the "To" field of an email? We're forwarding emails to an end-user's mailbox and want to catch the email address used in To, where the email was originally sent to.
Many thanks