Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 带打印输出的文本模式搜索_Powershell_Full Text Search_Text Search - Fatal编程技术网

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

Powershell 带打印输出的文本模式搜索,powershell,full-text-search,text-search,Powershell,Full Text Search,Text Search,我是一名网络工程师,我编写了一个小型但有效的PS脚本来搜索日志(或任何相关文件)中的文本模式。现在这个脚本只输出行、文件名等。现在我想扩展脚本,这样当它找到一行时,它会告诉我行、文件名等等,还有那行的内容 所以它应该是这样的: LineNumber Filename Path Pattern ---------- -------- -

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

所以它应该是这样的:

LineNumber Filename            Path                           Pattern                                  
---------- --------            ----                           -------                                  
4          190719_Success.log  C:\skripte\190719_Success.log  test 
.log第4行的文本应显示在此处

.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:感谢您的输入)


先生,你真了不起。我已经试过了,到目前为止效果很好,但是是否可以立即检查文件而不是先收集?如果我想在exchange日志中搜索特定的文本,可能需要几天的时间才能运行,因为我测试过的exchange上大约有80.000个日志。您可以用
Get ChildItem | foreach Object-Process{}
循环替换
foreach($Files中的File)
。因此,这些文件将被直接处理。请尝试修改我的脚本,如果您需要任何帮助,请告诉我们。
Select String
有一个
-Path
参数,因此您不需要先使用
Get Content
。另外,我会在使用前使用searchpattern,尤其是因为用户可以键入任何内容,并且任何内容都可能包含regex-specialcharacters@Theo:当您直接使用
-Path
参数时,是否有办法获取匹配行的内容?谢谢大家,我会试着自己修改它,然后进行修改,如果我有更多的问题,或者当我有新的草稿时,我会让你知道。
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
}       
}
}
#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