Click here to Skip to main content
15,878,814 members
Articles / Programming Languages / VBScript

Using VBScript and WMI to Access Vista's BcdStore

Rate me:
Please Sign up or sign in to vote.
3.10/5 (4 votes)
1 Apr 2007CPOL1 min read 51.3K   747   17  
An example showing how to use VBScript and WMI to access Vista's BcdStore.
'**********************************************************************
' Mimic_bcdedit.vbs
'----------------------------------------------------------------------
'    Purpose: To mimic the output of the "bcdedit" command.  This
'             script illustrates how to obtain basic BCD information
'             from WMI.  Using the information in this script as a
'             starting point, it is relatively easy to add additional 
'             functionality to modify the BcdStore settings.
'
'      Parms: None.
'
'   Requires: To be run on Vista or Longhorn to access BcdStore using
'             WMI.

'  Copyright: None.
'
' References: 1) Vista Software Development Kit.
'             2) MSDN Library, "Boot Configuration Data (BCD)" at
'                http://msdn2.microsoft.com/en-us/library/aa362692.aspx 
'             3) "Boot Configuration Data in Windows Vista" at
'                http://download.microsoft.com/download/a/f/7/af7777e5-7dcd-4800-8a0a-b18336565f5b/BCD.doc
'
'     Note 1: This function should be invoked as a cscript.
'             Issue the command "cscript //h:cscript" to set the
'             default scripting as cscript before issuing this
'             command.
'
'     Note 2: One significant difference between this script and the
'             "bdedit" command is the output in the "device" lines.
'             In "bcdedit" this lists the logical volume (e.g. 
'             partition=C:).  In the script only the low-level par-
'             tition identifer can be provided (e.g.
'             \Device\HarddiskVolume1).  The DDK function
'             ZwQuerySymbolicLinkObject() can return the drive letter
'             given \Device\HarddiskVolumeX, but this function cannot,
'             by default, be called from VBScript.
'
'     Author: Ross Johnston
'**********************************************************************
option explicit
on error resume next

dim colObjects
dim colSettings
dim colTypes
dim i
dim i1
dim i2
dim iMajor
dim iMinor
dim objBCD
dim objBCDStore
dim objElement
dim objLocator
dim objNtldr
dim objOS
dim objRootWMI
dim objShell
dim objWBL
dim objWBM
dim objX
dim colWMIService
dim objWMIService
dim strComputer
dim strPartition
dim strTemp

'----------------------------------------------------------------------
' Determine the operating system version.  This script requires Vista
' or Longhorn to find BCD WMI data.
'----------------------------------------------------------------------
strComputer = "."

strTemp = "winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2"
set objWMIService = GetObject( strTemp )

if Err.number <> 0 then
	WScript.Echo "GetObject(" & strTemp & ") failed rc=" & Hex(Err.number) & " " & Err.Description'
	WScript.Quit(1)
end if

set colWMIService = objWMIService.ExecQuery( "Select * from Win32_OperatingSystem" )

'
' objX.Version will contains something like 6.0.6000
'
for each objX In colWMIService
    i1 = InStr( objX.Version, "." )
    i2 = InStr( i1+1, objX.Version, "." )
    iMajor = Mid( objX.Version, 1, i1-1 )
    iMinor = Mid( objX.Version, i1+1, i2-i1-1 ) 
next

set colWMIService = nothing
set objWMIService = nothing

if iMajor <> 6 then
    WScript.Echo "Operating system is not Vista or Longhorn."
    WScript.Quit( 1 )
end if

