Sign in to follow this  
Followers 0
forthewin

Changing boot device before reboot

3 posts in this topic

I am running AuoIt ver. 3.3.8.1

I looked around for ways to do this, but I haven't found anything yet. So, I just decided to take a wack at it for a project I'm working on. Here is the code that I have.

#include
#Include-Once
#RequireAdmin

Func _CreateBootEntry()

Local $Drive
Local $ScriptPath = @ScriptDir
$Drive = StringRegExp($ScriptPath, "([[:alpha:]]:)", 1)

Msgbox(4096, "Drive", $Drive[0])

RunWait("bcdedit.exe /create {USB} /d ""USB Boot""", "", @SW_HIDE)
RunWait("bcdedit.exe /set {USB} ramdiskdidevice partition= " & $Drive[0], "", @SW_HIDE)
RunWait("bcdedit.exe /set {USB} ramdiskdipath \boot\boot.sdi", "", @SW_HIDE)
RunWait(@ComSpec & " /c bcdedit.exe /create /d ""USB Boot"" /application OSLOADER > C:\GUID", "", @SW_HIDE)

$File = FileOpen("C:\GUID", 0)
$CurrentConfig = FileOpen(@HomeDrive & "\CurrentConfig.xml", 1)
$Contents = FileRead($File)
$GUID = StringRegExp($Contents, "(\{.*\})", 1)

FileWriteLine($CurrentConfig, "" & $GUID[0] & "")
RunWait("bcdedit.exe /set " & $GUID[0] & " device ramdisk=[" & $Drive[0] & "]\sources\boot.wim,{USB}", "", @SW_HIDE)
RunWait("bcdedit.exe /set " & $GUID[0] & " path \windows\system32\winload.exe", "", @SW_HIDE)
RunWait("bcdedit.exe /set " & $GUID[0] & " osdevice ramdisk=[" & $Drive[0] & "]\sources\boot.wim,{USB}", "", @SW_HIDE)
RunWait("bcdedit.exe /set " & $GUID[0] & " systemroot \windows", "", @SW_HIDE)
RunWait("bcdedit.exe /set " & $GUID[0] & " winpe yes", "", @SW_HIDE)
RunWait("bcdedit.exe /set " & $GUID[0] & " detecthal yes", "", @SW_HIDE)
RunWait("bcdedit.exe /displayorder " & $GUID[0] & " /addlast", "", @SW_HIDE)
RunWait("bcdedit.exe /bootsequence " & $GUID[0], "", @SW_HIDE)

EndFunc

The script is supposed to be run from the bootable device. It can be a CD, DVD, or USB. The commands work if you run them from an elevated command prompt, but they do not work from the code I have listed here. I will continue to work on it, but any assistance is greatly appreciated. Also, hopefully someone else will find this useful. It only works in Windows Vista and newer. You would have to modify the Boot.ini in Windows XP.

Share this post


Link to post
Share on other sites



Have you tried replacing all the RunWaits with FileWrites to a log to see if everything is syntactically correct?


f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
Share on other sites

So, here is the answer for anybody who stumbles across this in the future. I meant to come back a little sooner and update this.

#Include-Once
#RequireAdmin

Local $fDrive
Local $BootEntryName = "Custom Boot"
Local $ScriptPath = @ScriptDir
$fDrive = StringRegExp($ScriptPath, "([[:alpha:]]:)", 1)

;Setting up RAMDisk options WIM file
RunWait("bcdedit /create {ramdiskoptions} /d """ & $BootEntryName & """", "", @SW_HIDE)

RunWait("bcdedit /set {ramdiskoptions} ramdisksdidevice partition=" & $fDrive[0], "", @SW_HIDE)

RunWait("bcdedit /set {ramdiskoptions} ramdisksdipath \boot\boot.sdi", "", @SW_HIDE)

RunWait(@ComSpec & " /c bcdedit /create /d """ & $BootEntryName & """ /application OSLOADER > " & @HomeDrive & "\GUID", "", @SW_HIDE)

$File = FileOpen(@HomeDrive & "\GUID", 0)

$Contents = FileReadLine($File, 1)

If StringRegExp($Contents, "(?i)successfully", 0) Then

   $GUID = StringRegExp($Contents, "(\{.*\})", 1)

   RunWait("bcdedit /set " & $GUID[0] & " device ramdisk=[" & $fDrive[0] & "]\sources\boot.wim,{ramdiskoptions}", "", @SW_HIDE)

   RunWait("bcdedit /set " & $GUID[0] & " path \windows\system32\boot\winload.exe", "", @SW_HIDE)

   RunWait("bcdedit /set " & $GUID[0] & " osdevice ramdisk=[" & $fDrive[0] & "]\sources\boot.wim,{ramdiskoptions}", "", @SW_HIDE)

   RunWait("bcdedit /set " & $GUID[0] & " systemroot \windows", "", @SW_HIDE)

   RunWait("bcdedit /set " & $GUID[0] & " winpe yes", "", @SW_HIDE)

   RunWait("bcdedit /set " & $GUID[0] & " detecthal yes", "", @SW_HIDE)

   RunWait("bcdedit /displayorder " & $GUID[0] & " /addlast", "", @SW_HIDE)

   RunWait("bcdedit /bootsequence " & $GUID[0], "", @SW_HIDE)

Else

   MsgBox(16, "Error", $Contents)

EndIf

As it turned out, Larry was correct in guessing that the syntax was incorrect. The commands were hardcoded incorrectly. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0