function ProcessFolder([string]$ThisFolderPath,[Microsoft.Exchange.WebServices.Data.FolderID]$MailFolderParentID) {
############ Get list of all folders in the file system ready to import - recursing manually to handle parent folder ID's more easily ############
$Folders=get-childitem -path $ThisFolderPath -directory -force -erroraction SilentlyContinue |select-object Name,FullName
############ Loop through all files and folders in this level and recursively call ProcessFolder to process sub-folders and messages ###########
Write-Progress -id 1 "Email Import ... started at $StartTime" "Working on messages in $MailBoxName [$EmailCounter of $EmailTotalCount]" -perc (($EmaiLCounter-1)/$EmailTotalCount*100)
foreach ($Folder in $Folders) {
$FolderCounter=$FolderCounter+1
$FolderStart=Get-Date -Format "dd-MMM yyyy HH:mm:ss"
$FolderPath=$Folder.FullName | split-path -Parent
# Make Mail folder and assign ID to $ThisFolder
Write-host "PROCESSING FOLDER : $($Folder.FullName)" -BackgroundColor DarkGreen
$ThisFolder=[Microsoft.Exchange.WebServices.Data.FolderID] (Create-Folder -NewFolderName "$($Folder.Name)" -EWSParentFolderID $MailFolderParentID)
Write-Progress -id 3 -ParentID 1 -Activity "Folder import for $MailboxName" "Started at $FolderStart" -CurrentOperation "Working on folder $($Folder.Name) [$FolderCounter of $FolderTotalCount]" -perc (($FolderCounter)/$FolderTotalCount*100)
$ThisFolderMessages=(Get-ChildItem $($Folder.FullName) -file | Where-Object {$_.name -like "*.Eml"})
# Process messages in this folder
foreach ($email in $ThisFolderMessages ) {
$EmailtoImport = $($Folder.FullName) + "\" + $Email.Name
Write-verbose " Importing: $EmailtoImport "
$UploadEmail = new-object Microsoft.Exchange.WebServices.Data.EmailMessage($service)
#Read File
[byte[]]$EmailinByte=get-content -encoding byte $EmailtoImport -Readcount 0
#Set Mime Content in Message
$UploadEmail.MimeContent = new-object Microsoft.Exchange.WebServices.Data.MimeContent("us-ascii", $Emailinbyte);
$PR_Flags = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition (3591, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);
$UploadEmail.SetExtendedProperty($PR_Flags,"1")
try {
$UploadEmail.Save($ThisFolder)
$MessageCounter=$MessageCounter + 1
rename-item -Path $EmailToImport -NewName ( $Email.Name -replace '.eml','.imported' )
}
catch {
Write-Host " ERROR importing $EmailToImport : $Error[0]" -ForegroundColor Red
}
Write-Progress -id 2 -ParentID 1 -Activity "Message import for $MailboxName" "Started at $FolderStart" -CurrentOperation "Messages so far [$MessageCounter of $MailboxEMLCount]" -perc ($MessageCounter/$MailboxEMLCount*100)
}
# Now process any sub folders of this folder
ProcessFolder -ThisFolderPath "$($Folder.FullName)" -MailFolderParentID $($ThisFolder.UniqueID)
}
# return $true
}
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Most Valuable Expert award recognizes technology experts who passionately share their knowledge with the community, demonstrate the core values of this platform, and go the extra mile in all aspects of their contributions. This award is based off of nominations by EE users and experts. Multiple MVEs may be awarded each year.
The Most Valuable Expert award recognizes technology experts who passionately share their knowledge with the community, demonstrate the core values of this platform, and go the extra mile in all aspects of their contributions. This award is based off of nominations by EE users and experts. Multiple MVEs may be awarded each year.