'----------------------------------------------------------------------
' Types found in the BcdStore:
'
'      Type        Class                 Name
'   ---------- -------------------- -----------------------------------
'   0x11000001 BcdDeviceData        BcdLibraryDevice_ApplicationDevice
'   0x12000002 BcdStringElement     BcdLibraryString_ApplicationPath
'   0x12000004 BcdStringElement     BcdLibraryString_Description      
'   0x12000005 BcdStringElement     BcdLibraryString_PreferredLocale  
'   0x14000006 BcdObjectListElement BcdLibraryObjectList_InheritedObjects 
'   0x21000001 BcdDeviceData        BcdOSLoaderDevice_OOSDevice 
'   0x22000002 BcdStringElement     BcdOSLoaderString_SystemRoot
'   0x23000003 BcdObjectElement     BcdOSLoaderObject_DefaultObject
'   0x24000001 BcdObjectListElement BcdBootMgrObjectList_DisplayOrder       
'   0x24000010 BcdObjectListElement BcdBootMgrObjectList_ToolsDisplayOrder  
'   0x25000004 BcdIntegerElement    BcdBootMgrInteger_Timeout               
'   0x25000020 BcdIntegerElement    BcdOSLoaderInteger_NxPolicy
'----------------------------------------------------------------------
const BcdLibraryDevice_ApplicationDevice       = &h11000001
const BcdLibraryString_ApplicationPath         = &h12000002
const BcdLibraryString_Description             = &h12000004
const BcdLibraryString_PreferredLocale         = &h12000005
const BcdLibraryObjectList_InheritedObjects    = &h14000006
const BcdOSLoaderDevice_OSDevice               = &h21000001
const BcdOSLoaderString_SystemRoot             = &h22000002
const BcdOSLoaderObject_DefaultObject          = &h23000003
const BcdOSLoaderObject_AssociatedResumeObject = &h23000003
const BcdBootMgrObjectList_DisplayOrder        = &h24000001
const BcdBootMgrObjectList_ToolsDisplayOrder   = &h24000010
const BcdBootMgrInteger_Timeout                = &h25000004
const BcdOSLoaderInteger_NxPolicy              = &h25000020

'----------------------------------------------------------------------
' Open the BcdStore.
'----------------------------------------------------------------------
set objLocator = CreateObject( "WbemScripting.SWbemLocator" )
set objRootWMI = objLocator.ConnectServer( ".", "root\wmi" )
objRootWMI.Security_.ImpersonationLevel = 3

strTemp    = "winmgmts:{impersonationlevel=Impersonate,(Backup,Restore)}!root/wmi:BcdStore"
set objBCD = GetObject( strTemp )

if Err.number <> 0 then
	WScript.Echo "ERROR - GetObject(" & strTemp & ") failed rc=" & Hex(Err.number) & " " & Err.Description'
	WScript.Quit(1)
end if

if not objBCD.OpenStore( "", objBcdStore ) then
    WScript.Echo "ERROR - Could not open the BCD system store."
    WScript.Quit(1)
end if

set objBCD = nothing

'----------------------------------------------------------------------
' Open the Windows Boot Manager.
'----------------------------------------------------------------------
if not objBcdStore.OpenObject( "{9dea862c-5cdd-4e70-acc1-f32b344d4795}", objWBM ) then
    WScript.Echo "ERROR - OpenObject failed for the Windows Boot Manager."
    WScript.Quit(1)
end if

'
' To enumerate the types that are returned for a BCD class use:
'
'    if not objWBM.EnumerateElementTypes( colTypes ) then
'        WScript.Echo "ERROR - objWBM.EnumerateElementTypes failed."
'        WScript.Quit(1)
'    end if
'    for i = 0 to UBound(colTypes)
'        WScript.Echo "        Type=" & Hex(colTypes(i))
'    next    

'----------------------------------------------------------------------
' Windows Boot Manager
'----------------------------------------------------------------------
WScript.Echo ""
WScript.Echo "Windows Boot Manager"
WScript.Echo "--------------------"
WScript.Echo "identifier              " & GetBcdId( objWBM.Id )

if not objWBM.GetElement( BcdLibraryDevice_ApplicationDevice, objElement ) then
    WScript.Echo "ERROR WBM GetElement for " & Hex(BcdLibraryDevice_ApplicationDevice) & " failed."
    WScript.Quit(1)
