@setlocal DisableDelayedExpansion
@echo off
if defined PROCESSOR_ARCHITEW6432 start "" %SystemRoot%\Sysnative\cmd.exe /c "%0 " &exit
set "SysPath=%SystemRoot%\System32"
set "Path=%SysPath%;%SystemRoot%;%SysPath%\Wbem;%SysPath%\WindowsPowerShell\v1.0\"

set "_err===== ERROR ===="
for /f "tokens=6 delims=[]. " %%# in ('ver') do if %%# geq 7600 goto :E_Win
reg query HKU\S-1-5-19 1>nul 2>nul || goto :E_Admin

set "_EDT=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
set "_SVC=HKLM\SYSTEM\CurrentControlSet\Services\wuauserv\Parameters"
set "xOS=64"
set "xBT=amd64"
if /i %PROCESSOR_ARCHITECTURE%==x86 (if not defined PROCESSOR_ARCHITEW6432 (
  set "xOS=32"
  set "xBT=x86"
  )
)
set "_bat=%~f0"
set "_arg=%~1"
set "_elv="
if defined _arg if /i "%_arg%"=="-su" set _elv=1

set "_work=%~dp0"
set "_work=%_work:~0,-1%"
setlocal EnableDelayedExpansion
pushd "!_work!"
if not exist "%xOS%\" (
echo %_err%
echo Required folder %xOS% is missing.
goto :TheEnd
)
cd %xOS%\
for %%# in (
bbe.exe kernelesA.dll kernelesS.dll kernelesV.dll sle.dll
) do if not exist "%%~#" (
echo %_err%
echo Required file %xOS%\%%~nx# is missing.
goto :TheEnd
)

:Begin
set "PKGS=%SystemRoot%\Servicing\Packages"
if exist "%PKGS%\Microsoft-Windows-Foundation-Package*6.0.6001.18000.mum" goto :E_Win

set "_ver=7.6.7600.256"
set "_file=%SysPath%\wuaueng.dll"
for /f "tokens=2 delims==" %%i in ('wmic /output:stdout datafile where "name='%_file:\=\\%'" get Version /value ^| find "="') do set "_ver=%%i"
if %_ver:~2,1% lss 7 goto :E_WUver
if %_ver:~2,1% equ 7 if %_ver:~4,4% lss 6003 goto :E_WUver
if %_ver:~2,1% equ 7 if %_ver:~4,4% equ 6003 if %_ver:~9% lss 20555 goto :E_WUver

:MainMenu
set _elr=0
@cls
echo ____________________________________________________________
echo.
echo [1] Patch WUC as Vista
echo.
echo [2] Patch WUC as Server2008
echo.
echo [3] Patch WUC as Server2008 + Vista
echo.
if exist "%SysPath%\wuaueng6.dll" (
echo [4] Remove Patched WUC
echo.
)
echo [9] Exit
echo.
echo ____________________________________________________________
echo.
choice /C 12349 /N /M "Choose a menu option: "
set _elr=%errorlevel%
if %_elr%==5 goto :eof
if %_elr%==4 if exist "%SysPath%\wuaueng6.dll" (goto :Unpatch)
if %_elr%==3 (set "_DLL=kernelesA.dll"&goto :PatchWU)
if %_elr%==2 (set "_DLL=kernelesS.dll"&goto :PatchWU)
if %_elr%==1 (set "_DLL=kernelesV.dll"&goto :PatchWU)
goto :MainMenu

:PatchWU
@cls
echo.
echo ____________________________________________________________
echo.
echo Patching wuaueng.dll . . .
echo.

call :StopService wuauserv 1>nul 2>nul
if exist "%SystemRoot%\WuEsu\" rmdir /s /q "%SystemRoot%\WuEsu\"
1>nul 2>nul schtasks /Delete /F /TN "Patch WU ESU"

