Search code examples
filepowershellcountalarm

Run a script that counts occurences of token in all / certain files in a folder and adds the results


In this thread I was helped to put together a script that splits a text-files lines into tokens, count the occurrences of a certain token and sends the output to a presented table. Looks like this:

$alarmList = @(Get-Content -Path '.\A.ALM') | foreach { 
$token = $_ -split ' +'
[PSCustomObject]@{
     date = $token[0]
     time = $token[1]
     H2   = $token[2]
     H3   = $token[3]
     H4   = $token[4]
     H5   = $token[5]
     H6   = $token[6]
     H7   = $token[7]
     H8   = $token[8]
}
}
$alarmList | Group H4 -NoElement | Select Name,Count | Out-Gridview

Next I want to achieve a script-run over the files in a folder. Then put together the count and present it in the same way. Basically it is a script for alarm statistics.

I am stuck on the part for running the script for multiple files. This seems to work correctly:

foreach ($i in Get-ChildItem .\) {$alarmList = @(Get-Content -Path $i)}

When i try to tie more functions it don't work though.

foreach ($i in Get-ChildItem .\) {$alarmList = @(Get-Content -Path $i)} |  
foreach { 
    $token = $_ -split ' +'
    [PSCustomObject]@{
         date = $token[0]
         time = $token[1]
         H2   = $token[2]
         H3   = $token[3]
         H4   = $token[4]
         H5   = $token[5]
         H6   = $token[6]
         H7   = $token[7]
         H8   = $token[8]
    }
}
$alarmList | Group H4 -NoElement | Select Name,Count | Out-File $.txt

I suspect that the syntax for second for-loop is incorrect since powershell returns

An empty pipe element is not allowed.

A nod in the right direction would be appreciated.

The data files look like this and are all called something with .alm:

2018-05-19  00:26:00,551 [LUNSC1  ] D_TA204_GT1_DV_AL             CFN              LARM      D_TA204_GT1_Reglerfel                  
2018-05-19  00:28:01,049 [LUNSC1  ] D_TA204_GT41_DV_AL            CFN              LARM      D_TA204_GT41_Reglerfel                 
2018-05-19  00:28:01,049 [LUNSC1  ] D_TA204_GT31_DV_AL            CFN              LARM      D_TA204_GT31_Reglerfel                 
2018-05-19  00:28:01,049 [LUNSC1  ] D_TA204_GT21_DV_AL            CFN              LARM      D_TA204_GT21_Reglerfel                 
2018-05-19  00:35:19,627 [LUNSC1  ] U_TA364_GT11_LARM             CFN              LARM      U_TA364_GT11_LARM                      
2018-05-19  00:39:56,135 [LUNSC1  ] U_TA364_GT11_LARM             CFN              LARM      U_TA364_GT11_LARM 

I for example have 5 files that all contain data like above.


Solution

  • I implemented a script that merges text files and ran this first, then appended the working script to count tokens for a single file. Looks like this:

    dir C:\temp\Powershell\* -include *.alm -rec | gc | out-file C:\temp\Powershell\Total.txt 
    $alarmList = @(Get-Content -Path '.\Total.txt') | foreach { 
            $token = $_ -split ' +'
            [PSCustomObject]@{
                 date = $token[0]
                 time = $token[1]
                 H2   = $token[2]
                 H3   = $token[3]
                 H4   = $token[4]
                 H5   = $token[5]
                 H6   = $token[6]
                 H7   = $token[7]
                 H8   = $token[8]
            }
            }
    $alarmList | Group H4 -NoElement | Select Name,Count | Out-File c:\temp\Powershell\Sum.txt
    

    Not the most elegant since i had to mix batch and powershell. But it's what i came up with and it worked. Next step is to take the first token of the last row, and make this the name of the summed up file. Lastly make it run once a month and i'm good.