0

Want to preface this by stating I am not a PowerShell programmer/expert or have much experience with scripting in general. I'm here in a last attempt for some assistance with what seems to be an impossible ask.

Essentially what I'm looking to do is have a script that can be ran to extract all attachments from a mailbox. I have the following code below.

Update: I used the exchange online management module to connect to the tenant/download data.

#Scope to Mailbox
$Mailbox = "someemail@somedomain.com"

#Assign attachment location
$OutputFolder = "$env:USERPROFILE\Downloads\Mailbox Attachment Results"

#Create folder if doesn't exist
if (-not (Test-Path -Path $OutputFolder)) {
    New-Item -ItemType Directory -Path $OutputFolder | Out-Null
}

#Get attachments
$Attachments = Get-MailboxFolderStatistics -Identity $Mailbox -FolderScope All -IncludeOldestAndNewestItems | ForEach-Object {
    $Folder = $_.FolderPath.Replace("/", "\")
    $Permissions = Get-MailboxFolderPermission -Identity $Mailbox -User "Default" | Where-Object { $_.AccessRights -like "*CreateItems*" }
    if ($Permissions) {
        $Permissions | ForEach-Object {
            $FolderPermission = $_
            Get-MailboxFolderStatistics -Identity $Mailbox -FolderScope All -IncludeOldestAndNewestItems -Folder $_.FolderPath | Where-Object { $_.FolderPath -eq $FolderPermission.FolderPath } | ForEach-Object {
                $_.Folder.Attachments | ForEach-Object {
                    $Attachment = $_
                    $AttachmentPath = Join-Path -Path $OutputFolder -ChildPath ($Attachment.Name)
                    $Attachment.Content | ForEach-Object {
                        $_.Stream.Position = 0
                        $FileStream = [System.IO.File]::Create($AttachmentPath)
                        $_.Stream.CopyTo($FileStream)
                        $FileStream.Close()
                    }
                }
            }
        }
    }
}

It executes without error, creates the 'Mailbox Attachment Results' folder on my local machine; however, no data from the mailbox is being moved to that folder.

Content search (via Microsoft Purview compliance portal) can not be used due to needing these files on a SharePoint to share out to 100's of end users (and they are far from computer savvy)

Not sure if this is relevant, but the only files needed are images, PDFs, word documents and excel documents. This mailbox has over 30years of historical data as well.

Any suggestions are welcomed outside using Content Search.

Thank you to anyone that can take some time to help here.

Julez
  • 1
  • 1
  • Hi KJ, thank for looking at this :) Not sure if this answers your question, but I'm using the exchange online management module. I connect using my admin creds and then run the script. Or are you saying that what I am trying to accomplish here w/ PS is not doable? – Julez Aug 07 '23 at 17:10
  • Okay, I will look into these links, thank you – Julez Aug 07 '23 at 17:11
  • The ‎Exchange Online Management is not for Mailbox content. You're best using the Microsoft Graph API for [example](https://stackoverflow.com/questions/61787665/microsoft-graphapi-powershell-download-attachment) to do this or the older EWS API (required for In-Place Archive Mailbox content). – jfrmilner Aug 07 '23 at 22:54

0 Answers0