带有行打印输出的文本模式搜索

Textpattern search with printout of line

我是一名网络工程师,我写了一个小而有效的 PS 脚本来搜索文本模式的日志(或任何文件)。现在这个脚本只输出行、文件名等。现在我想扩展脚本,以便当它找到一行时,它会告诉我该行、文件名等以及该行的内容。

所以它应该是这样的:

LineNumber Filename            Path                           Pattern                                  
---------- --------            ----                           -------                                  
4          190719_Success.log  C:\skripte0719_Success.log  test 

.log 第 4 行的文本应该出现在这里

5          190719_Success.log  C:\skripte0719_Success.log  test  

.log 第 5 行的文本应该出现在这里

抱歉格式问题,希望您明白我的意思。

由于我对 PS 脚本编写还比较陌生,我有点不知道应该如何实现这个目标,或者如果可能的话。

这是我目前的代码:

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
}       
}
}

看看下面的代码。我添加了一个用于处理文件的循环和一些输入验证 + 您对向控制台显示行内容的请求。

代码改编(@Theo:感谢输入)

#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