How to read Office 365 Outlook attachments using powershell script?

Copper Contributor

Hi,

We have email account and users daily send emails with attachments to that email account and automated powershell script was running on the windows server to retrieve data from email attachments . Script was working fine but now we are getting authentication token issue since our Outlook was migrated to Office 365.

 

May I know your suggestions to resolve the issue?

I am totally new to azure Office 365 and Azure.

 

Thank you,

AA

8 Replies
If you have migrated to Office 365, you might need to change in your scripts the way you authenticate against the service

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.

 

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.

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

Thank you Vasil. I used search filter to get Date Range results.

@AA_007 

I used search filter to get Date Range results.

 

Would you mind to share the piece of code you used for the search filter based on date?

 

Thanks.

 

 

Hi @AA_007 , @Vasil Michev , @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