Skip to content

Instantly share code, notes, and snippets.

@TakashiSasaki
Last active March 16, 2024 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TakashiSasaki/fa7a3f673882568a0a51eedaf0f85eaa to your computer and use it in GitHub Desktop.
Save TakashiSasaki/fa7a3f673882568a0a51eedaf0f85eaa to your computer and use it in GitHub Desktop.
HKEY_CLASSES_ROOT_20240316.txt
HKEY_CURRENT_CONFIG_20240316.txt
HKEY_CURRENT_USER_20240316.txt
HKEY_LOCAL_MACHINE_20240316.txt
HKEY_USERS_20240316.txt

export-registry-trees.ps1

レジストリ全体をトップレベル直下の5つのツリーに分けてテキストファイルにエクスポートする。

search-registry-trees.ps1

エクスポートした複数のファイルに対して、特定の文字列を値に含むレジストリエントリを探してレジストリキーとともに表示する。

コードの作成のお手伝いをしてくれた人たち https://chat.openai.com/c/b0ec2c7b-6cb2-454c-93dc-19cc1b7fbed5

# エクスポート先のパスをスクリプトが存在するディレクトリに設定
$exportPath = $PSScriptRoot
# 現在の日付を取得
$currentDate = Get-Date -Format "yyyyMMdd"
# エクスポートするレジストリキーのリスト
$registryKeys = @(
"HKEY_CLASSES_ROOT",
"HKEY_CURRENT_USER",
"HKEY_LOCAL_MACHINE",
"HKEY_USERS",
"HKEY_CURRENT_CONFIG"
)
foreach ($key in $registryKeys) {
# ファイル名を生成(キー名に日付を追加)
$fileName = "${key}_$currentDate.txt"
$filePath = Join-Path -Path $exportPath -ChildPath $fileName
# regeditコマンドを使用してレジストリキーをエクスポート
reg export $key $filePath /y
}
$scriptDirectory = $PSScriptRoot
$dllNames = @("KBDJPN.DLL", "KBD106N.DLL", "KBD106.DLL")
$filePattern = "*.txt"
$textFiles = Get-ChildItem -Path $scriptDirectory -Filter $filePattern
$totalFiles = $textFiles.Count
$fileCounter = 0
# 処理開始時刻を記録
$startTime = Get-Date
foreach ($file in $textFiles) {
$fileCounter++
$fileSize = (Get-Item $file.FullName).length
$readBytes = 0
$lineCount = 0
$currentRegistryKey = "" # 現在のレジストリキーを追跡する変数
$matchCount = 0 # マッチしたエントリの数を追跡する変数
try {
$reader = [System.IO.StreamReader]::new($file.FullName)
while (!$reader.EndOfStream) {
$line = $reader.ReadLine()
$lineCount++
$readBytes += [System.Text.Encoding]::UTF8.GetByteCount($line + [Environment]::NewLine)
if ($lineCount % 1000 -eq 0) {
$percentComplete = ($readBytes / $fileSize) * 100
$formattedReadBytes = "{0:N0}" -f $readBytes
$formattedFileSize = "{0:N0}" -f $fileSize
$elapsed = (Get-Date) - $startTime
$elapsedString = "{0:hh\:mm\:ss}" -f $elapsed
$estimatedTotalTime = $elapsed.TotalSeconds / ($percentComplete / 100)
$remainingTime = $estimatedTotalTime - $elapsed.TotalSeconds
$remainingTimeString = [TimeSpan]::FromSeconds($remainingTime).ToString("hh\:mm\:ss")
Write-Progress -Activity "Reading file: $($file.Name)" -Status "Read $lineCount lines ($formattedReadBytes bytes of $formattedFileSize bytes) [Elapsed: $elapsedString] [Remaining: $remainingTimeString]" -PercentComplete $percentComplete
}
if ($line.StartsWith('[')) {
$currentRegistryKey = $line
}
foreach ($dllName in $dllNames) {
if ($line.ToLower().Contains($dllName.ToLower())) {
$matchCount++ # マッチ数をインクリメント
$matchResult = "${matchCount}: Found '$dllName' in key: $currentRegistryKey in $($file.Name) at line $lineCount"
Write-Host $matchResult
}
}
}
# ファイル処理完了時にマッチが0の場合のメッセージを出力
if ($matchCount -eq 0) {
Write-Host "No matches found in $($file.Name)."
}
$reader.Close()
} catch {
Write-Host "An error occurred: $_"
}
}
Write-Progress -Activity "File Processing" -Completed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment