powershell - 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
}
}
解决方案
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
推荐阅读
- c# - 如何将用户提供的登录凭据与存储在数据库中的盐和哈希进行比较以允许用户登录
- asp.net-core - 如何在 .net Identity 中创建注册审核/批准流程
- python - 如何对函数中的项目进行范围划分?
- java - DefaultHttpClient (httpclient-4.2.6) 的连接中止异常
- bluedata-3.7 - 如何使用 BlueData REST API 处理会话?
- java - ArrayList和LinkedList插入10,000,000个元素的耗时问题
- postgresql-9.3 - 错误:PostgreSQL 中“from”处或附近的语法错误
- javascript - pugjs 渲染转义的 html 和正常的 html
- python - 使用 Jupyter 笔记本在 python 中进行相对导入
- javascript - vue.js:属性或方法“”未定义,渲染错误:“TypeError:无法读取未定义的属性”