end if
WScript.Echo "device                  " & objElement.Device.Path

if not objWBM.GetElement( BcdLibraryString_Description, objElement ) then
    WScript.Echo "ERROR WBM GetElement for " & Hex(BcdLibraryString_Description) & " failed."
    WScript.Quit(1)
end if
WScript.Echo "description             " & objElement.String

if not objWBM.GetElement( BcdLibraryString_PreferredLocale, objElement ) then
    WScript.Echo "ERROR WBM GetElement for " & Hex(BcdLibraryString_PreferredLocale) & " failed."
    WScript.Quit(1)
end if
WScript.Echo "locale                  " & objElement.String

if not objWBM.GetElement( BcdLibraryObjectList_InheritedObjects, objElement ) then
    WScript.Echo "ERROR WBM GetElement for " & Hex(BcdLibraryObjectList_InheritedObjects) & " failed."
    WScript.Quit(1)
end if
WScript.Echo "inherit                 " & GetBcdId( objElement.Ids(0) )
for i = 1 to UBound(objElement.Ids)
    WScript.Echo "                        " & GetBcdId( objElement.Ids(i) )
next

if not objWBM.GetElement( BcdOSLoaderObject_DefaultObject, objElement ) then
    WScript.Echo "ERROR WBM GetElement for " & Hex(BcdOSLoaderObject_DefaultObject) & " failed."
    WScript.Quit(1)
end if
WScript.Echo "default                 " & GetBcdId( objElement.Id )

if not objWBM.GetElement( BcdBootMgrObjectList_DisplayOrder, objElement ) then
    WScript.Echo "ERROR WBM GetElement for " & Hex(BcdBootMgrObjectList_DisplayOrder) & " failed."
    WScript.Quit(1)
end if
WScript.Echo "displayorder            " & GetBcdId( objElement.Ids(0) )
for i = 1 to UBound(objElement.Ids)
    WScript.Echo "                        " & GetBcdId( objElement.Ids(i) )
next

if not objWBM.GetElement( BcdBootMgrObjectList_ToolsDisplayOrder, objElement ) then
    WScript.Echo "ERROR WBM GetElement for " & Hex(BcdBootMgrObjectList_ToolsDisplayOrder) & " failed."
    WScript.Quit(1)
end if
WScript.Echo "toolsdisplayorder       " & GetBcdId( objElement.Ids(0) )
for i = 1 to UBound(objElement.Ids)
    WScript.Echo "                        " & GetBcdId( objElement.Ids(i) )
next

if not objWBM.GetElement( BcdBootMgrInteger_Timeout, objElement ) then
    WScript.Echo "ERROR WBM GetElement for " & Hex(BcdBootMgrInteger_Timeout) & " failed."
    WScript.Quit(1)
end if
WScript.Echo "timeout                 " & objElement.Integer

set objWBM = nothing

'----------------------------------------------------------------------
' Determine if any legacy Windows boot loaders are present.
'
'   0x10IAAAAA is the object type for a boot environment application. 
'       ||
'       |+....Application type, 1=firmware,            2=Windows boot manager,
'       |                       3=Windows boot loader, 4=Windows resume application 
'       |                       5=Memory tester,       6=legacy NTLdr,
'       |                       7=legacy SetupLdr,     8=Boot sector
'       |                       9=startup module,      a=generic application
'       |
'       +.....Image type 1=firmware, 2=boot, 3=legacy loader, 4=real mode code
'
'  A type 0f 0x10300006 indicates all legacy Windows boot loaders.
'----------------------------------------------------------------------
if not objBcdStore.EnumerateObjects( &h10300006, colObjects ) then
    WScript.Echo "ERROR objBcdStore.EnumberateObjects( &h10300006 ) failed."
    WScript.Quit(1)
end if

