2016-01-21 36 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。我需要更密切地關注正則表達式。看起來很困難 –