Automatically save outlook message

 
I hope someone will be able to help. I am trying to automatically save emails as .msg files. These emails are requests that are sent by the public via an online portal. The emails will then have an index file created relating to it and then uploaded into an EDM system. I need to save the email in its original format as it contains personal/confidential information that cannot be tampered with. I have done something similar before but saving attachments rather than the actual email.
 
I had a number of attempts and the script below is the closest I have got but this is searching through “my” default email address and I need it to read though a different one, possibly a number of different ones. Does anyone know if this can be modified to parameterise an email address to search though?

 
$DestinationPath = "c:\Support\files\"
 
$EmailAddress = "Application.Emails@donny.gov.uk"
 
 
 
#Removes invalid Characters for file names from a string input and outputs the clean string
 
Function Remove-InvalidFileNameChars 
 
{
 
    param
 
    (
 
        [Parameter(Mandatory=$true, Position=0)]
 
        [String]$Name
 
    )
 
    return [RegEx]::Replace($Name, "[{0}]" -f ([RegEx]::Escape([String][System.IO.Path]::GetInvalidFileNameChars())), '-')
 
}
 
 
 
#Add Interop Assembly
 
Add-type -AssemblyName "Microsoft.Office.Interop.Outlook" | Out-Null
 
 
 
#Type declaration for Outlook Enumerations
 
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
 
$olSaveType = "Microsoft.Office.Interop.Outlook.OlSaveAsType" -as [type]
 
$olClass = "Microsoft.Office.Interop.Outlook.OlObjectClass" -as [type]
 
 
 
#Add Outlook Com Object, MAPI namespace, and set folder to the Inbox
 
$outlook = New-Object -ComObject Outlook.Application
 
$namespace = $outlook.GetNameSpace("MAPI")
 
 
 
#
 
$folder = $namespace.getDefaultFolder($olFolders::olFolderDrafts)
 
#$folder = $namespace.getDefaultFolder($olFolders::olFolderDrafts)
 
#Iterate through each object in the chosen folder
foreach ($email in $folder.Items) 
{
    #Get email's subject and date
    [string]$subject = $email.Subject
    [string]$sentOn = $email.SentOn
 
    #Strip subject and date of illegal characters, add .msg extension, and combine
    $fileName = Remove-InvalidFileNameChars -Name ($sentOn + "-" + $subject + ".msg")
     #Combine destination path with stripped file name
    $dest = $DestinationPath + $fileName
    $email.SaveAs($dest, $olSaveType::olMSG)   
}
 

 

 

 

@Wayne - Did you get the solution for this ? if so please share it with us which will be helpful for others else please update this thread with latest update.

Sounds like you are hammer out a GDPR thing for your org as required by the new laws and policies.

I am a huge PS geek, but, don’t write stuff that I don’t have to. Well, unless it’s a learning effort.

Why are you not just using an Outlook macro to do this natively?

There are many articles on this topic, well, VBA vs PS, but again we are talking making Outlook do this for you.

Example:

https://www.extendoffice.com/documents/outlook/3747-outlook-auto-download-save-attachments-to-folder.html
Unless you or the account your are using has email direct or delegation (via direct or Exchange Application Impersonation) rights, you will not be able to do this.

I say this because back in my dev days, my team worked on a email archiving solution to scan all email on an Exchange server for eDiscovery and store offline for investigators in PST an msg formats. Now I cannot post that code as It’s customer internal and it was all written in Visual Studio.NET.

You are doing this via Outlook, proper, this would say you are trying to do this for your own account, hence the success. If you are trying to do this on remote user machine via Outlook as you are doing locally, that is not possible with PS.

You cannot use PS natively to run in a remote interactive user context, you need other tools for that, such as SysInternals PSExec.

Question, why are you not just using Exchange email archive features for this?

Is it that you EDM systems has not Exchange directly or even EWS interop and can only consume .msg files?

What about Exchange email retention policies?

There are 3rdP tools that will do this for you?

For Example:

Or

ReliefJet Essentials Overview - ReliefJet for Outlook


Here is other discussions, with code, that should interest you, again, not PS put using Outlook directly using VBA.

https://stackoverflow.com/questions/24811521/outlook-vba-email-autosave/24828160#24828160

Save all incoming messages to the hard drive


We all love PS here, but don’t unnecessarily over complicate your life / over engineer a solution that could already exist. Except of course as a learning or improvement over something previously in place that is no longer quite up to snuff.

But if you really wan to go the PS route. There are per-built scripts to get you there, though (vs starting from scratch) you’ll need to take additional steps for the automation and for touching other users mailboxes, you have that whole permissions / rights thing to deal with.

https://gallery.technet.microsoft.com/office/Save-Email-from-Outlook-to-3abf1ff3

https://powershell.org/forums/topic/save-msg-file-from-outlook