Hello, I’m new to PS and wondering if anyone can help me. What I am trying to do is force the output of this script to create new lines for every string that it finds in our log file. Currently, the output from this script is one huge block of text. Any help would be greatly appreciated! Here is my full script:
# This script will search a text file for a particular string. # It is designed to return the number of strings found along with the actual line that the string(s) were found in. # Arguments example: d:\powertest.txt,string $logfile_path = $args.get(0); $regex = $args.get(1); $Error.Clear(); if ( !$logfile_path ) { Write-Host "Message: Can't find ""logfile_path"" argument. Check documentation." exit 1 } if ( !$regex ) { Write-Host "Message: Can't find ""regex"" argument. Check documentation." exit 1 } if ( Test-Path $logfile_path ) { } else { Write-Host "Message: File $logfile_path not found." exit 1 } $filename = split-path "$logfile_path" -leaf $t = (Get-Childitem env:temp).value $file_path = "$t\$usage-$filename-$position" if ( Test-Path $file_path ) { $known_rows = get-content $file_path } else { $known_rows = 0 } $resn = @() $resl = @() $total = (get-childitem $logfile_path | select-string -pattern $regex | select linenumber, line | measure-object).count if ($Error.Count -ne 0) { Write-Host "$($Error[0])" exit 1 } if ( $total -lt $known_rows ) { $known_rows = 0 $known_rows > $file_path } $temp = $total - $known_rows $known_rows = $temp + $known_rows $known_rows > $file_path for ( $i = 0 ; $i -le $total; $i++ ) { $resn += @($i) $resl += @($i) } $i = 1 $stat = get-childitem $logfile_path | select-string -pattern $regex | select linenumber, line | ForEach-Object { $resn[$i] = $_.linenumber $resl[$i] = $_.line $i = $i + 1 } if ($temp -eq 0) { write-host "Statistic: $temp" write-host "Message: No newly found strings" exit 0 } if ($temp -gt 0) { $lines = @() write-host "Statistic: $temp" If($temp -gt 1) { for ( $i = 1 ; $i -le $temp; $i++ ) { $lines += $resl[$count - $i] } write-host "Message: Number of newly found strings $temp. Lines that included the error are. $lines" } else { $line = $resl[$resl.Count - 1] write-host "Message: Number of newly found strings $temp. Lines that included the error are $line" } }
Currently the output looks similar to this:
Number of newly found strings 3. Lines that included the error are. 2017-10-26 17:18:30,004 INFO [com.company.workflow.ejb.ProcessRulesEJB]
(EJB default - 7) processWorkflowRule()- rules to process! 2017-10-26 17:17:39,476 INFO [com.company.workflow.ejb.ProcessEnrollmentEJB] (EJB
default - 2) processEnrollmentFile() - No enrollments were found 2017-10-26 17:17:29,991 INFO [com.company.workflow.ejb.ProcessRulesEJB] (EJB
default - 4) processWorkflowRule()- rules to process!
I want it to looks like this:
Number of newly found strings 3. Lines that included the error are:
2017-10-26 17:18:30,004 INFO [com.company.workflow.ejb.ProcessRulesEJB] (EJB default - 7) processWorkflowRule()- rules to process!
2017-10-26 17:17:39,476 INFO [com.company.workflow.ejb.ProcessEnrollmentEJB] (EJB default - 2) processEnrollmentFile() - No enrollments were found
2017-10-26 17:17:29,991 INFO [com.company.workflow.ejb.ProcessRulesEJB] (EJB default - 4) processWorkflowRule()- rules to process!