2016-01-21 41 views
1

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

登录| 10/03/2012 | 01:12:45 |约翰·史密斯博士| 3 | FS01 | Windows 7的域控制器的终端服务的Service Pack 1(6.1构建7601)| 3.12.1

进度备注 - 新纪录已打开| 10/03/2012 | 01:13:33 | John Smith博士| 666241 | 8463 | Richard Test^05/09/1956 | .F。| .T。| 1 | FS01

进展注 - 由用户丢弃| 10/03/2012 | 01:14:29 |约翰·史密斯博士| 666241 | 8463 |理查德测试| .F | .T | FS01

我可以很容易地拉出任何有问题的记录名称,即“理查德测试”,但这些日志一直回到2012年。有没有人有任何想法如何我可以解析每行的日期,以便我可以例如01/01/2016后拉什么东西?

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 -- $_"} } 
} 
} 
} 

回答

0

我发现后审和错误我可以在这种情况下使用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叠加的。我想RegEx可以让我获得更多的灵活性,但是If's

1

使用正则表达式来提取日期,是这样的:

$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 
} 
+0

感谢Dave。我需要更密切地关注正则表达式。看起来很困难 –