使用bcdedit编写脚本



使用ImageX和WIM重建HDD后,BCD有时会损坏。因此,我需要在命令提示符下从无人参与运行的脚本重新拒绝BCD。

当手动输入时,以下代码将完成此工作。我需要帮助来实现它的自动化(请参阅下面的代码示例):

bootrec.exe /fixmbr
bootsect.exe /nt60 all /force
attrib -h -s C:bootBCD
del C:bootBCD
bcdedit.exe /createstore c:bootbcd.temp
bcdedit.exe /store c:bootbcd.temp /create {bootmgr} /d "Windows Boot Manager"
bcdedit.exe /import c:bootbcd.temp
bcdedit.exe /set {bootmgr} device partition=C:
bcdedit.exe /timeout 10
attrib -h -s C:bootbcd.temp
del c:bootbcd.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 Windowssystem32winload.exe
bcdedit.exe /set {GUID} systemroot Windows
bcdedit.exe /displayorder {GUID}

如上所述,我需要在无人参与的命令提示符下运行此程序。最后第6条语句"bcdedit.exe/create/d"Microsoft Windows"/application osloader"的输出是新创建的GUID。以下命令中需要此ID。

如何将此新GUID从bcdedit加载到可以在以下代码中调用的变量?

致以最诚挚的问候Henrik V.Nielsen

如果其他人也面临同样的问题,我通过添加以下行来解决。

For /F "tokens=2 delims={}" %%i in ('bcdedit.exe') do (set _NEWGUID=%%i)

这是因为文件中只有一个GUID。

有一种更简单的方法。

创建新条目时,BCD接受aaaaaaa-bbb-cccc-dddd-eeeeeeeeee形式的所有GUID(位数8-4-4-12

这意味着您可以定义GUID,而不必使用For循环搜索GUID。

它对我有效。

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

这将从BCD创建的GUID放入一个文本文件中,for循环从文件获取GUID

bootrec.exe /fixmbr
bootsect.exe /nt60 all /force
attrib -h -s C:bootBCD
del C:bootBCD
bcdedit.exe /createstore c:bootbcd.temp
bcdedit.exe /store c:bootbcd.temp /create {bootmgr} /d "Windows Boot Manager"
bcdedit.exe /import c:bootbcd.temp
bcdedit.exe /set {bootmgr} device partition=C:
bcdedit.exe /timeout 10
attrib -h -s C:bootbcd.temp
del c:bootbcd.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 Windowssystem32winload.exe
bcdedit.exe /set %_NEWGUID% systemroot Windows
bcdedit.exe /displayorder %_NEWGUID%
迪伦·格拉沙你的答案有一些错误,我添加了一些增强功能,使其更加完整。
@Echo Off
bootrec.exe /fixmbr
bootsect.exe /nt60 C: /force
attrib -h -s C:bootBCD
del C:bootBCD
attrib -h -s C:bootbcd.temp >nul
del C:bootbcd.temp >nul
bcdedit /createstore c:bootbcd.temp
bcdedit.exe /store c:bootbcd.temp /create {bootmgr} /d "Windows Boot Manager"
bcdedit.exe /import c:bootbcd.temp
bcdedit.exe /set {bootmgr} device partition=C:
bcdedit.exe /timeout 10
attrib -h -s C:bootbcd.temp
del c:bootbcd.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 Windowssystem32winload.exe
bcdedit.exe /set {%_NEWGUID%} systemroot Windows
bcdedit.exe /displayorder {%_NEWGUID%}
del guid.txt
cmd

有一种更简单的方法可以修复BCD。

bcdboot c:windows 

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

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

实用程序bcdboot和bootsect可以修复所有启动问题(关于初始启动序列)。

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

最新更新