I have written the below Powershell code that loops through all attachments in a given ".eml" file. My goal is to be able to extract each attachment to a local directory.
Up to now, I have been able to only print the filenames (to see that the loop indeed accesses the attachments).
Thank you in advance.
$emlFn = "thedirectory\file.eml"
$adoDbStream = New-Object -ComObject ADODB.Stream
$adoDbStream.Open()
$adoDbStream.LoadFromFile($emlFn)
$cdoMessage = New-Object -ComObject CDO.Message
$cdoMessage.DataSource.OpenObject($adoDbStream, "_Stream")
foreach($attachment in $cdoMessage.Attachments){
Write-Output $attachment.FileName
#I need some code here to save each attachment locally
}
$adoDbStream.Close()
According to the Exchange Server 2003 SDK docs the interface used for attachments expose a SaveToFile
method - pass the full path to it to save the attachment to disk:
# construct output path from current directory + attachment filename
$exportPath = Join-Path $PWD $attachment.FileName
# save to disk
$attachment.SaveToFile("$exportPath")