looking for feedback/help logfile search utility

Welcome Forums General PowerShell Q&A looking for feedback/help logfile search utility

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
11 months ago.

  • Author
    Posts
  • #91817

    Participant
    Points: 1
    Rank: Member

    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() 
    
    
    
  • #91840

    Participant
    Points: 332
    Helping Hand
    Rank: Contributor

    It works in the PoSH console because the console native allows for. Once you are outside of the console, that falls on you to address. So, you have to do this in your forms (WinForm or XAML) code or not use a dialog box, and instead use HTML/HTA UI to do what you are after.

    For example – to do this in a form textbox UI object, it's just this:

    $textbox1.ForeColor='yellow'
    $textbox1.BackColor='darkblue'

    Now for specific strings inserted into a textbox, that is a whole different thing.

    If this were me, I'd keep it simple by putting that Id in its own textbox (readonly) above the RichTextBox and leave the remaining text as is, ... for example ...

        # create textbox for log ID 
        $LogIDInputTextBox = new-object System.Windows.Forms.textbox
        $LogIDInputTextBox.location = new-object System.Drawing.point(400,300)
        $LogIDInputTextBox.Name = "LogIDInputTextBox"
        $LogIDInputTextBox.ForeColor = [Drawing.Color]::Red
        $LogIDInputTextBox.Text = 'Log ID is displayed here'
        $LogIDInputTextBox.Size = new-object System.Drawing.Size(150,30)
        $Form.Controls.Add($LogIDInputTextBox)
    

    or maybe put that Id in the forms title bar along with the logs name.

    • #91936

      Participant
      Points: 1
      Rank: Member

      thanks Postanote for your response.
      the reason i had for "Hi-Lighting" the Student ID number was that is what im searching for, and to make it easier to read through the logs to find information on what pertains to the Student ID in the existing log files.
      i would love to eventually make this a better performing script/gui for this purpose but i guess ill have to just except the output of the log as a solution. it does do what i want it to do... was just looking to bring more attention to the Student ID in question for better readability. it does search through all the directories and finds any text logs that have that student ID in them. it seemed it was possible to do, and that i was missing something.

      but you are saying that HTML/HTA UI would be able to provide what im looking to do? is there any search terms that may help narrow down my google search in my attempts at learning more about that?

The topic ‘looking for feedback/help logfile search utility’ is closed to new replies.