Improving the find string script.

Dear export

Below is the PS script finding the Pattern in a folder now I want to improve it by if it founds Patterns It will inform us in the
$SMAAcheck
$STcheck
$LEDcheck
$FFAcheck
Which text line number it finds.

Thanks

$time = (Get-Date).AddDays(-1)
$Pattern = "Error"
$PatternSMAA = "UIFSMAAMbr"
$PatternLED = "UIFLEDMbr"
$PatternST = "UIFSTbr"
$PatternFFA = "UIFFFAMbr"
$Path = "\\server\logs"
$files = Get-ChildItem -Path $Path *.* -Recurse -File | Where-Object {$_.LastWriteTime -gt $time} | Select -ExpandProperty FullName
$MatchingFiles = @()
$files | ForEach `
{
    foreach ($line in (Get-Content $_ -ReadCount 250))
    {
        If ( $line -match $pattern )
        { $MatchingFiles += $_; break }
    }
}

$resultSMAA = @($MatchingFiles | ForEach `
{
    foreach ($line in (Get-Content $_ -ReadCount 250))
    {
        If ( $line -match $PatternSMAA )
        { $_; break }
    }
})
$resultLED = @($MatchingFiles | ForEach `
{
    foreach ($line in (Get-Content $_ -ReadCount 250))
    {
        If ( $line -match $PatternLED )
        { $_; break }
    }
})
$resultST = @($MatchingFiles | ForEach `
{
    foreach ($line in (Get-Content $_ -ReadCount 250))
    {
        If ( $line -match $PatternST )
        { $_; break }
    }
})
$resultFFA = @($MatchingFiles | ForEach `
{
    foreach ($line in (Get-Content $_ -ReadCount 250))
    {
        If ( $line -match $PatternFFA )
        { $_; break }
    }
})

if ($resultSMAA.count -gt 0) {$SMAAcheck=1}
else{$SMAAcheck=0}
if ($resultST.count -gt 0) {$STcheck=1}
else{$STcheck=0}
if ($resultLED.count -gt 0) {$LEDcheck=1}
else{$LEDcheck=0}
if ($resultFFA.count -gt 0) {$FFAcheck=1}
else{$FFAcheck=0}
$SMAAcheck
$STcheck
$LEDcheck
$FFAcheck

Open in new window

LVL 1
WeTiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Try this; it will return custom objects with all relevant information:
$time = (Get-Date).AddDays(-1)
$path = "\\server\logs"
$patternList = @{
	'UIFSMAAMbr' =	'SMAA'
	'UIFLEDMbr' =	'LED'
	'UIFSTbr' =		'ST'
	'UIFFFAMbr' =	'FFA'
}

$pattern = $patternList.Keys -join '|'
Get-ChildItem -Path $Path -Filter *.* -Recurse -File |
	Where-Object {($_.LastWriteTime -gt $time) -and (Select-String -Path $_.FullName -Pattern 'Error' -List)} |
	Select-String -Pattern $pattern |
	Select-Object -Property Path, LineNumber, Line, @{n='Type'; e={$patternList[$_.Matches.Value]}}, @{n='Pattern'; e={$_.Matches.Value}}

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WeTiAuthor Commented:
Hi oBdA

Now your code works, I wonder if its possible to insert all the result in a variable? Like: $result.
1
oBdACommented:
Just assign the variable:
$time = (Get-Date).AddDays(-3600)
$path = "\\server\logs"
$path = "C:\Temp\log"
$patternList = [ordered]@{
	'UIFSMAAMbr' =	'SMAA'
	'UIFLEDMbr' =	'LED'
	'UIFSTbr' =		'ST'
	'UIFFFAMbr' =	'FFA'
}

$pattern = $patternList.Keys -join '|'
$result = Get-ChildItem -Path $Path -Filter *.* -Recurse -File |
	Where-Object {($_.LastWriteTime -gt $time) -and (Select-String -Path $_.FullName -Pattern 'Error' -List)} |
	Select-String -Pattern $pattern |
	Select-Object -Property Path, LineNumber, Line, @{n='Type'; e={$patternList[$_.Matches.Value]}}, @{n='Pattern'; e={$_.Matches.Value}}

Open in new window

1
Defend Against the Q2 Top Security Threats

Were you aware that overall malware worldwide was down a surprising 42% from Q1'18? Every quarter, the WatchGuard Threat Lab releases an Internet Security Report that analyzes the top threat trends impacting companies worldwide. Learn more by viewing our on-demand webinar today!

WeTiAuthor Commented:
Why I can't dedicate the points?...
0
oBdACommented:
No idea? What's different than usual?
0
WeTiAuthor Commented:
My Best solution buttom is gone... Asking expert-exchange support right now...
0
Shaun VermaakTechnical Specialist IVCommented:
1
WeTiAuthor Commented:
oBdA Thanks
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.