最短・最小構成でIISログ(.log / .txt)をサクッと全文検索できるPowerShell関数を紹介します。正規表現なし、単純一致のみに割り切ることで、現場での“とりあえず今すぐ探したい”に最速で応えます。
Find-IISLog.ps1などの名前で保存してください。
function Find-IISLog {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$Path, # さがすフォルダ or ファイル
[Parameter(Mandatory)]
[string]$Keyword, # 単純一致のキーワード
[switch]$Recurse, # 再帰(サブフォルダも)
[string[]]$Include = @('*.log','*.txt') # 対象拡張子
)
# フォルダ or ファイルを解決
$targets =
if (Test-Path $Path -PathType Leaf) {
Get-Item $Path
} else {
Get-ChildItem -Path $Path -File -Include $Include -Recurse:$Recurse -ErrorAction SilentlyContinue
}
# 単純一致で検索(出力はファイル名, 行番号, 行テキスト)
$targets | Select-String -SimpleMatch -Pattern $Keyword |
Select-Object Path, LineNumber, Line
}
Find-IISLog -Path 'D:\IISLogs' -Keyword 'wp-login.php' -Recurse
Find-IISLog -Path 'D:\IISLogs\u_ex240925.log' -Keyword ' 500 '
Find-IISLog -Path 'D:\IISLogs' -Keyword '404' -Recurse -Include *.log,*.txt,*.csv
Find-IISLog -Path 'D:\IISLogs' -Keyword 'login' -Recurse |
Export-Csv -NoTypeInformation -Encoding UTF8 'hits.csv'
Get-ChildItem 'D:\IISLogs' -Recurse -Include *.log,*.txt -File |
Select-String -SimpleMatch '404' |
Select-Object Path, LineNumber, Line
-SimpleMatch はそのまま文字列一致です。スペースや大文字小文字の差に注意(大文字小文字は既定で無視)。IPやURL断片は前後の空白も含めると意図せぬヒットが減ります。-Recurse を付け忘れていないか確認。拡張子が .log / .txt 以外なら -Include に追加。Get-Content -Encoding ... で変換してから検索を。notepad $PROFILE # 開いて関数を追記、保存function Find-500 { param($p='D:\IISLogs') Find-IISLog -Path $p -Keyword ' 500 ' -Recurse }まず結論:ここだけ見ればOK✅…