11Imports System.ComponentModel
22Imports System.IO
33Imports Microsoft.Dism
4+ Imports Microsoft.Win32
45
56Namespace Helpers.PreparationTasks
67
@@ -9,6 +10,7 @@ Namespace Helpers.PreparationTasks
910
1011 Private WillPrepareBootImage As Boolean = Environment.GetCommandLineArgs().Contains( "/dt_capture" )
1112 Private Const DISM_ERR_CANT_UNMOUNT_OPEN_FILE_HANDLES As Integer = - 1052638953
13+ Private ReadOnly GUID_WINDOWS_SETUP_RAMDISK_OPTIONS As New Guid( "AE5534E0-A924-466C-B836-758539A3EE3A" )
1214
1315 ''' <summary>
1416 ''' Gets the information of a given Windows image.
@@ -193,8 +195,10 @@ Namespace Helpers.PreparationTasks
193195 RunBCDConfigurator( "/set {current} bootmenupolicy legacy" , True )
194196 RunBCDConfigurator( "/set {bootmgr} timeout 3" , True )
195197
196- ' Configure ramdisk
197- RunBCDConfigurator( "/create {ramdiskoptions}" )
198+ ' Configure ramdisk. Creating something that already exists will cause bcdedit to fail,
199+ ' so we'll first check if the ramdisk entry is already defined; it may have been added
200+ ' by a previous run of this PT.
201+ If Not BcdObjectExists(GUID_WINDOWS_SETUP_RAMDISK_OPTIONS) Then RunBCDConfigurator( "/create {ramdiskoptions}" )
198202 RunBCDConfigurator( "/set {ramdiskoptions} ramdisksdidevice partition=" & Environment.GetEnvironmentVariable( "SYSTEMDRIVE" ))
199203 RunBCDConfigurator( "/set {ramdiskoptions} ramdisksdipath " & sdiPath.Replace(Path.GetPathRoot(sdiPath), "\" ))
200204
@@ -239,6 +243,25 @@ Namespace Helpers.PreparationTasks
239243 RunBCDConfigurator( String .Format( "/default {0}" , targetGuid))
240244 End Sub
241245
246+ Private Function BcdObjectExists(objectId As Guid) As Boolean
247+ DynaLog.LogMessage( "Determining if BCD boot entry with object ID " & objectId.ToString() & " exists in the system BCD store..." )
248+
249+ Dim bcdEntryCollectionRk As RegistryKey = Nothing
250+ Dim objectEntryExists As Boolean = False
251+
252+ Try
253+ bcdEntryCollectionRk = Registry.LocalMachine.OpenSubKey( "BCD00000000\Objects" )
254+ objectEntryExists = bcdEntryCollectionRk.GetSubKeyNames().Any( Function (entry) entry.Equals( String .Format( "{{{0}}}" , objectId.ToString()), StringComparison.OrdinalIgnoreCase))
255+ Catch ex As Exception
256+ DynaLog.LogMessage( "Could not get presence of BCD boot entry object. Error message: " & ex.Message)
257+ Finally
258+ If bcdEntryCollectionRk IsNot Nothing Then bcdEntryCollectionRk.Close()
259+ End Try
260+
261+ DynaLog.LogMessage( "Boot entry object exists? " & If (objectEntryExists, "Yes" , "No" ))
262+ Return objectEntryExists
263+ End Function
264+
242265 ''' <summary>
243266 ''' Unmounts a specified Windows image.
244267 ''' </summary>
0 commit comments