vbscriptwmibcdstore

Unable to set DeviceElements for a Windows BCD with WMI


I am trying to programatically create a Windows 7 BCD Store using VBScript. Building the store itself, the BootMgr, and the OS Loader seems to go swimmingly as does populating most of the elements for each of the objects. Where I run into trouble is in populating the device and osdevice elements. Here is the code that works:

Const BootMgrId = "{9dea862c-5cdd-4e70-acc1-f32b344d4795}"
BootLdrId = Left(CreateObject("Scriptlet.TypeLib").Guid,38)
Set objStoreClass = GetObject("winmgmts:{(Backup,Restore)}\\.\root\wmi:BcdStore")
objStoreClass.CreateStore "m:\boot\BCD", objStore
objStore.CreateObject BootMgrId, &h10200002, objBootMgr
objStore.CreateObject BootLdrId, &h10200003, objBootLdr
objBootLdr.SetStringElement &h12000002, "\windows\system32\boot\winload.exe"
objBootLdr.SetStringElement &h12000004, "DiskWipe"
objBootLdr.SetStringElement &h22000002, "\windows"
objBootLdr.SetBooleanElement &h26000022, True

The code with which I am having trouble is:

objBootMgr.SetPartitionDeviceElement &h11000001, 2, "", "m:"
objBootLdr.SetPartitionDeviceElement &h11000001, 2, "", "m:"
objBootLdr.SetPartitionDeviceElement &h21000001, 2, "", "m:"

I have tried a couple of variations on the partition path at the end of the statement, to include the partition's DeviceId as reported from win32_Volume, the old boot.ini-style ARC path, "partition=", and other syntax that seems to work in bcdedit.

A couple of notes. The ultimate goal is to use another partition on the same physical disk. I'm not convinced that SetPartitionDeviceElement is the right method to be using (it seems to be exclusively for the benefit of vhds). If, instead, I use:

objBootMgr.SetDeviceElement &h11000001, 1, ""
objBootLdr.SetDeviceElement &h11000001, 1, ""    
objBootLdr.SetDeviceElement &h21000001, 1, ""

the device and osdevice elements seem to populate properly and reflect the current boot partition, but when I change the active partition to the partition this new BCD is on and reboot, while thankfully error-free, it does not boot. Not surprising, since this method doesn't allow for the specification of a particular partition. I can make it work using bcdedit with

bcdedit -store m:\boot\bcd -set {bootmgr} device partition=m:

and

bcdedit -store m:\boot\bcd -set %guid% osdevice partition=m:
bcdedit -store m:\boot\bcd -set %guid% device partition=m:

where %guid% is the GUID of the OS Loader.

Useful info on this is pretty thin on the ground. I have been neck-deep in the MSDN pages on the BCD WMI Provider, and an old MS word doc on BCD. I have also found the Visual BCD editor to be of some use in sorting out object and element types, but I'm really stuck on this one. Anyone?


Solution

  • OK. I figured this one out. The options are as follows:

    objBootMgr.SetPartitionDeviceElement &h11000001, 2, "", "\Device\Harddisk0\Partition3"
    

    for disk/partition dependent syntax, or

    objBootMgr.SetPartitionDeviceElement &h11000001, 2, "", "\Device\HarddiskVolume3"
    

    for disk-independent volume syntax.