0

i wonder if there is a way to get the file extension from a public folder attachment?

Backgroud: We are using a software (AttachmentsProcessor) which extracts all attachments from the e-mails in the public folder structure and save it to the filesystem. The software puts a .lnk in the e-mail, which points to the location in the filesystem. So we can open the attachment by double-click.

Lately we moved our public folder structure from internal Exchange to Office365 / Exchange Online. During this process we tried to put all extracted attachments back into the e-mails. After we done some tests, we noticed the this didn't work for some of the e-mails. We have still the .lnk as an attachment.

So what am I looking for? I would like to write a script in powershell which shows me a list of all e-mails and the corresponding folders (Identites), which have a .lnk file attached.

On my search I just found something that works for mailboxes but nothing for public folders.

-> Get-Mailbox | Export-Mailbox -AttachmentFilenames "*.PDF"

-> Get-Mailbox | New-MailboxExportRequest -ContentFilter {Attachment -like "*.PDF"}

Any help would be very nice. ;-)

Thanks for your attention Peter

1 Answers1

0

I can't overtly write all the code for you. But I have something that can get you started. This script will recursively iterate through your public folders and find items that have attachments on them. The last bit of code inside the loop currently saves the files to disk, but that's where you can replace that with some logic to do what you need it to do (i.e. filtering by attachment, pulling the link information, etc.).

$TargetDirectory = "C:\temp\PublicFolders"

function process-folders-recursive($folder, $path) {
    if($folder.DefaultMessageClass -ne "IPM.Appointment" -and $folder.DefaultMessageClass -ne "IPM.Contact")
    {
        $path = (Join-Path $path $folder.name)
        write-host $folder.class $path
        if($folder.Items.Count -gt 0 ){
            foreach($item in $folder.Items){
              if($item.MessageClass -ne "IPM.Appointment")
              {
                #Write-Host $item.name
                if($item.Attachments.Count -gt 0 ) {
                    if(!(Test-Path -path $path)){
                        New-Item -ItemType directory -Path $path
                    }
                    foreach($attch in $item.Attachments){
                        try
                            {
                            Write-Host $attch.FileName
                            $fileName = $attch.FileName
                            $fileNameAndPath = (Join-Path $path $fileName)
                            $attch.saveasfile($fileNameAndPath)
                        }
                        catch [System.Exception]
                        {
                            Write-Host $path $fileName # $_.Exception.ToString()
                        }
                    }
                  }
                }
            }
        }
        $folder.Folders | ForEach { process-folders-recursive $_ $path}  
   }
}

$objOutlook = New-Object -comobject outlook.application
$objNamespace = $objOutlook.GetNamespace(“MAPI”)

#Get Outlook folder with name Public Folders
$publicFolders  = $objNamespace.Folders | Where { $_.name.StartsWith("Public Folders") } | Select -f 1

#Go into the "All Public Folders" folder
$AllPublicFolders = $publicFolders.Folders | Where { $_.name -eq "All Public Folders" } | Select -f 1

#Recurse through the Public Folders
process-folders-recursive $AllPublicFolders $TargetDirectory
HAL9256
  • 12,384
  • 1
  • 34
  • 46