[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[技术讨论] [出题挑战]PowerShell比较注册表 可以使用Ai 代码少效率高胜出

PowerShell 脚本,用于比较两个注册表文件 A.reg 和 B.reg,
并将相同部分保存为 AB相同.reg,
A.reg 的独有部分保存为 A独有.reg,
B.reg 的独有部分保存为 B独有.reg,
A.reg 的差异部分保存为 A差异.reg,
B.reg 的差异部分保存为 B差异.reg:

例如(实际情况比这多的多)
以下为A.reg
  1. Windows Registry Editor Version 5.00
  2. [HKEY_LOCAL_MACHINE\PE_SYSTEM]
  3. [HKEY_LOCAL_MACHINE\PE_SYSTEM\ControlSet001]
  4. [HKEY_LOCAL_MACHINE\PE_SYSTEM\ControlSet001\CI]
  5. "UMCIDisabled"=dword:00000000
  6. [HKEY_LOCAL_MACHINE\PE_SYSTEM\ControlSet001\Control]
  7. "BootDriverFlags"=dword:0000001c
  8. "CurrentUser"="USERNAME"
  9. "EarlyStartServices"=hex(7):52,00,70,00,63,00,53,00,73,00,00,00,50,00,6f,00,77,\
  10.   00,65,00,72,00,00,00,42,00,72,00,6f,00,6b,00,65,00,72,00,49,00,6e,00,66,00,\
  11.   72,00,61,00,73,00,74,00,72,00,75,00,63,00,74,00,75,00,72,00,65,00,00,00,53,\
  12.   00,79,00,73,00,74,00,65,00,6d,00,45,00,76,00,65,00,6e,00,74,00,73,00,42,00,\
  13.   72,00,6f,00,6b,00,65,00,72,00,00,00,44,00,63,00,6f,00,6d,00,4c,00,61,00,75,\
  14.   00,6e,00,63,00,68,00,00,00,52,00,70,00,63,00,45,00,70,00,4d,00,61,00,70,00,\
  15.   70,00,65,00,72,00,00,00,4c,00,53,00,4d,00,00,00,41,00,70,00,70,00,49,00,64,\
  16.   00,53,00,76,00,63,00,00,00,00,00
  17. "PreshutdownOrder"=hex(7):44,00,65,00,76,00,69,00,63,00,65,00,49,00,6e,00,73,\
  18.   00,74,00,61,00,6c,00,6c,00,00,00,55,00,73,00,6f,00,53,00,76,00,63,00,00,00,\
  19.   77,00,75,00,61,00,75,00,73,00,65,00,72,00,76,00,00,00,64,00,6f,00,73,00,76,\
  20.   00,63,00,00,00,67,00,70,00,73,00,76,00,63,00,00,00,74,00,72,00,75,00,73,00,\
  21.   74,00,65,00,64,00,69,00,6e,00,73,00,74,00,61,00,6c,00,6c,00,65,00,72,00,00,\
  22.   00,00,00
  23. "SystemBootDriveLetter"=dword:00000058
  24. "WaitToKillServiceTimeout"="5000"
  25. [HKEY_LOCAL_MACHINE\PE_SYSTEM\ControlSet001\Control\ACPI]
  26. "AMLIGlobalHeapSize"=dword:00100000
复制代码
以下为B.reg
  1. Windows Registry Editor Version 5.00
  2. [HKEY_LOCAL_MACHINE\PE_SYSTEM]
  3. [HKEY_LOCAL_MACHINE\PE_SYSTEM\ControlSet001]
  4. [HKEY_LOCAL_MACHINE\PE_SYSTEM\ControlSet001\CI]
  5. "UMCIDisabled"=dword:00000000
  6. [HKEY_LOCAL_MACHINE\PE_SYSTEM\ControlSet001\Control]
  7. "BootDriverFlags"=dword:0000001A
  8. "CurrentUser"="USERNAME"
  9. "WaitToKillService"="6000"
复制代码
比较后得到AB相同.reg
  1. Windows Registry Editor Version 5.00
  2. [HKEY_LOCAL_MACHINE\PE_SYSTEM]
  3. [HKEY_LOCAL_MACHINE\PE_SYSTEM\ControlSet001]
  4. [HKEY_LOCAL_MACHINE\PE_SYSTEM\ControlSet001\CI]
  5. "UMCIDisabled"=dword:00000000
  6. [HKEY_LOCAL_MACHINE\PE_SYSTEM\ControlSet001\Control]
  7. "CurrentUser"="USERNAME"
复制代码
A独有.reg
  1. Windows Registry Editor Version 5.00
  2. [HKEY_LOCAL_MACHINE\PE_SYSTEM\ControlSet001\Control]
  3. "EarlyStartServices"=hex(7):52,00,70,00,63,00,53,00,73,00,00,00,50,00,6f,00,77,\
  4.   00,65,00,72,00,00,00,42,00,72,00,6f,00,6b,00,65,00,72,00,49,00,6e,00,66,00,\
  5.   72,00,61,00,73,00,74,00,72,00,75,00,63,00,74,00,75,00,72,00,65,00,00,00,53,\
  6.   00,79,00,73,00,74,00,65,00,6d,00,45,00,76,00,65,00,6e,00,74,00,73,00,42,00,\
  7.   72,00,6f,00,6b,00,65,00,72,00,00,00,44,00,63,00,6f,00,6d,00,4c,00,61,00,75,\
  8.   00,6e,00,63,00,68,00,00,00,52,00,70,00,63,00,45,00,70,00,4d,00,61,00,70,00,\
  9.   70,00,65,00,72,00,00,00,4c,00,53,00,4d,00,00,00,41,00,70,00,70,00,49,00,64,\
  10.   00,53,00,76,00,63,00,00,00,00,00
  11. "PreshutdownOrder"=hex(7):44,00,65,00,76,00,69,00,63,00,65,00,49,00,6e,00,73,\
  12.   00,74,00,61,00,6c,00,6c,00,00,00,55,00,73,00,6f,00,53,00,76,00,63,00,00,00,\
  13.   77,00,75,00,61,00,75,00,73,00,65,00,72,00,76,00,00,00,64,00,6f,00,73,00,76,\
  14.   00,63,00,00,00,67,00,70,00,73,00,76,00,63,00,00,00,74,00,72,00,75,00,73,00,\
  15.   74,00,65,00,64,00,69,00,6e,00,73,00,74,00,61,00,6c,00,6c,00,65,00,72,00,00,\
  16.   00,00,00
  17. "SystemBootDriveLetter"=dword:00000058
  18. "WaitToKillServiceTimeout"="5000"
  19. [HKEY_LOCAL_MACHINE\PE_SYSTEM\ControlSet001\Control\ACPI]
  20. "AMLIGlobalHeapSize"=dword:00100000
复制代码
B独有.reg
  1. Windows Registry Editor Version 5.00
  2. [HKEY_LOCAL_MACHINE\PE_SYSTEM\ControlSet001\Control]
  3. "WaitToKillService"="6000"
复制代码
A差异.reg
  1. Windows Registry Editor Version 5.00
  2. [HKEY_LOCAL_MACHINE\PE_SYSTEM\ControlSet001\Control]
  3. "BootDriverFlags"=dword:0000001c
复制代码
B差异.reg
  1. Windows Registry Editor Version 5.00
  2. [HKEY_LOCAL_MACHINE\PE_SYSTEM\ControlSet001\Control]
  3. "BootDriverFlags"=dword:0000001A
复制代码

  1. $ErrorActionPreference = 'Stop'
  2. function Parse-RegFile {
  3.     param([string]$filePath)
  4.     $content = Get-Content $filePath
  5.     $regData = @{
  6.         Keys = [System.Collections.Generic.List[string]]::new()
  7.         KeyValues = @{}
  8.         KeyExistence = @{}
  9.     }
  10.     $currentKey = $null
  11.     $buffer = $null
  12.     foreach ($line in $content) {
  13.         $trimmed = $line.Trim()
  14.         if ($trimmed -eq '' -or $trimmed.StartsWith(';') -or $trimmed.StartsWith('Windows Registry Editor')) {
  15.             continue
  16.         }
  17.         if ($trimmed.StartsWith('[') -and $trimmed.EndsWith(']')) {
  18.             $currentKey = $trimmed
  19.             if (-not $regData.KeyExistence.ContainsKey($currentKey)) {
  20.                 $regData.Keys.Add($currentKey)
  21.                 $regData.KeyExistence[$currentKey] = $true
  22.                 $regData.KeyValues[$currentKey] = @{}
  23.             }
  24.             $buffer = $null
  25.         }
  26.         elseif ($currentKey -ne $null) {
  27.             if ($line -match '\\\s*$') {
  28.                 $buffer = if ($buffer) { $buffer + $line.TrimEnd('\') } else { $line.TrimEnd('\') }
  29.             }
  30.             else {
  31.                 $fullLine = if ($buffer) { $buffer + $line } else { $line }
  32.                 $buffer = $null
  33.                 if ($fullLine -match '^"([^"]+)"\s*=') {
  34.                     $name = $matches[1]
  35.                     $regData.KeyValues[$currentKey][$name] = $fullLine
  36.                 }
  37.             }
  38.         }
  39.     }
  40.     return $regData
  41. }
  42. function Write-RegFile {
  43.     param([string]$path, [array]$entries)
  44.     $header = "Windows Registry Editor Version 5.00`r`n"
  45.     [System.IO.File]::WriteAllText($path, $header + ($entries -join "`r`n"), [System.Text.Encoding]::Unicode)
  46. }
  47. $regA = Parse-RegFile 'A.reg'
  48. $regB = Parse-RegFile 'B.reg'
  49. # 处理键路径比较
  50. $commonKeys = $regA.Keys | Where-Object { $_ -in $regB.Keys }
  51. $aOnlyKeys = $regA.Keys | Where-Object { $_ -notin $regB.Keys }
  52. $bOnlyKeys = $regB.Keys | Where-Object { $_ -notin $regA.Keys }
  53. # 初始化结果集合
  54. $abCommon = @()
  55. $aUnique = @()
  56. $bUnique = @()
  57. $aDiff = @()
  58. $bDiff = @()
  59. # 处理共同键路径
  60. foreach ($key in $commonKeys) {
  61.     $aValues = $regA.KeyValues[$key]
  62.     $bValues = $regB.KeyValues[$key]
  63.    
  64.     # 收集相同键值
  65.     $commonValues = @()
  66.     foreach ($name in $aValues.Keys) {
  67.         if ($bValues.ContainsKey($name) -and $aValues[$name] -eq $bValues[$name]) {
  68.             $commonValues += $aValues[$name]
  69.         }
  70.     }
  71.    
  72.     # 添加到AB相同
  73.     $abCommon += "$key"
  74.     $abCommon += $commonValues
  75.     $abCommon += ''
  76.     # 处理差异
  77.     foreach ($name in $aValues.Keys) {
  78.         if ($bValues.ContainsKey($name) -and $aValues[$name] -ne $bValues[$name]) {
  79.             $aDiff += "$key"
  80.             $aDiff += $aValues[$name]
  81.             $aDiff += ''
  82.             
  83.             $bDiff += "$key"
  84.             $bDiff += $bValues[$name]
  85.             $bDiff += ''
  86.         }
  87.     }
  88. }
  89. # 处理A独有
  90. foreach ($key in $aOnlyKeys) {
  91.     $aUnique += "$key"
  92.     $aUnique += $regA.KeyValues[$key].Values
  93.     $aUnique += ''
  94. }
  95. foreach ($key in $commonKeys) {
  96.     $aValues = $regA.KeyValues[$key]
  97.     $bValues = $regB.KeyValues[$key]
  98.    
  99.     foreach ($name in $aValues.Keys) {
  100.         if (-not $bValues.ContainsKey($name)) {
  101.             $aUnique += "$key"
  102.             $aUnique += $aValues[$name]
  103.             $aUnique += ''
  104.         }
  105.     }
  106. }
  107. # 处理B独有
  108. foreach ($key in $bOnlyKeys) {
  109.     $bUnique += "$key"
  110.     $bUnique += $regB.KeyValues[$key].Values
  111.     $bUnique += ''
  112. }
  113. foreach ($key in $commonKeys) {
  114.     $aValues = $regA.KeyValues[$key]
  115.     $bValues = $regB.KeyValues[$key]
  116.    
  117.     foreach ($name in $bValues.Keys) {
  118.         if (-not $aValues.ContainsKey($name)) {
  119.             $bUnique += "$key"
  120.             $bUnique += $bValues[$name]
  121.             $bUnique += ''
  122.         }
  123.     }
  124. }
  125. # 写入文件
  126. Write-RegFile 'AB相同.reg' $abCommon
  127. Write-RegFile 'A独有.reg' $aUnique
  128. Write-RegFile 'B独有.reg' $bUnique
  129. Write-RegFile 'A差异.reg' $aDiff
  130. Write-RegFile 'B差异.reg' $bDiff
  131. Write-Host "比较完成,结果文件已生成!"
  132. pause
复制代码

TOP

只有一个例子 , 太片面了
  1. "参考 http://www.bathome.net/thread-70496-1-1.html 这样不就行了"
复制代码

TOP

回复 3# Five66 从电脑注册表导出一部分为A.reg 复制一份对其部分修改为B.reg ,用以测试

TOP

回复 1# smss

    如果两个文件的比对粒度是 ‘文本行’,可用 findstr 凑个趣...
  1. @echo off
  2. findstr /ilxg:"b.reg" a.reg>ab_same.reg
  3. findstr /vilxg:"ab_same.reg" a.reg>a_only.reg
  4. findstr /vilxg:"ab_same.reg" b.reg>b_only.reg
  5. pause&exit/b
复制代码

TOP

返回列表