使用 bcdedit 进行脚本编写

6
使用ImageX和WIM重建HDD后,BCD有时会损坏。因此,我需要从在命令提示符下运行的脚本中无人值守地重新构建BCD。
以下代码可以手动完成任务。我需要帮助将其自动化(请参见下面的代码示例):
bootrec.exe /fixmbr
bootsect.exe /nt60 all /force
attrib -h -s C:\boot\BCD
del C:\boot\BCD
bcdedit.exe /createstore c:\boot\bcd.temp
bcdedit.exe /store c:\boot\bcd.temp /create {bootmgr} /d "Windows Boot Manager"
bcdedit.exe /import c:\boot\bcd.temp
bcdedit.exe /set {bootmgr} device partition=C:
bcdedit.exe /timeout 10
attrib -h -s C:\boot\bcd.temp
del c:\boot\bcd.temp
bcdedit.exe /create /d "Microsoft Windows" /application osloader
bcdedit.exe /set {GUID} device partition=C:
bcdedit.exe /set {GUID} osdevice partition=C:
bcdedit.exe /set {GUID} path \Windows\system32\winload.exe
bcdedit.exe /set {GUID} systemroot \Windows
bcdedit.exe /displayorder {GUID}

如上所述,我需要在不需人工干预的命令提示符中运行此程序。 第六个最后一个语句 "bcdedit.exe /create /d "Microsoft Windows" /application osloader" 的输出是新创建的 GUID。以下命令需要使用此 ID。
如何将 bcdedit 中的新 GUID 加载到变量中,以便我可以在下面的代码中调用它?
最好的问候 Henrik V. Nielsen

1
你好。我忘记提到操作系统是Win7嵌入式32位。我正在从WinPE USB磁盘运行脚本。 - Henrik Nielsen
5个回答

5
如果其他人遇到相同的问题,我通过添加以下行来解决它。
For /F "tokens=2 delims={}" %%i in ('bcdedit.exe') do (set _NEWGUID=%%i)

这个方法有效是因为文件中只有一个全局唯一标识符(GUID)。


0

这是一个基于 Henrik 代码的解决方案

它将从BCD创建的GUID存储到文本文件中,然后通过for循环从文件获取GUID。

bootrec.exe /fixmbr
bootsect.exe /nt60 all /force
attrib -h -s C:\boot\BCD
del C:\boot\BCD
bcdedit.exe /createstore c:\boot\bcd.temp
bcdedit.exe /store c:\boot\bcd.temp /create {bootmgr} /d "Windows Boot Manager"
bcdedit.exe /import c:\boot\bcd.temp
bcdedit.exe /set {bootmgr} device partition=C:
bcdedit.exe /timeout 10
attrib -h -s C:\boot\bcd.temp
del c:\boot\bcd.temp
bcdedit.exe /create /d "Microsoft Windows" /application osloader>GUID.txt
For /F "tokens=2 delims={}" %%i in (GUID.txt) do (set _NEWGUID=%%i)
bcdedit.exe /set %_NEWGUID% device partition=C:
bcdedit.exe /set %_NEWGUID% osdevice partition=C:
bcdedit.exe /set %_NEWGUID% path \Windows\system32\winload.exe
bcdedit.exe /set %_NEWGUID% systemroot \Windows
bcdedit.exe /displayorder %_NEWGUID%

0

修复BCD有更简单的方法。

bcdboot c:\windows 

例如,替换问题中的所有bcdedit命令。

请参阅使用bcdboot修复BCD的说明。

实用程序bcdboot和bootsect可以解决所有引导问题(有关初始引导顺序)。

sfc.exe可以修复损坏的系统文件。


0
Dylan Grasha 你的回答有一些错误,我做了一些改进以使其更完整。
@Echo Off
bootrec.exe /fixmbr
bootsect.exe /nt60 C: /force
attrib -h -s C:\boot\BCD
del C:\boot\BCD
attrib -h -s C:\boot\bcd.temp >nul
del C:\boot\bcd.temp >nul
bcdedit /createstore c:\boot\bcd.temp
bcdedit.exe /store c:\boot\bcd.temp /create {bootmgr} /d "Windows Boot Manager"
bcdedit.exe /import c:\boot\bcd.temp
bcdedit.exe /set {bootmgr} device partition=C:
bcdedit.exe /timeout 10
attrib -h -s C:\boot\bcd.temp
del c:\boot\bcd.temp
bcdedit.exe /create /d "Microsoft Windows" /application osloader>GUID.txt
For /F "tokens=2 delims={}" %%i in (GUID.txt) do (set _NEWGUID=%%i)
bcdedit.exe /set {%_NEWGUID%} device partition=C:
bcdedit.exe /set {%_NEWGUID%} osdevice partition=C:
bcdedit.exe /set {%_NEWGUID%} path \Windows\system32\winload.exe
bcdedit.exe /set {%_NEWGUID%} systemroot \Windows
bcdedit.exe /displayorder {%_NEWGUID%}
del guid.txt
cmd

0

有一个更简单的方法。

创建新条目时,BCD接受所有GUID格式为 aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee(数字数量为8-4-4-4-12)的值。

这意味着您可以定义GUID而无需使用For-Loop搜索GUID。

这对我起作用。


你好,Netlord先生,听起来很有趣。下次我需要更新脚本时会尝试实现它。 - Henrik Nielsen
你如何指定自己的GUID?这就是我来这里寻找的。 - iamacarpet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接