Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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 从文本文件分析日期,并提取日期大于X的行_Powershell - Fatal编程技术网

Powershell 从文本文件分析日期,并提取日期大于X的行

Powershell 从文本文件分析日期,并提取日期大于X的行,powershell,Powershell,我有一个客户使用第三方软件存储机密医疗信息。我们现在需要找出哪些用户打开了数据库中的特定记录。我已经和供应商联系过了,他们记录的唯一方式是在每台电脑上的文本文件中(显然)。我需要从每台计算机解析这个文本文件,以提取我需要的信息。这是文本文件中信息的匿名示例-为了便于阅读,我在每行之间添加了空格 登录。| 2012年3月10日| 01:12:45 | John Smith博士| 3 | FS01 |带终端服务Service Pack 1(6.1构建7601)的Windows 7域控制器| 3.12

我有一个客户使用第三方软件存储机密医疗信息。我们现在需要找出哪些用户打开了数据库中的特定记录。我已经和供应商联系过了,他们记录的唯一方式是在每台电脑上的文本文件中(显然)。我需要从每台计算机解析这个文本文件,以提取我需要的信息。这是文本文件中信息的匿名示例-为了便于阅读,我在每行之间添加了空格

登录。| 2012年3月10日| 01:12:45 | John Smith博士| 3 | FS01 |带终端服务Service Pack 1(6.1构建7601)的Windows 7域控制器| 3.12.1

进展记录-新纪录于2012年3月10日创下| 01:13:33 |约翰·史密斯博士| 666241 | 8463 |理查德·特斯特| 1956年9月5日| F.T.| 1 | FS01

进度说明-被用户丢弃| 2012年3月10日| 01:14:29 |约翰·史密斯博士| 666241 | 8463 |理查德测试| F.T.| FS01

我可以很容易地抽出任何一行有问题的记录名,即“Richard test”,但这些日志可以追溯到2012年。有人知道我如何解析每行的日期,以便我可以提取2016年1月1日之后的任何内容吗

import-module activedirectory
$computers = "FS01"#get-adcomputer -filter * | Select-object -ExpandProperty Name

foreach($computer in $computers){

$path = "\\$computer\C$\Users\Public\Documents\HCN\MD\MDTrace.LOG"
If(Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue){
If(Test-Path $Path){
Get-Content -Path $path | foreach { if($_ -match "Thomas Hogan"){Write-Output "$computer -- $_"} }
}
}
}

使用正则表达式提取日期,如:

$cutoff = Get-Date -Year 2013 -Month 1 -Day 1
Get-Content .\log.txt | ? {
    $g = [regex]::Match($_, '(\d\d)/(\d\d)/(\d\d\d\d)').Groups
    (Get-Date -Year $g[3].Value -Month $g[2].Value -Day $g[1].Value) -gt $cutoff 
} | Out-File filtered_log.txt
如果文件较大,则此方法可能更快:

$cutoff = Get-Date -Year 2013 -Month 1 -Day 1
Get-Content .\log.txt -ReadCount 1 | % {
    $_ | ? {
        $g = [regex]::Match($_, '(\d\d)/(\d\d)/(\d\d\d\d)').Groups

        (Get-Date -Year $g[3].Value -Month $g[2].Value -Day $g[1].Value) -gt $cutoff 
    } | Out-File filtered_log.txt -Append
}

经过反复试验,我发现在这种情况下,我可以用split来完成,因为它们总是用|

$computers = "NB04TMPL" #Get-Content D:\computers.txt | Sort-Object
$date = "21/01/2013"
$name = "Richard Test"
foreach ($computer in $computers)
{
    $path = "C:\temp\MDTrace.LOG"
    If (Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue)
    {
        If (Test-Path $Path)
        {
            Get-Content -Path $path | foreach {
                $str = ($_ -split '\|')
                if ($str[1] -gt $date)
                {
                    if ($str[6] -match $name)
                    {
                        Write-Output $_
                    }
                }
            }   
        }   
    }
}

渴望听到任何想法。我不知道这和正则表达式有什么不同。我想RegEx会让我用更少的If获得更多的灵活性

谢谢Dave。我需要更仔细地研究正则表达式。这似乎是一件很难掌握的事情