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?
 
oBdAConnect With a Mentor Commented:
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
 
WeTiAuthor Commented:
Hi oBdA

Now your code works, I wonder if its possible to insert all the result in a variable? Like: $result.
1
 
oBdAConnect With a Mentor Commented:
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
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

 
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/DeveloperCommented:
1
 
WeTiAuthor Commented:
oBdA Thanks
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.