首页 > 解决方案 > Powershell log scanning (multiple outputs)

问题描述

im pretty new to ps, and looking to add some advance features to my ps script.

Currently the script (below) runs and scans a log file for a specific string, and outputs it to a 'results' txt file.

I'm looking to get it to scan for different type of strings and output to 2x reports.

Any thoughts would be much appreciated..

Current script:

$log = get-content pitstop_email.log* 
foreach ($line in $log) { 
if ($line -like "*CamelHttpResponseCode=200*") {
$line | out-file -FilePath "Results.txt" -Append
    }
}

标签: powershell

解决方案


I pulled some lines of code from a similar script I made a while back and this should get you going. If you need to do multiple log files, throw the whole script into a for-each loop. I currently have it set to put the results into $Results variable but if you are expecting a significant number of results you should direct the matches to a file or you will run into serious performance issues.

#region --------------------------------------------- [Manual Configuration] ----------------------------------------------------

$StringsToSearchFor = $('CamelHttpResponseCode=200')
$LogFile = "C:\Admin\SomeFiles.txt"

#endregion [Script Prerequisits],#')}]#")}]#'")}]

#region----------------------------------------- [Initializations & Prerequisites] -----------------------------------------------
    #region [Script Prerequisits] ---------------------------------------------------------
    If ([system.io.file]::Exists($LogFile)) {
        Try {
            (New-Object -TypeName 'System.IO.FileStream' -ArgumentList ($LogFile),([System.IO.FileMode]::Open),([System.IO.FileAccess]::Read),([System.IO.FileShare]::Read),4096,([System.IO.FileOptions]::None)).Dispose()
            Write-Information -MessageData "Successfully validated that header file is writeable."
        } Catch {
            Write-nLog -Type Fatal -Message "Unable to read log file. (Full Path: '$LogFile')"
        }
    }
    #endregion [Script Prerequisits],#')}]#")}]#'")}]
#endregion [Script Prerequisits],#')}]#")}]#'")}]

#region ------------------------------------------------- [Main Script] ---------------------------------------------------------
    #region [Build Regex String] ---------------------------------------------------------
    
        New-Variable -Force -Name RegexSearcher   -Value ([String]$Null)                                                  -Description "Hold the regex-formatted search."
        New-Variable -Force -Name RegexEscapeList -Value (New-Object -TypeName 'System.Collections.Generic.List[string]') -Description "Conver the `$StringsToSearchFor variable to a list of regex-escaped strings."
        ForEach ($String in $StringsToSearchFor) {
            $RegexEscapeList.Add([regex]::Escape($String))
        }
        Set-Variable -Name RegexSearcher -Value "^\b(($($RegexEscapeList -Join (')|('))))\b$" -Force
    #endregion [Build Regex String],#')}]#")}]#'")}]

    #region [Process File] ---------------------------------------------------------
        New-Variable -Force -Name Stream          -Value (New-Object -TypeName 'System.IO.FileStream' -ArgumentList $LogFile,([System.IO.FileMode]::Open),([System.IO.FileAccess]::Read))
        New-Variable -Force -Name File            -value (New-Object -TypeName 'System.IO.StreamReader' -ArgumentList $Stream,([Text.Encoding]::UTF8),$False,"10000")
        New-Variable -Force -Name Results         -Value (New-Object -TypeName 'System.Collections.Generic.List[string]')
        While ($File.Peek() -GE 0) {
            [Regex]::match($File.ReadLine(),$RegexSearcher,1).where({$_.Success -eq $True}) | ForEach {
                $Results.Add($_)
            }
    
            $Line = $Null
        }
        #Close the file so other resources can use it.
        $File.close()
    #endregion [Build Regex String],#')}]#")}]#'")}]

#Script is complete!
$Results

推荐阅读