如何从 .reg 备份创建注册表配置单元文件

如何从 .reg 备份创建注册表配置单元文件

长话短说,我无意中搞砸了我的HKLM\SYSTEM 注册表目录尝试修复使用 Windows 安全补丁更改的 WinApps 权限。

截至目前,我的系统完全无法启动,并出现 BSOD 消息“无法访问的启动设备“由我的更改引起的。我尝试过

  • 更改注册表项的值以启用 AHCI
  • 安全模式
  • sfc /scannow + chkdsk
  • 检查 DISM 中的待处理包
  • 将文件从 Regback 移动到 /config
  • 将我的 SYSTEM.reg 工作备份导入到 Windows 恢复命令提示符和 WinPE 下的注册表中

    其中一个通常会起作用,但我的问题是由垃圾 SYSTEM 注册表引起的。

我需要创建一个系统蜂巢来自我的文件.注册备份HKLM\系统目录。

我认为这是一个非常简单的解决方案,但是我在这个主题上唯一能找到的就是几年前的一个随机 MSDN 帖子,它似乎可以实现我想要的,但我无法让脚本运行。(https://blogs.msdn.microsoft.com/sergey_babkins_blog/2014/11/10/how-to-create-a-brand-new-registry-hive/

  • 尝试以 .bat 形式运行他的脚本会返回一条错误,提示:function' is not recognized as an internal or external command, operable program or batch file.
  • 尝试在 powershell 中运行 .bat 返回:merge.bat : The term 'merge.bat' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

如果有人知道如何让上述 powershell 脚本运行,请告诉我。

答案1

您链接的脚本是 PowerShell 脚本,需要以 .ps1 扩展名保存并在 PowerShell 中执行。

您可以尝试将其保存为 .ps1 文件并运行它,这能解决您的问题吗?

编辑:

.ps1 文件的内容应为:

function ConvertTo-RegistryHive
{
<#
.SYNOPSIS
Convert a registry-exported  text (contents of a .reg file) to a binary registry hive file.

.EXAMPLE
PS> ConvertTo-RegistryHive -Text (Get-Content my.reg) -Hive my.hive
#>
    param(
        ## The contents of registry exported (.reg) file to convert into the hive.
        [string[]] $Text,
        ## The hive file name to write the result to.
        [parameter(Mandatory=$true)]
        [string] $Hive
    )

    $basefile = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName())
    $regfile = $basefile + ".reg"
    $inifile = $basefile + ".ini"
    $subkey = [System.Guid]::NewGuid().ToString()

    &{
        foreach ($chunk in $Text) {
            foreach ($line in ($chunk -split "`r")) {
                $line -replace "^\[\w*\\\w*","[HKEY_LOCAL_MACHINE\$subkey"
            }
        }
    } | Set-Content $regfile

    # Since bcdedit stores its data in the same hives as registry,
    # this is the way to create an almost-empty hive file.
    bcdedit /createstore $Hive
    if (!$?) { throw "failed to create the new hive '$Hive'" }

    reg load "HKLM\$subkey" $Hive
    if (!$?) { throw "failed to load the hive '$Hive' as 'HKLM\$subkey'" }

    try {
        # bcdedit creates some default entries that need to be deleted,
        # but first the permissions on them need to be changed to allow deletion
@"
HKEY_LOCAL_MACHINE\$subkey\Description [1]
HKEY_LOCAL_MACHINE\$subkey\Objects [1]
"@ | Set-Content $inifile
        regini $inifile
        if (!$?) { throw "failed to change permissions on keys in 'HKLM\$subkey'" }
        Remove-Item -LiteralPath "hklm:\$subkey\Description" -Force -Recurse
        Remove-Item -LiteralPath "hklm:\$subkey\Objects" -Force -Recurse

        # now import the file contents
        reg import $regfile
        if (!$?) { throw "failed to import the data from '$regfile'" }
    } finally {
        reg unload "HKLM\$subkey"
        Remove-Item -LiteralPath $inifile -Force
    }

    Remove-Item -LiteralPath $regfile -Force
}

ConvertTo-RegistryHive -Text (Get-Content C:\MyHive.reg) -Hive HiveName

然后只需将其更改C:\MyHive.reg为指向您的 .reg 文件和HiveName要创建的 Hive 的名称。

相关内容