for each objNtldr in colObjects
    WScript.Echo ""
    WScript.Echo "Windows Legacy OS Loader"
    WScript.Echo "------------------------"
    WScript.Echo "identifier              " & GetBcdId( objNtldr.Id )

    if not objNtldr.GetElement( BcdLibraryDevice_ApplicationDevice, objElement ) then
        WScript.Echo "ERROR Ntldr GetElement for " & Hex(BcdLibraryDevice_ApplicationDevice) & " failed."
        WScript.Quit(1)
    end if

    WScript.Echo "device                  " & objElement.Device.Path

    if not objNtldr.GetElement( BcdLibraryString_ApplicationPath, objElement ) then
        WScript.Echo "ERROR Ntldr GetElement for " & Hex(BcdLibraryString_ApplicationPath) & " failed."
        WScript.Quit(1)
    end if
    WScript.Echo "path                    " & objElement.String

    if not objNtldr.GetElement( BcdLibraryString_Description, objElement ) then
        WScript.Echo "ERROR Ntldr GetElement for " & Hex(BcdLibraryString_Description) & " failed."
        WScript.Quit(1)
    end if
    WScript.Echo "description             " & objElement.String
next

set objNtldr = nothing

'----------------------------------------------------------------------
' Determine if a Vista Windows boot loader is present:
'
'   0x10IAAAAA is the object type for a boot environment application. 
'       ||
'       |+....Application type, 1=firmware,            2=Windows boot manager,
'       |                       3=Windows boot loader, 4=Windows resume application 
'       |                       5=Memory tester,       6=legacy NTLdr,
'       |                       7=legacy SetupLdr,     8=Boot sector
'       |                       9=startup module,      a=generic application
'       |
'       +.....Image type 1=firmware, 2=boot, 3=legacy loader, 4=real mode code
'
'  A type 0f 0x10200003 indicates all boot images for Windows boot
'  loader.
'----------------------------------------------------------------------
if not objBcdStore.EnumerateObjects( &h10200003, colObjects ) then
    WScript.Echo "ERROR objBcdStore.EnumberateObjects( &h10200003 ) failed."
    WScript.Quit(1)
end if

for each objWBL in colObjects
    WScript.Echo ""
    WScript.Echo "Windows Boot Loader"
    WScript.Echo "-------------------"

    WScript.Echo "identifier              " & GetBcdId( objWBL.Id )

    if not objWBL.GetElement( BcdLibraryDevice_ApplicationDevice, objElement ) then
        WScript.Echo "ERROR WBL GetElement for " & Hex(BcdLibraryDevice_ApplicationDevice) & " failed."
        WScript.Quit(1)
    end if

    WScript.Echo "device                  " & objElement.Device.Path

    if not objWBL.GetElement( BcdLibraryString_ApplicationPath, objElement ) then
        WScript.Echo "ERROR WBL GetElement for " & Hex(BcdLibraryString_ApplicationPath) & " failed."
        WScript.Quit(1)
    end if
    WScript.Echo "path                    " & objElement.String

    if not objWBL.GetElement( BcdLibraryString_Description, objElement ) then
        WScript.Echo "ERROR WBL GetElement for " & Hex(BcdLibraryString_Description) & " failed."
        WScript.Quit(1)
    end if
    WScript.Echo "description             " & objElement.String

    if not objWBL.GetElement( BcdLibraryString_PreferredLocale, objElement ) then
        WScript.Echo "ERROR WBL GetElement for " & Hex(BcdLibraryString_PreferredLocale) & " failed."
        WScript.Quit(1)
    end if
    WScript.Echo "locale                  " & objElement.String

    if not objWBL.GetElement( BcdLibraryObjectList_InheritedObjects, objElement ) then
        WScript.Echo "ERROR WBL GetElement for " & Hex(BcdLibraryObjectList_InheritedObjects) & " failed."
        WScript.Quit(1)
    end if
    WScript.Echo "inherit                 " & GetBcdId( objElement.Ids(0) )
    for i = 1 to UBound(objElement.Ids)
        WScript.Echo "                        " & GetBcdId( objElement.Ids(i) )
    next

    if not objWBL.GetElement( BcdOSLoaderDevice_OSDevice, objElement ) then
        WScript.Echo "ERROR WBL GetElement for " & Hex(BcdOSLoaderDevice_OSDevice) & " failed."
        WScript.Quit(1)
    end if
    WScript.Echo "osdevice                " & objElement.Device.Path

    if not objWBL.GetElement( BcdOSLoaderString_SystemRoot, objElement ) then
        WScript.Echo "ERROR WBL GetElement for " & Hex(BcdOSLoaderString_SystemRoot) & " failed."
        WScript.Quit(1)
    end if
    WScript.Echo "systemroot              " & objElement.String

    if not objWBL.GetElement( BcdOSLoaderObject_AssociatedResumeObject, objElement ) then
        WScript.Echo "ERROR WBL GetElement for " & Hex(BcdOSLoaderObject_AssociatedResumeObject) & " failed."
        WScript.Quit(1)
    end if
    WScript.Echo "resumeobject            " & GetBcdId( objElement.Id )

    if not objWBL.GetElement( BcdOSLoaderInteger_NxPolicy, objElement ) then
        WScript.Echo "ERROR WBL GetElement for " & Hex(BcdOSLoaderInteger_NxPolicy) & " failed."
        WScript.Quit(1)
    end if

    select case objElement.Integer
        case 0
            strTemp = "OptIn"
        case 1
            strTemp = "OptOut"
        case 2
            strTemp = "AlwaysOff"
        case 3
            strTemp = "AlwaysOn"
        case else
            strTemp = "Unknown"
    end select   
    WScript.Echo "nx                      " & strTemp
