Scripting with bcdedit
Asked Answered
W

5

6

After rebuilding a HDD using ImageX and a WIM, the BCD sometimes gets corrupted. I therefor need to rebuid the BCD from a script running unattended in a command prompt.

The below code does the job, when entered manually. I need help to automate it (see further below code example):

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}

As started above, I need to run this in a unattended command prompt. The output from the 6th last statement "bcdedit.exe /create /d "Microsoft Windows" /application osloader" is a newly creates GUID. This ID is needed in the following commands.

How do I load this new GUID from bcdedit to a variable I can call in the following code?

Best Regards Henrik V. Nielsen

Wilhelm answered 9/5, 2016 at 12:48 Comment(1)
Hello. I forgot to mentions that the OS is Win7 Embedded 32bit. I am running the script from a WinPE USB disk.Wilhelm
W
5

If other should face the same problem, I solved it by adding the following line.

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

This works because there is only one GUID in the file.

Wilhelm answered 12/5, 2016 at 10:29 Comment(0)
L
0

there is an easier way.

When creating a new entry BCD accepts all GUIDs in the Form aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee (number of digits 8-4-4-4-12)

This means you can define an GUID an don´t have to search the GUID with the For-Loop.

It´s working for me.

Limner answered 9/5, 2016 at 12:48 Comment(2)
Hello mr netlord, Sounds interesting. I will try to implement next time I need to update the script.Wilhelm
How do you specify your own GUID? That's what I came here looking for.Oquendo
K
0

This is a solution based on Henrik's code

This takes the GUID created from BCD into a text file and the for loop gets the GUID from the file

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%
Kirst answered 8/12, 2016 at 21:41 Comment(0)
W
0

Dylan Grasha Your answer has some errors and I have added some enhancements to make it more complete.

@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
Wrinkle answered 22/2, 2017 at 20:26 Comment(0)
F
0

There is an easier way for fixing BCD.

bcdboot c:\windows 

for example replaces all bcdedit commands from question.

See explanations for using bcdboot to fix BCD.

The utilities bcdboot and bootsect can fix all boot problems (regarding initial boot sequence).

sfc.exe can fix corrupted system files.

Featherstitch answered 31/8, 2017 at 6:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.