如何使用 PowerShell 脚本在文件中搜索单词
How to search a word in a file using PowerShell script
我有一个包含 350 个文件夹的列表,每个文件夹都有一个文件访问日志。我需要在所有 350 个文件夹下的所有 350 个文件中搜索名称 "Hound",并在其访问日志文件中显示包含名称 "Hound" 的文件夹的名称。
下面是我的代码,有人可以帮助我在这里添加什么以获得所需的输出吗?
#List all the folders in C:\testfolder
$folders = (Get-ChildItem -Path "C:\testfolder" | Where-Object{$_.Attributes -eq "Directory"} | Select Fullname)
#looping all folders
Foreach ($folder in $folders)
{
#Here I need to look for the word "Hound" inside the Access.log file and if the word is there, it should display the name of the $folder which has the word
}
这是一个相当基本的方法:
Get-ChildItem -Path d:\testfolder -Recurse | Select-String -Pattern "Hound"
如果您需要确保只搜索名为 access.log
的文件,请指定过滤器:
Get-ChildItem -Path d:\testfolder -Include "access.log" -Recurse | Select-String -Pattern "Hound"
我有一个包含 350 个文件夹的列表,每个文件夹都有一个文件访问日志。我需要在所有 350 个文件夹下的所有 350 个文件中搜索名称 "Hound",并在其访问日志文件中显示包含名称 "Hound" 的文件夹的名称。 下面是我的代码,有人可以帮助我在这里添加什么以获得所需的输出吗?
#List all the folders in C:\testfolder
$folders = (Get-ChildItem -Path "C:\testfolder" | Where-Object{$_.Attributes -eq "Directory"} | Select Fullname)
#looping all folders
Foreach ($folder in $folders)
{
#Here I need to look for the word "Hound" inside the Access.log file and if the word is there, it should display the name of the $folder which has the word
}
这是一个相当基本的方法:
Get-ChildItem -Path d:\testfolder -Recurse | Select-String -Pattern "Hound"
如果您需要确保只搜索名为 access.log
的文件,请指定过滤器:
Get-ChildItem -Path d:\testfolder -Include "access.log" -Recurse | Select-String -Pattern "Hound"