support del reg

This commit is contained in:
Julian Freeman
2026-04-04 15:54:40 -04:00
parent dac6f6cd62
commit c230847cc0
2 changed files with 17 additions and 7 deletions

View File

@@ -38,7 +38,10 @@ foreach ($line in $lines) {
# 匹配 [HKEY_...] 路径
if ($line.StartsWith("[") -and $line.EndsWith("]")) {
$fullPath = $line.Substring(1, $line.Length - 2)
# 检查是否是删除整个 Key 的语法:[-HKEY_...]
$isDeleteKey = $line.StartsWith("[-")
$fullPath = if ($isDeleteKey) { $line.Substring(2, $line.Length - 3) } else { $line.Substring(1, $line.Length - 2) }
$root = ""
$basePath = ""
@@ -51,11 +54,12 @@ foreach ($line in $lines) {
}
if ($root -ne "") {
# 使用 [ordered] 确保 JSON 键位顺序
$currentBatch = [ordered]@{
type = "registry_batch"
root = $root
base_path = $basePath
# 如果是删除整个 Key我们可以在这里记录或者扩展 schema
# 但目前我们先处理 Value 删除
values = [ordered]@{}
}
$results += $currentBatch
@@ -67,10 +71,14 @@ foreach ($line in $lines) {
if ($line -match '^"(.+)"\s*=\s*(.+)$') {
$name = $Matches[1]
$rawVal = $Matches[2]
$vType = "String"
$vType = ""
$data = $null
if ($rawVal.StartsWith("dword:")) {
if ($rawVal -eq "-") {
# 处理删除 Value 的逻辑: "Key"=-
$vType = "Delete"
$data = $null
} elseif ($rawVal.StartsWith("dword:")) {
$vType = "Dword"
$hex = $rawVal.Substring(6)
$data = [Convert]::ToInt32($hex, 16)
@@ -91,7 +99,7 @@ foreach ($line in $lines) {
}
}
if ($null -ne $currentBatch -and $null -ne $data) {
if ($null -ne $currentBatch -and $vType -ne "") {
$currentBatch.values[$name] = [ordered]@{
v_type = $vType
data = $data
@@ -104,7 +112,6 @@ if ($results.Count -eq 0) {
Write-Warning "未在文件中识别到有效的注册表项。"
}
# 转换为格式化的 JSON
$jsonOutput = ConvertTo-Json $results -Depth 10
[System.IO.File]::WriteAllText($OutputPath, $jsonOutput, [System.Text.Encoding]::UTF8)