ファイル内の文字列を検索して、文字列を含むファイル名を表示しようとしています
以下のようなスクリプトを作成しました。
Function GetFileContainsData ([string] $Folderpath,[string] $filename, [string] $Data) {
$Files=Get-ChildItem $Folderpath -include $filename -recurse | Select-String -pattern $Data | group path | select name
return,$Files
}
$configFiles= GetFileContainsData "C:\MyApplication" "web.config" "localhost"
Foreach ($file in $configFiles) { Add-Content -Path "Filelist.txt" -Value $file.Name}
このスクリプトは、文字列「localhost」を含むすべてのファイル名をFilelist.txtに書き込みます。
複数の文字列を見つけたいのですが。配列を渡すと
$stringstofind=("localhost","$env:ComputerName")
Foreach ($strings in $stringsToFind) {
$configFiles= GetFileContainsData $Installpath "web.config" $strings
Foreach ($file in $configFiles) { Add-Content -Path "Filelist.txt" -Value $file.Name}
}
ファイルのリストとともに配列内の各文字列を検索し、更新します。同じファイルに両方の文字列がある場合、Filelist.txtにそのファイルの2つのエントリがあります。
ファイル内で複数の文字列を見つけて、ファイルの名前をリストすることは可能ですか?[ファイル名の冗長な入力を排除するため]