Skip to content

Instantly share code, notes, and snippets.

@AlexanderPavlenko
Created April 1, 2012 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AlexanderPavlenko/2278427 to your computer and use it in GitHub Desktop.
Save AlexanderPavlenko/2278427 to your computer and use it in GitHub Desktop.
usbbootfix.bat doesn't help by itself since SP1, but may be involved, so run it before set_7_usb_boot.cmd
@echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd %~dp0
rem set_7_usb_boot.cmd v0.02
rem created by cdob
echo prepare offline windows 7 registry, set USB boot drivers &echo.
set System_Root=\Windows
if not %1.==. set System_Root=%1
if not exist "%System_Root%\system32\config\system" set /P System_Root=Path to your SystemRoot folder on usb drive (e:\Windows):
if not exist "%System_Root%\system32\config\system" (echo Error: target not found &pause &popd &goto :eof)
copy "%System_Root%\system32\config\system" "%System_Root%\system32\config\system_%random%.sav"
reg.exe load HKLM\loaded_SYSTEM "%System_Root%\system32\config\system"
set ControlSetDefault=
call :setReg Default
call :setReg LastKnownGood
reg.exe unload HKLM\loaded_SYSTEM
popd
goto :eof
:setReg
set ControlSet=001
reg.exe query "HKLM\loaded_SYSTEM\Select" /v "%1" >nul 2>&1 && (
for /f "skip=2 tokens=3" %%a in ('reg.exe query "HKLM\loaded_SYSTEM\Select" /v "%1"') do set /a ControlSet=%%a
set ControlSet=000!ControlSet!
)
set ControlSetNNN=ControlSet%ControlSet:~-3%
if %ControlSetDefault%.==%ControlSetNNN%. goto :eof
echo. &echo ControlSet "%ControlSetNNN%" used.
set Services=HKLM\loaded_SYSTEM\%ControlSetNNN%\Services
set Control=HKLM\loaded_SYSTEM\%ControlSetNNN%\Control
rem http://msdn.microsoft.com/en-us/library/ff794567(v=winembedded.1001).aspx
rem Deploying Microsoft Hyper-V Server 2008 R2 on USB Flash Drive
rem http://technet.microsoft.com/en-us/library/ee731893%28WS.10%29.aspx
reg.exe add %Control%\PnP /f /v PollBootPartitionTimeout /t REG_DWORD /d 30000
rem Loads VHD, SD, and USB boot drivers.
reg.exe add %Control% /f /v BootDriverFlags /t REG_DWORD /d 0x6
set ControlSetDefault=%ControlSetNNN%
goto :eof
@echo off
if "%1"=="fix" goto :fix
rem -- install task
copy /y "%~f0" "%SystemRoot%\system32\usbbootfix.bat"
SCHTASKS /Create /RU SYSTEM /SC ONEVENT /MO "*[System[Provider[@Name='Microsoft-Windows-UserPnp'] and EventID=20003]]" /EC System /TN USBBootFix /TR "'%SystemRoot%\system32\usbbootfix.bat' fix" /F
rem -- apply other settings
reg add HKLM\SYSTEM\CurrentControlSet\services\pciide /v Start /t REG_DWORD /d 0x0 /f
reg add HKLM\SYSTEM\CurrentControlSet\services\msahci /v Start /t REG_DWORD /d 0x0 /f
reg add HKLM\SYSTEM\CurrentControlSet\services\intelide /v Start /t REG_DWORD /d 0x0 /f
reg add HKLM\SYSTEM\CurrentControlSet\services\viaide /v Start /t REG_DWORD /d 0x0 /f
rem -- run :fix once after install
:fix
call :fixservice usbehci "Boot Bus Extender"
call :fixservice usbohci "Boot Bus Extender"
call :fixservice usbuhci "Boot Bus Extender"
call :fixservice usbhub "System Bus Extender"
call :fixservice usbstor "SCSI miniport"
goto :eof
:fixservice
setlocal
set Start=
set Group=
for /f "skip=2 tokens=1,2,*" %%I in ('reg query HKLM\SYSTEM\CurrentControlSet\services\%~1') do (
if "%%I"=="Start" set Start=%%K
if "%%I"=="Group" set Group=%%K
)
if not "%Start%"=="0x0" reg add HKLM\SYSTEM\CurrentControlSet\services\%~1 /v Start /t REG_DWORD /d 0x0 /f
if not "%Group%"=="%~2" reg add HKLM\SYSTEM\CurrentControlSet\services\%~1 /v Group /t REG_SZ /d "%~2" /f
endlocal
goto :eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment