Parsing 编写powershell脚本分析日志
我对Powershell非常陌生,需要一些Powershell脚本。日志文件位于D:\Example\Example\Log\文件夹中。它是像log20140513.log一样逐日创建的 我只想检查日志文件最近1小时的数据搜索“Runstate is false!”,如果找到,它应该返回1,否则返回0。就这样 以下是我所拥有的:Parsing 编写powershell脚本分析日志,parsing,search,powershell,logging,Parsing,Search,Powershell,Logging,我对Powershell非常陌生,需要一些Powershell脚本。日志文件位于D:\Example\Example\Log\文件夹中。它是像log20140513.log一样逐日创建的 我只想检查日志文件最近1小时的数据搜索“Runstate is false!”,如果找到,它应该返回1,否则返回0。就这样 以下是我所拥有的: $file = "D:\EnterAccount\Gecko\log\log20140513.log" cat $file | Select-String "ERRO
$file = "D:\EnterAccount\Gecko\log\log20140513.log"
cat $file | Select-String "ERROR" -SimpleMatch | select -expand line | foreach {
$_ -match '(.+)\s[ERROR]\s\.\s(.+)' | out-null
new-object psobject -Property @{
Timestamp = [datetime]$matches[1];Error = $matches[2]
} | where {$_.timestamp -gt (get-date).AddDays(-1)
}
}
您可以创建一个包含所有日志文件的数组,然后对每个日志文件运行代码:
$files = get-childitem D:\EnterAccount\Gecko\log\ -filter *.log | ?{ $_.lastwritetime -gt ((get-date).addHours(-1)) }
$files | foreach{
cat $_ | Select-String "ERROR" -SimpleMatch | select -expand line | foreach {
$_ -match '(.+)\s[ERROR]\s\.\s(.+)' | out-null
new-object psobject -Property @{
Timestamp = [datetime]$matches[1];Error = $matches[2]
} | where {$_.timestamp -gt (get-date).AddDays(-1)
}
}
所以在这里帮助人们编程,你不能要求用户做你的工作,你没有展示任何研究/编程的努力。我理解,对不起。但我试过了,但没用,我试过了,但它只检查了一个名为log的日志$file=“D:\entercount\Gecko\log\log20140513.log”cat$file | Select String“ERROR”-SimpleMatch | Select-expand line | foreach{$|-match'(.+)\s\[ERROR\]\s\.+''s\.'''out null新对象psobject-Property@{Timestamp=[datetime]$matches[1];ERROR=$matches[2]}其中${.Timestamp-gt(get date).AddDays(-1)}}您只想在11:00PM之后记录内容,还是只想在文件的最后一个小时记录内容?例如,由于服务崩溃,最后一次记录的内容是在9:43 pm,您想在8:43 pm-9:43 pm之间记录内容由于在这个问题中没有明确说明问题,我将投票暂停。非常感谢Kayasax