next

set objElement  = nothing
set objWBL      = nothing
set objBcdStore = nothing
set objLocator  = nothing
set objRootWMI  = nothing
WScript.Quit(0)

'**********************************************************************
' Function: GetBcdId
'----------------------------------------------------------------------
'  Purpose: To convert GUID strings to their well-known names.
'    Parms: strGUID - [in][req] string
'  Returns: Either the input string or the well-known name.
'**********************************************************************
function GetBcdId( strGUID )
	select case strGUID
		case "{0ce4991b-e6b3-4b16-b23c-5e0d9250e5d9}"
			GetBcdId = "{emssettings}"
		case "{1afa9c49-16ab-4a5c-901b-212802da9460}"
			GetBcdId = "{resumeloadersettings}"
		case "{4636856e-540f-4170-a130-a84776f4c654}"
			GetBcdId = "{dbgsettings}"
		case "{466f5a88-0af2-4f76-9038-095b170dc21c}"
			GetBcdId = "{ntldr}"
		case "{5189b25c-5558-4bf2-bca4-289b11bd29e2}"
			GetBcdId = "{badmemory}"
		case "{6efb52bf-1766-41db-a6b3-0ee5eff72bd7}"
			GetBcdId = "{bootloadersettings}"
		case "{7ea2e1ac-2e61-4728-aaa3-896d9d0a9f0e}"
			GetBcdId = "{globalsettings}"
		case "{9dea862c-5cdd-4e70-acc1-f32b344d4795}"
			GetBcdId = "{bootmgr}"
		case "{a5a30fa2-3d06-4e9f-b5f4-a01df9d1fcba}"
			GetBcdId = "{fwbootmgr}"
		case "{b2721d73-1db4-4c62-bf78-c548a880142d}"
			GetBcdId = "{memdiag}"
		case "{b4cf24bc-dfab-11db-8a4d-85082ac3becb}"
			GetBcdId = "{current}"
		case "{fa926493-6f1c-4193-a414-58f0b2456d1e}"
			GetBcdId = "{current}"
		case else
			GetBcdId = strGUID
	end select
end function

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions