How to read Office 365 Outlook attachments using powershell script?

%3CLINGO-SUB%20id%3D%22lingo-sub-329992%22%20slang%3D%22en-US%22%3EHow%20to%20read%20Office%20365%20Outlook%20attachments%20using%20powershell%20script%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-329992%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EWe%20have%20email%20account%20and%20users%20daily%20send%20emails%20with%20attachments%20to%20that%20email%20account%20and%20automated%20powershell%20script%20was%20running%20on%20the%20windows%20server%20to%20retrieve%20data%20from%20email%20attachments%20.%20Script%20was%20working%20fine%20but%20now%20we%20are%20getting%20authentication%20token%20issue%20since%20our%20Outlook%20was%20migrated%20to%20Office%20365.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMay%20I%20know%20your%20suggestions%20to%20resolve%20the%20issue%3F%3C%2FP%3E%3CP%3EI%20am%20totally%20new%20to%20azure%20Office%20365%20and%20Azure.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%2C%3C%2FP%3E%3CP%3EAA%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-329992%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-332357%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20read%20Office%20365%20Outlook%20attachments%20using%20powershell%20script%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-332357%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%20Vasil.%20I%20used%20search%20filter%20to%20get%20Date%20Range%20results.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-332130%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20read%20Office%20365%20Outlook%20attachments%20using%20powershell%20script%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-332130%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20will%20have%20to%20use%20a%20Search%20filter%20for%20that%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fexchange%2Fclient-developer%2Fexchange-web-services%2Fhow-to-use-search-filters-with-ews-in-exchange%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fexchange%2Fclient-developer%2Fexchange-web-services%2Fhow-to-use-search-filters-with-ews-in-exchange%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-331985%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20read%20Office%20365%20Outlook%20attachments%20using%20powershell%20script%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-331985%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Vasil%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20my%20script%2C%20it%20is%20working%20fine%20but%20I%20want%20to%20use%20Date%20Range%20instead%20of%20ArgumentList%20in%20FindItems%20method%20(%3CSTRONG%3Ehighlighted%20in%20script%3C%2FSTRONG%3E).%20May%20I%20know%20which%20property%20I%20should%20use%20for%20Date%20Range%20in%20FindItems%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24mail%3D%22xxxx%40xxxx.xxx%22%3CBR%20%2F%3E%24password%3D%22xxxxxx%22%3C%2FP%3E%3CP%3E%23%20Set%20the%20path%20to%20your%20copy%20of%20EWS%20Managed%20API%3CBR%20%2F%3E%24dllpath%20%3D%20%22C%3A%5CProgram%20Files%5CMicrosoft%5CExchange%5CWeb%20Services%5C2.2%5CMicrosoft.Exchange.WebServices.dll%22%3CBR%20%2F%3E%23%20Load%20the%20Assembly%3CBR%20%2F%3E%5Bvoid%5D%5BReflection.Assembly%5D%3A%3ALoadFile(%24dllpath)%3C%2FP%3E%3CP%3E%23%20Create%20a%20new%20Exchange%20service%20object%3CBR%20%2F%3E%24service%20%3D%20new-object%20Microsoft.Exchange.WebServices.Data.ExchangeService%3C%2FP%3E%3CP%3E%23These%20are%20your%20O365%20credentials%3CBR%20%2F%3E%24Service.Credentials%20%3D%20New-Object%20Microsoft.Exchange.WebServices.Data.WebCredentials(%24mail%2C%24password)%3C%2FP%3E%3CP%3E%23%20this%20TestUrlCallback%20is%20purely%20a%20security%20check%3CBR%20%2F%3E%24TestUrlCallback%20%3D%20%7B%3CBR%20%2F%3Eparam%20(%5Bstring%5D%20%24url)%3CBR%20%2F%3Eif%20(%24url%20-eq%20%22%3CA%20href%3D%22https%3A%2F%2Fautodiscover-s.outlook.com%2Fautodiscover%2Fautodiscover.xml%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fautodiscover-s.outlook.com%2Fautodiscover%2Fautodiscover.xml%3C%2FA%3E%22)%20%7B%24true%7D%20else%20%7B%24false%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%23%20Autodiscover%20using%20the%20mail%20address%20set%20above%3CBR%20%2F%3E%24service.AutodiscoverUrl(%24mail%2C%24TestUrlCallback)%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSTRONG%3E%24results%20%3D%20%24service.FindItems(%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%22Inbox%22%2C%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E(%20New-Object%20Microsoft.Exchange.WebServices.Data.ItemView%20-ArgumentList%2020)%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E)%3C%2FSTRONG%3E%3CBR%20%2F%3E%24MailItems%20%3D%20%24results.Items%20%7C%20where%20hasattachments%3C%2FP%3E%3CP%3Eforeach%20(%24MailItem%20in%20%24MailItems)%7B%3C%2FP%3E%3CP%3E%24MailItem.Load()%3C%2FP%3E%3CP%3Eforeach(%24Attachment%20in%20%24MailItem.Attachments)%7B%3CBR%20%2F%3E%24Attachment.Load()%3CBR%20%2F%3E%24File%20%3D%20new-object%20System.IO.FileStream((%22C%3A%5CTemp%5CAttachments%5C%22%20%2B%20%24Attachment.Name.ToString())%2C%5BSystem.IO.FileMode%5D%3A%3ACreate)%3CBR%20%2F%3E%24File.Write(%24attachment.Content%2C%200%2C%20%24attachment.Content.Length)%3CBR%20%2F%3E%24File.Close()%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%2C%3C%2FP%3E%3CP%3EAA%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-330311%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20read%20Office%20365%20Outlook%20attachments%20using%20powershell%20script%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-330311%22%20slang%3D%22en-US%22%3E%3CP%3EWithout%20seeing%20the%20actual%20script%2C%20we%20can%20only%20guess%20what's%20going%20on.%20My%20guess%20would%20be%20that%20the%20script%20is%20using%20EWS%20impersonation%2C%20as%20you%20cannot%20access%20item-level%20details%20with%20%22regular%22%20PowerShell.%20Thus%20you%20most%20likely%20need%20to%20add%20the%20impersonation%20permissions%20on%20the%20migrated%20account.%20But%20that's%20just%20a%20guess%2C%20best%26nbsp%3Btalk%20with%20the%20author%20of%20the%20script.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-330230%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20read%20Office%20365%20Outlook%20attachments%20using%20powershell%20script%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-330230%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%20for%20your%20comments%20but%20my%20problem%20is%2C%20I%20do%20not%20have%20access%20for%20admin%20site%20and%20I%20am%20not%20sure%20what%20kind%20of%20information%20I%20should%20get%20from%20admin%20team.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-330028%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20read%20Office%20365%20Outlook%20attachments%20using%20powershell%20script%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-330028%22%20slang%3D%22en-US%22%3EIf%20you%20have%20migrated%20to%20Office%20365%2C%20you%20might%20need%20to%20change%20in%20your%20scripts%20the%20way%20you%20authenticate%20against%20the%20service%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1227175%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20read%20Office%20365%20Outlook%20attachments%20using%20powershell%20script%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1227175%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F277451%22%20target%3D%22_blank%22%3E%40AA_007%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3EI%20used%20search%20filter%20to%20get%20Date%20Range%20results.%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWould%20you%20mind%20to%20share%20the%20piece%20of%20code%20you%20used%20for%20the%20search%20filter%20based%20on%20date%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New 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

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

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.

 

Highlighted

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.

Highlighted

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

Highlighted

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

Highlighted

@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.