Question

I am very new to Powershell, and I need some kind of Powershell script. Log files are in D:\Example\Example\log\ folder. It is created day by day like log20140513.log.

I just want to check the logfile's last 1 hour data searching "Runstate is false!" and if it find, it should return 1, else return 0. That's it.

Here is what I have:

$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)
    }
}
Was it helpful?

Solution

you can create an array of all your log files then run your code upon each ones :

$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)
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top