I’ve been working on this for a couple weeks, ( off and on ).
basic premise is to search through a directory structure of log files in text format, for any logfile that has a specific ID number in the text… and then present that log file in the richtextbox with that ID number hi-lighted in a different color for easy readability.
i can get a basic output in just plain power shell, where it will show that ID number in red, above where it actually resides in the log file, so that it is easier to pick out where that ID number is, in the log. i suspect my issue is in where it doesn’t seem to break down the log file to separate words. it seems to work in the powershell only version, but trying to convert it to a GUI version doesn’t seem to be working as expected.
some issues i have run into while attempting to transfer this to a GUI .net design is the formatting of the text log file will not show up as it is written in the text file, such as line breaks, spaces, etc. ive used “get-content -raw” and
“$word.split(” “)” as ways to get past these issues.
I’ve tried various version of my script to get closer to a final script, but i’m still not yet there, and need help/suggestions.
any thoughts and/or suggestions very much appreciated
here is my current code;
# Load Assemblies Add-Type -AssemblyName system.windows.forms | Out-Null Add-Type -AssemblyName system.drawing | Out-Null Add-Type -AssemblyName windowsformsintegration | Out-Null #Draw Main form $Form = New-Object System.Windows.Forms.Form $Form.width = 875 $Form.height = 875 $Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D $Form.Text = “Student Log Lookup Utility Ver 1” $Form.startposition = “centerscreen” $Form.KeyPreview = $True $form.AutoSize = $true Function SearchLogs { $richtextbox1.clear() [string]$pattern = $StuIDInputTextBox.text # searchs through out this whole directory structure. this is the list of log files that contains the pattern. $SearchForFiles = get-childitem -path "C:\Daily-Student-Functions\Feedback\*.txt" -recurse | select-string -pattern $pattern | group path | select name $TotalFoundLogfileCount = ( $SearchForFiles | measure-object).count $richtextbox1.appendtext("there are $TotalFoundLogfileCount files that have the student $pattern in it") $richtextbox1.appendtext("`n") # this is the full path of any files that contain the Pattern $FileName = $SearchForFiles.name foreach($one in $FileName){ $richtextbox1.appendtext("### **** Begining of $one File **** ##") $seperateWords = Get-Content $one -raw #| Out-Null $richtextbox1.appendtext( "before foreach, pattern equals $pattern `n") $richtextbox1.appendtext( "before foreach, seperate words equals $seperateWords `n") foreach ($thing in $seperateWords){ $word = $thing -split(" ") if($word -match $pattern){ $richTextBox1.selectioncolor = [drawing.color]::red } } } } $richtextbox1.appendtext("### **** End Of $one File ******* ##") # create Richtextbox for feedback $richTextBox1 = new-object System.Windows.Forms.RichTextBox $richTextBox1.Location = new-object System.Drawing.Point(24,335) $richTextBox1.size = new-object system.drawing.size(800,400) $richTextBox1.ReadOnly = $True # $richTextBox1.forecolor = [drawing.color]::green $form.controls.add($richTextBox1) # create label for above textbox for input of Student ID number $InputStuIDLabel = New-Object System.Windows.Forms.Label $InputStuIDLabel.location = New-Object system.drawing.point(24,20) $InputStuIDLabel.size = new-object System.Drawing.Size(175,30) $InputStuIDLabel.Text = "Input Student ID number Here" $form.controls.add($InputStuIDLabel) # Create textbox for input of Student ID $StuIDInputTextBox = new-object System.Windows.Forms.textbox $StuIDInputTextBox.location = new-object System.Drawing.point(24,50) $StuIDInputTextBox.Name = "StuIDInputTextBox" $StuIDInputTextBox.Size = new-object System.Drawing.Size(150,30) $Form.Controls.Add($StuIDInputTextBox) #Create "Search Student Logs via Stu ID" button $SearchStudentLogs = new-object System.Windows.Forms.Button $SearchStudentLogs.Location = new-object System.Drawing.point(200,30) $SearchStudentLogs.Size = new-object System.Drawing.Size(215,40) $SearchStudentLogs.Text = “Search Student Creation Logs” # when button is clicked, perform the function inside the {} $SearchStudentLogs.Add_Click({SearchLogs}) $Form.Controls.Add($SearchStudentLogs) # create label for above richtextbox1 $OutputRichTextLabel = New-Object System.Windows.Forms.Label $OutputRichTextLabel.location = New-Object system.drawing.point(24,300) $OutputRichTextLabel.size = new-object System.Drawing.Size(250,30) $OutputRichTextLabel.Text = "Student Logs will appear here" $form.controls.add($OutputRichTextLabel) # Create "Exit" button. $Buttonexit = new-object System.Windows.Forms.Button $Buttonexit.Location = new-object System.Drawing.point(24,800) $Buttonexit.Size = new-object System.Drawing.Size(75,24) $Buttonexit.Text = “Exit” $Buttonexit.Add_Click({$form.Close()}) $Form.Controls.Add($Buttonexit) #Create the Form and show it in windows $Form.Add_Shown({$Form.Activate()}) $Form.ShowDialog()