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.