1>nul 2>nul copy /y %_DLL% %SysPath%\kerneles.dll
if /i %_DLL%==kernelesV.dll (
if exist "%SysPath%\sle.dll" del /f /q "%SysPath%\sle.dll"
1>nul 2>nul bbe.exe -e "s/KERNEL32/KERNELES/" -e "s/kernel32/kerneles/" -o %SysPath%\wuaueng6.dll %SysPath%\wuaueng.dll
) else (
1>nul 2>nul copy /y sle.dll %SysPath%\sle.dll
1>nul 2>nul bbe.exe -e "s/KERNEL32/KERNELES/" -e "s/kernel32/kerneles/" -e "s/\x73\x00\x6C\x00\x63\x00\x2E\x00\x64\x00\x6C\x00\x6C\x00/\x73\x00\x6C\x00\x65\x00\x2E\x00\x64\x00\x6C\x00\x6C\x00/" -o %SysPath%\wuaueng6.dll %SysPath%\wuaueng.dll
)
1>nul 2>nul reg add "%_SVC%" /f /v ServiceDll /t REG_EXPAND_SZ /d ^%%SystemRoot^%%\System32\wuaueng6.dll

set "_ebak="
for /f "skip=2 tokens=2*" %%a in ('reg query "%_EDT%" /v EditionID 2^>nul') do set "_ebak=%%b"
reg query "%_EDT%" /v EditionID_bak 1>nul 2>nul && for /f "skip=2 tokens=2*" %%a in ('reg query "%_EDT%" /v EditionID_bak 2^>nul') do set "_ebak=%%b"

if /i %_DLL%==kernelesV.dll (
1>nul 2>nul reg add "%_EDT%" /f /v EditionID /d %_ebak%
1>nul 2>nul reg delete "%_EDT%" /f /v EditionID_bak
) else (
1>nul 2>nul reg add "%_EDT%" /f /v EditionID /d ServerStandard
1>nul 2>nul reg add "%_EDT%" /f /v EditionID_bak /d %_ebak%
)

echo.
echo Done.
goto :TheEnd

:Unpatch
@cls
echo.
echo ____________________________________________________________
echo.
echo Removing Patched wuaueng.dll . . .
echo.

call :StopService wuauserv 1>nul 2>nul
if exist "%SysPath%\kerneles.dll" del /f /q %SysPath%\kerneles.dll
if exist "%SysPath%\wuaueng6.dll" del /f /q %SysPath%\wuaueng6.dll
if exist "%SysPath%\sle.dll" del /f /q "%SysPath%\sle.dll"
1>nul 2>nul reg add "%_SVC%" /f /v ServiceDll /t REG_EXPAND_SZ /d ^%%SystemRoot^%%\System32\wuaueng.dll

set "_ebak="
reg query "%_EDT%" /v EditionID_bak 1>nul 2>nul && for /f "skip=2 tokens=2*" %%a in ('reg query "%_EDT%" /v EditionID_bak 2^>nul') do set "_ebak=%%b"
if defined _ebak (
1>nul 2>nul reg add "%_EDT%" /f /v EditionID /d %_ebak%
1>nul 2>nul reg delete "%_EDT%" /f /v EditionID_bak
)

echo.
echo Done.
goto :TheEnd

:StopService
sc query %1 | find /i "STOPPED" || net stop %1 /y
sc query %1 | find /i "STOPPED" || sc stop %1
exit /b

:E_Admin
echo %_err%
echo This script requires administrator privileges.
goto :TheEnd

:E_Win
echo %_err%
echo This project is for Windows Vista only.
goto :TheEnd

:E_WUver
echo %_err%
echo wuaueng.dll version not supported
echo Minimum : 7.7.6003.20555
echo Detected: %_ver%
echo.
echo You should reboot after running Install_WUC.cmd
echo or verify that WinMgmt service is working properly.

:TheEnd
echo.
echo Press any key to exit.
pause >nul
goto :eof
