troubleshooting Question

Powershell - incorporate find/replace into

Avatar of Steve Knight
Steve KnightFlag for United Kingdom of Great Britain and Northern Ireland asked on
Microsoft 365Scripting LanguagesPowershell
3 Comments2 Solutions94 ViewsLast Modified:
Follow up question to https://www.experts-exchange.com/questions/29182817/Powershell-add-line-to-millions-of-text-files-EWS.html

I have the above great scripts which will process given text files to add in missing header lines.

I can run those over the files overnight, or I am wondering whether it is easily possible to do as part of my recursive import process? I could of course just call the routine before importing to O365 too but could the transform be done in memory once it has been read in?

Not done a lot of powershell so apologies for any obvious wrongs...

if it's not going to be feasible/logical to do that I will just incorporate the existing scripts either as one-off hit or during the import, quite happy how to do that but always open to better or faster suggestions for any of it!

thanks

Steve

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
        }
    
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 3 Comments.
Try for 7 days

”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.

-Mike Kapnisakis, Warner Bros