Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用powershell脚本在日志的最后10分钟内搜索模式_Powershell - Fatal编程技术网

如何使用powershell脚本在日志的最后10分钟内搜索模式

如何使用powershell脚本在日志的最后10分钟内搜索模式,powershell,Powershell,我正在尝试在Powershell中创建日志文件监视器。要求使用Powershell在日志文件中搜索最近(最近10分钟日志打印)的模式。 日志文件的格式如下: 20/04/2018 23:15:28: some information 20/04/2018 23:16:28: some information 20/04/2018 23:17:28: some information : 20/04/2018 23:55:28: some information 20/04/2018 23:56:

我正在尝试在Powershell中创建日志文件监视器。要求使用Powershell在日志文件中搜索最近(最近10分钟日志打印)的模式。 日志文件的格式如下:

20/04/2018 23:15:28: some information
20/04/2018 23:16:28: some information
20/04/2018 23:17:28: some information
:
20/04/2018 23:55:28: some information
20/04/2018 23:56:28: some information
例如,脚本在23:58运行,那么它应该只在“20/04/2018 23:48”和“20/04/2018 23:48”之间的日志打印中搜索模式


请告知。谢谢

您总是可以从如下文本文件中获取最后几个条目,比如10条:

Get-Content .\data.txt -Tail 10
当然,这可能不是从最后10分钟开始的。这里有一个选项可以做到这一点:

Get-Content .\data.txt |
        ForEach-Object {$threshold = (Get-Date).AddMinutes(-10)}{
            if($_ -match "^(?<timestamp>(\d{2}/){2}\d{4} (\d{2}:){2}\d{2}):.*$")
            {
                if((Get-Date $Matches.timestamp) -gt $threshold)
                {
                    $_
                }
            }           
        }
获取内容。\data.txt|
ForEach对象{$threshold=(Get Date).AddMinutes(-10)}{
如果($匹配“^(?(\d{2}/){2}\d{4}(\d{2}:){2}\d{2}):.$”)
{
if((获取日期$Matches.timestamp)-gt$threshold)
{
$_
}
}           
}

当然,模式匹配适用于
mm/dd/yyyy
dd/mm/yyyy
日期格式,因为结构相同。但是,如果文件使用与系统相反的格式,则在解析模式时可能会遇到困难。例如,您的示例中的格式在英国(
dd/mm/yyyy
)可以正常工作,但在美国(
mm/dd/yyyy
)可能会失败-我没有测试过它,只是根据以前的日期问题发出警告。

到目前为止您有什么发现?我们不会根据请求编写随时可用的代码。我想您知道Powershell可以使用DateTime对象进行计算,对吧?