I'm an Network Engineer an I wrote a small but effective PS Script to search logs(or any file for that matter) for textpatterns. Now this script only outputs the line, filename and so on. Now I wanted to extend the script so that when it finds a line, it'll tell me the line, filename and so on but also the contents of that line.
So it should look like this:
LineNumber Filename Path Pattern
---------- -------- ---- -------
4 190719_Success.log C:\skripte\190719_Success.log test
Text that's on Line 4 of the .log should appear here
5 190719_Success.log C:\skripte\190719_Success.log test
Text thats on Line 5 of the .log should appear here
Sorry for the formatting, I hope you get what I mean.
Since I'm relatively new to PS Scripting Im kinda lost how I should achieve this goal or if thats even possible.
Here is my code sofar:
Clear-Host
$Pfad = Read-Host "Bitte Pfad angeben" #Enter Directory Path to Search
$Suchbegriff = Read-Host "Suchbegriff eingeben" #Enter Pattern to search for
New-Item -ItemType directory -Path C:\Skripte -erroraction 'silentlycontinue' #create C:\Skripte Folder
Remove-Item -Path C:\Skripte\Suchergebnis.txt -erroraction 'silentlycontinue' #Cleanup from previous run
Remove-Item -Path C:\Skripte\Indizierung.csv -erroraction 'silentlycontinue' #Cleanup
cd $Pfad
echo $file.fullname
echo ""
select-string -Path .\*.* -Pattern "$Suchbegriff" -erroraction 'silentlycontinue' | Select-Object LineNumber,Filename,Path,Pattern | ft -wrap #Search the specified Directory
echo ""
while(($Create = Read-Host -Prompt "Unterordner durchsuchen? J für Ja, N für Nein") -ne "x") #Userinput if Subdirectorys should be searched aswell
{
switch ($Create)
{
'J'
{
Get-Childitem -erroraction 'silentlycontinue' | Get-ChildItem -Recurse -erroraction 'silentlycontinue' | Where-Object {$_.PSIsContainer} | Export-CSV -NoClobber -NoTypeInformation -Path C:\Skripte\Indizierung.csv #Get all Subdirectorys and put them in a CSV, two GCI are needed to reliably get all subdirectories.
$Files = import-csv -Delimiter ',' -Path C:\Skripte\Indizierung.csv #Import CSV
foreach ($File in $Files)
{
cd $file.fullname
echo $file.fullname
echo ""
select-string -Path .\*.* -Pattern "$Suchbegriff" -erroraction 'silentlycontinue' | Select-Object LineNumber,Filename,Path,Pattern | ft -wrap
echo ""
pause
exit
}
}
'n'
{
pause
Exit
}
}
}
Take a look at the following code. I added a loop for processing the files and some validation for the input + your request for the displaying the line content to the console.
Code adapted (@Theo: thanks for input)
#Get input
[System.String]$SearchPath = Read-Host -Prompt 'Enter path'
[System.String]$SearchPattern = Read-Host -Prompt 'Enter search pattern'
[System.String]$SearchRecurse = Read-Host -Prompt 'Search recurse (Y/N)'
#Validate input
if ((-not $SearchPath) -or (-not (Test-Path -Path $SearchPath)))
{
throw ('Path "' + $SearchPath + 'is not available!')
}
if ((-not $SearchPattern))
{
throw ('Search pattern is empty!')
}
if (('Y', 'N') -notcontains $SearchRecurse)
{
throw ('Search recurse parameter "' + $SearchRecurse + ' is not valid!')
}
#Get all files
Out-Host -InputObject 'Get all files...'
[PSCustomObject[]]$Files = @()
if ($SearchRecurse -eq 'Y')
{
$Files = Get-ChildItem -Path $SearchPath -File -Recurse -Force #Collect also files from subfolders
}
else
{
$Files = Get-ChildItem -Path $SearchPath -File -Force #Collect only files from the current folder
}
#Search for string
Out-Host -InputObject 'Search for string...'
[PSCustomObject[]]$Output = @()
foreach ($File in ($Files)) #Process each file
{
Out-Host -InputObject $File.FullName
$Output += Select-String -Path $File.FullName -Pattern $SearchPattern | Select-Object -Property LineNumber, Filename, Path, Pattern, Line
}
$Output | Format-Table -Wrap