There are logs in the folder, in these logs I found the data I needed: and saved everything to a variable:

$Files = Get-ChildItem C:\log\1\* -Include *.log New-Item -ItemType file -Path C:\log\result.txt –Force foreach ($File in $Files) { $StringMatch = $null $StringMatch = select-string $File -pattern "\[Exception\]|\[304 Not Modified\]" if ($StringMatch) {out-file -filepath C:\log\result.txt -inputobject $StringMatch } } 

How can I set the required output format: Log itself:

 20015-10-01 12:00:53.986 War 8 [R: 33] [Ex][Type:Apiion][Err:30][HTTPStatus:NotFound][Message:text: 4950][CorrId:d1ce7555f63803a2] 

Example output: enter image description here

  • you need to make an object and then output what you want - Senior Pomidor
  • @ Senior Automator and more? - upward

1 answer 1

 [string] $s = "20015-10-01 12:00:53.986 War 8 [R: 33] [Ex][Type:Apiion][Err:30][HTTPStatus:NotFound][Message:text: 4950][CorrId:d1ce7555f63803a2]" [string] $s2 = "20015-10-01 13:00:53.986 War 8 [R: 33] [Ex][Type:Apiion][Err:33][HTTPStatus:Found][Message:text: 4930][CorrId:d333333333333a2]" # предположим, что это массив твоих строк из файла [array] $loglines = @($s, $s2) # для примера возьмум 2 $List = New-Object System.Collections.ArrayList # идем по массиву $loglines | ForEach-Object { $arr = $_.split("\[*\]") $Hash = [ordered]@{ # это название столбца = значение # $_ - это одна строка из лога Description = $arr[0] ID = $arr[1] Time = $arr[0] Status = $arr[9] URL = "http://" Message = $arr[11] CorrId = $arr[13] } [void]$List.Add(( [pscustomobject]$Hash )) } $List | Out-GridView -Title 'My Logs' 

conclusion:

 PS> $List Description : 20015-10-01 12:00:53.986 War 8 ID : R: 33 Time : 20015-10-01 12:00:53.986 War 8 Status : HTTPStatus:NotFound URL : http:// Message : Message:text: 4950 CorrId : CorrId:d1ce7555f63803a2 Description : 20015-10-01 13:00:53.986 War 8 ID : R: 33 Time : 20015-10-01 13:00:53.986 War 8 Status : HTTPStatus:Found URL : http:// Message : Message:text: 4930 CorrId : CorrId:d333333333333a2 PS > $List[0] | select -Property ID, Time, Status, URL ID Time Status URL -- ---- ------ --- R: 33 20015-10-01 12:00:53.986 War 8 HTTPStatus:NotFound http:// 

or so

 PS > $List[0] | Format-Table Description ID Time Status URL Message CorrId ----------- -- ---- ------ --- ------- ------ 20015-10-01 12:00:53.986 War 8 R: 33 20015-10-01 12:00:53.986 War 8 HTTPStatus:NotFound http:// Message:text: 4950 CorrId:d1ce7555f63803a2 

beautiful conclusion:

enter image description here

the rest depends on the implementation, but the principle is

  • I thank, and where you can read about all the subtleties and nuances, I just did in one place, and you opened my eyes) - upward
  • @upward experience, Internet, books, but I don’t remember the name already - Senior Pomidor
  • But tell me, I save the result of select-string "[R: ]" $Result variable select-string "[R: ]" but how can I group this result by the same parameter? - upward
  • @upward your question is not very clear, what does $ Result keep in itself? what string do you execute select-string "[R:]"? How to understand "group this result by the same parameter"? - Senior Pomidor