Search code examples
powershellpowershell-3.0

Parsing Log file - Extracting lines with multiple targets and further parsing results


I have a question regarding some parsing of a log file in powershell 3.0. Help is much appreciated

Log File Example:

.Processing begin...
     -Sending file \\CL2BATCH1\CFGP\PDF\templates\T_Test_Printer_Page.pdf to test the printer...
     [05:15:06 AM] Begin printing file [\\CL2BATCH1\CFGP\PDF\templates\T_Test_Printer_Page.pdf]. Number of pages:1
     [05:15:07 AM] Print completed.

     [5:15:08 AM] Merging PDF files to master PDF file:\\SERVER1\CUSTOMERNAME\PDF\Alt_Man_Cert\P_00292300-00_700700-0000_1_AMC_20200325051507.PDF
      [5:15:08 AM] Merged file:\\SERVER1\CUSTOMERNAME\PDF\Alt_Man_Cert\MC_SCHLIST_000.PDF
      [5:15:08 AM] Merged file:\\SERVER1\CUSTOMERNAME\PDF\Alt_Man_Cert\MC_4729028.PDF
     [05:15:08 AM] Begin printing file [\\SERVER1\CUSTOMERNAME\PDF\Alt_Man_Cert\P_00292300-00_700700-0000_1_AMC_20200325051507.PDF]. Number of pages:2
     [05:15:09 AM] Print completed.
    -----------------------------
       -Number of Accounts selected for this run:1
       -Number of Account successfully printed  :1
       -Number of Account failed to be printed  :0

   ----------------------------------
   Generating In-House School Report: MCPrintReport_700700-0000_1_20200325051507.PDF
     [5:15:10 AM] Merging PDF files to master PDF file:\\SERVER1\CUSTOMERNAME\PDF\Alt_Man_Cert\MCPrintReport_700700-0000_1_20200325051507.PDF
     [05:15:10 AM] Begin printing file [\\SERVER2\CUSTOMERNAME\PDF\Alt_Man_Cert\MCPrintReport_700700-0000_1_20200325051507.PDF]. Number of pages:1
     [05:15:11 AM] Print completed.

I would like to extract lines that have the contain the following criteria:

  1. 'Begin printing file' AND
  2. 'Alt_Man_Cert'

My current code looks through a set of log files and extracts the entire line properly.

$root = "c:\psscripts\mcprintcopy"
$files = Get-ChildItem -Filter MCPrint_*.log -Path $root


foreach($file in $files)
{

    if($file.LastWriteTime.ToShortDateString() -gt (get-date).AddDays(-.5))
    {

        $InStuff = Get-Content -LiteralPath $root\$file
        Write-Host 'Analyzing File: '$file
        $TargetOne = 'Begin printing file'
        $TargetTwo = @(
            'Alt_Man_Cert'
            )
        # this pipeline version otta work with ps3
        $T2_Regex = ($TargetTwo |
            ForEach-Object {
            [regex]::Escape($_)
                }) -join '|'


        $InStuff |
            Where-Object {
                $_ -match $TargetOne -and
                $_ -match $T2_Regex
                }


        $r = [regex] "\[([^\[]*)\]"
        $match = $r.match($InStuff)
        $text = $match.groups[1].value


    }
}

The problem is that I actually only want whats in between the brackets as I will need the directory path to copy those files to another destination.

[\CL2BATCH1\CFGP\PDF\Alt_Man_Cert\P_00292300-00_700700-0000_1_AMC_20200325051507.PDF] and [\CL2BATCH1\CFGP\PDF\Alt_Man_Cert\MCPrintReport_700700-0000_1_20200325051507.PDF]


Solution

  • Combine the patterns and use a capture-group (just like your $r) and the automatic $matches variable for this:

    $InStuff = Get-Content -LiteralPath $root\$file
    
    $filePaths = $InStuff |ForEach-Object {
      if($_ -match 'Begin printing file \[([^\]]*Alt_Man_Cert[^\]]*)\]'){
        $matches[1]
      }
    }
    

    $filePaths will contain the file paths (minus the brackets)