Forum Discussion
How to read Office 365 Outlook attachments using powershell script?
- AA_007Feb 04, 2019Copper Contributor
Thank you for your comments but my problem is, I do not have access for admin site and I am not sure what kind of information I should get from admin team.
- VasilMichevFeb 04, 2019MVP
Without seeing the actual script, we can only guess what's going on. My guess would be that the script is using EWS impersonation, as you cannot access item-level details with "regular" PowerShell. Thus you most likely need to add the impersonation permissions on the migrated account. But that's just a guess, best talk with the author of the script.
- AA_007Feb 06, 2019Copper Contributor
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 hasattachmentsforeach ($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