Page 1 of 1

Save message by the last attachmentname (which is not a picture )

Posted: Mon Apr 08, 2019 3:42 pm
by Sven

I just made a script for somebody which can save the eMail file with a filename from the attachment of it.
Here is it for the case that somebody needs it:

Code: Select all

Import-Module -Name 'C:\Program Files\Somebytes\EEAttachments\Microsoft.Exchange.WebServices.dll'
$item = Get-Variable -Name mailitem -ValueOnly
$psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::Attachments)  
$attname = "unknown"
foreach($a in $item.Attachments)
    if (!($attname.EndsWith("jpg")) -or ($attname.EndsWith("png")))
		$attname =[IO.Path]::GetFileNameWithoutExtension($a.Name)
$eml = Get-Variable -Name emlfile -ValueOnly
$emlfile = Get-ChildItem $eml 
$newfilename = $emlfile.DirectoryName + "\" + $attname  +".eml" 
move $eml $newfilename