@setlocal DisableDelayedExpansion
@echo off
set "_args=%*"
set "_elv="
if not defined _args goto :NoProgArgs
if "%~1"=="" set "_args="&goto :NoProgArgs
set _args=%_args:"=%
for %%A in (%_args%) do (
if /i "%%A"=="-wow" (set _rel1=1) else if /i "%%A"=="-arm" (set _rel2=1) else if /i "%%A"=="-su" (set _elv=1)
)
:NoProgArgs
set "_cmdf=%~f0"
if exist "%SystemRoot%\Sysnative\cmd.exe" if not defined _rel1 (
setlocal EnableDelayedExpansion
start %SystemRoot%\Sysnative\cmd.exe /c ""!_cmdf!" -wow %*"
exit /b
)
if exist "%SystemRoot%\SysArm32\cmd.exe" if /i %PROCESSOR_ARCHITECTURE%==AMD64 if not defined _rel2 (
setlocal EnableDelayedExpansion
start %SystemRoot%\SysArm32\cmd.exe /c ""!_cmdf!" -arm %*"
exit /b
)
set "SysPath=%SystemRoot%\System32"
set "Path=%SystemRoot%\System32;%SystemRoot%\System32\Wbem;%SystemRoot%\System32\WindowsPowerShell\v1.0\"
if exist "%SystemRoot%\Sysnative\reg.exe" (
set "SysPath=%SystemRoot%\Sysnative"
set "Path=%SystemRoot%\Sysnative;%SystemRoot%\Sysnative\Wbem;%SystemRoot%\Sysnative\WindowsPowerShell\v1.0\;%Path%"
)
set "_err===== ERROR ===="
for /f "tokens=6 delims=[]. " %%# in ('ver') do (
if %%# gtr 7700 goto :E_Win
if %%# lss 7600 goto :E_Win
)
reg query HKU\S-1-5-19 1>nul 2>nul || goto :E_Admin

set "_wrn===== WARNING ===="
set "_POS=HKLM\SOFTWARE\Microsoft\POSReady"
set "_EDT=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
set "_SVC=HKLM\SYSTEM\CurrentControlSet\Services\wuauserv\Parameters"
set "_WUA=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
set "_SPP=HKLM\SOFTWARE\Classes\CLSID\{6ACB028E-48C0-4A44-964C-E14567C578BA}\InprocServer32"
set "pPKG=%SystemRoot%\Servicing\Packages"
set "xOS=x64"
if /i %PROCESSOR_ARCHITECTURE%==x86 (if not defined PROCESSOR_ARCHITEW6432 (
  set "xOS=x86"
  )
)
if exist "%pPKG%\Microsoft-Windows-Server-LanguagePack-Package~*.mum" goto :E_Win

set _Embed=0
if exist "%pPKG%\WinEmb-Branding-Embedded-*.mum" set _Embed=1

set _SrvOp=0
if %_Embed%==0 if %xOS%==x64 set _SrvOp=1

if %_Embed%==0 if not exist "%SysPath%\spp\tokens\channels\Client-ESU-Year3\*.xrm-ms" goto :E_LPP

if exist "%SystemRoot%\WinSxS\pending.xml" (
echo %_wrn%
echo Pending update operation detected.
echo Restart the system first, then run the script.
goto :TheEnd
)

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

call :TIcmd 1>nul 2>nul
whoami /USER | find /i "S-1-5-18" 1>nul && (
goto :Begin
) || (
if defined _elv goto :E_TI
net start TrustedInstaller 1>nul 2>nul
1>nul 2>nul NSudoLC.exe -U:T -P:E cmd.exe /c ""!_bat!" -su" &exit /b
)
whoami /USER | find /i "S-1-5-18" 1>nul || goto :E_TI

:Begin
@cls
echo.
echo ____________________________________________________________
echo.
echo Checking . . .
echo.

set "_sku="
for /f "tokens=2 delims==" %%# in ('"wmic OS Get OperatingSystemSKU /value" 2^>nul') do set "_sku=%%#"
if "%_sku%"=="" goto :E_WMI

set _WuExt=0
if exist "%SysPath%\sle.dll" (
if exist "%SysPath%\kerneles.dll" set _WuExt=1
if exist "%SysPath%\wbem\slw.dll" set _WuExt=1
)

set _Optn1=1&set _Optn2=1&set _Optn3=1
if %_SrvOp%==0 set _Optn2=0&set _Optn3=0
if %_WuExt%==1 (
if exist "%SysPath%\wbem\slw.dll" set _Optn1=0
if not exist "%SysPath%\wbem\slw.dll" if %_SrvOp%==1 (
  reg query "%_EDT%" /v EditionID 2>nul | find /i "ServerEnterprise" 1>nul && set _Optn2=0
  reg query "%_EDT%" /v EditionID 2>nul | find /i "ServerStandard" 1>nul && set _Optn3=0
  )
)

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 6 goto :E_WUver
if %_ver:~2,1% equ 6 if %_ver:~4,4% lss 7601 goto :E_WUver
if %_ver:~2,1% equ 6 if %_ver:~4,4% equ 7601 if %_ver:~9% lss 19161 goto :E_WUver

set _wufile=wuaueng.dll
if exist "%SysPath%\wuaueng2.dll" set _wufile=wuaueng2.dll
@title WU ESU Patcher {source: %_wufile%}

:MainMenu
set _elr=0
@cls
echo ____________________________________________________________
echo.
if %_Optn1%==1 (
if %_Embed%==0 (echo [1] Patch as Windows Embedded 7 category) else (echo [1] Install for Windows Embedded 7 category)
echo.
)
if %_Optn2%==1 (
echo [2] Patch as Windows Server 2008 R2 category
echo.
)
if %_Optn3%==1 (
echo [3] Patch as Windows Server 2008 R2 category + Windows 7 category
echo.
)
if %_WuExt%==1 (
echo [4] Remove WU ESU Patcher
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 %_WuExt%==1 (goto :Unpatch)
if %_elr%==3 if %_Optn3%==1 (goto :doPatch)
if %_elr%==2 if %_Optn2%==1 (goto :doPatch)
if %_elr%==1 if %_Optn1%==1 (goto :doPatch)
goto :MainMenu

:doPatch
@cls
echo.
echo ____________________________________________________________

if %_elr%==1 set "_DLL=kernelesC.dll"&set "_SLE=sleC.dll"
if %_elr%==2 set "_DLL=kernelesS.dll"&set "_SLE=sleS.dll"
if %_elr%==3 set "_DLL=kernelesA.dll"&set "_SLE=sleS.dll"
set "_SLW=slwE.dll"
if %_Embed%==0 set "_SLW=slwC.dll"

call :sharedB
1>nul 2>nul schtasks /Delete /F /TN "Patch WU ESU"
if exist "%SystemRoot%\WuEsu\" rmdir /s /q "%SystemRoot%\WuEsu\"
if exist "%SysPath%\wuaueng3.dll" del /f /q %SysPath%\wuaueng3.dll

echo.
echo - Patch %_wufile%
if %_Embed%==0 (
1>nul 2>nul bbe.exe -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%\wuaueng3.dll %SysPath%\%_wufile%
) else (
1>nul 2>nul bbe.exe -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%\wuaueng3.dll %SysPath%\%_wufile%
)
if not exist "%SysPath%\wuaueng3.dll" goto :E_Patch

if %_elr%==1 (
echo.
echo - Patch sppwmi.dll
1>nul 2>nul bbe.exe -e "s/slc.dll/slw.dll/" -o %SysPath%\wbem\sppwmi.dll %SysPath%\sppwmi.dll
if not exist "%SysPath%\wbem\sppwmi.dll" goto :E_SPP
echo.
echo - Add slw.dll
1>nul 2>nul copy /y %_SLW% "%SysPath%\wbem\slw.dll"
1>nul 2>nul reg add %_POS% /f /v Version /t REG_SZ /d "7.0"
1>nul 2>nul reg add "%_SPP%" /f /ve /t REG_EXPAND_SZ /d ^%%SystemRoot^%%\System32\wbem\sppwmi.dll
) else (
1>nul 2>nul reg add "%_SPP%" /f /ve /t REG_EXPAND_SZ /d ^%%SystemRoot^%%\System32\sppwmi.dll
if %_Embed%==0 reg delete %_POS% /f 1>nul 2>nul
if exist "%SysPath%\wbem\slw.dll" del /f /q %SysPath%\wbem\slw.dll
if exist "%SysPath%\wbem\sppwmi.dll" del /f /q %SysPath%\wbem\sppwmi.dll
)

echo.
echo - Add sle.dll
1>nul 2>nul copy /y %_SLE% %SysPath%\sle.dll
1>nul 2>nul reg add "%_SVC%" /f /v ServiceDll /t REG_EXPAND_SZ /d ^%%SystemRoot^%%\System32\wuaueng3.dll
if %_Embed%==0 (
echo.
echo - Add kerneles.dll
1>nul 2>nul copy /y %_DLL% %SysPath%\kerneles.dll
) else (
if exist "%SysPath%\kerneles.dll" del /f /q %SysPath%\kerneles.dll
)

echo.
echo - Add "%SystemRoot%\WuEsu"
mkdir %SystemRoot%\WuEsu
cd ..
1>nul 2>nul copy /y %xOS%\bbe.exe %SystemRoot%\WuEsu\bbe.exe
1>nul 2>nul copy /y PatchWU.cmd %SystemRoot%\WuEsu\PatchWU.cmd
echo.
echo - Add schedule task "Patch WU ESU"
1>nul 2>nul schtasks /Create /F /TN "Patch WU ESU" /XML PatchWU.xml

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"
set _eid=ServerEnterprise
if %_elr%==3 set _eid=ServerStandard
if %_elr%==1 (
1>nul 2>nul reg add "%_EDT%" /f /v EditionID /d %_ebak%
1>nul 2>nul reg delete "%_EDT%" /f /v EditionID_bak
) else (
echo.
echo - Change registry EditionID to "%_eid%"
1>nul 2>nul reg add "%_EDT%" /f /v EditionID /d %_eid%
1>nul 2>nul reg add "%_EDT%" /f /v EditionID_bak /d %_ebak%
)

call :sharedE
echo.
echo Done.
echo It's highly recommended to restart the system.
goto :TheEnd

:sharedB
set "_abak=0x1"
for /f "skip=2 tokens=2*" %%a in ('reg query "%_WUA%" /v AUOptions 2^>nul') do set "_abak=%%b"
reg query "%_WUA%" /v AUOptions_bak 1>nul 2>nul && for /f "skip=2 tokens=2*" %%a in ('reg query "%_WUA%" /v AUOptions_bak 2^>nul') do set "_abak=%%b"
1>nul 2>nul reg add "%_WUA%" /f /v AUOptions /t REG_DWORD /d 0x1
1>nul 2>nul reg add "%_WUA%" /f /v AUOptions_bak /t REG_DWORD /d %_abak%
echo.
echo - Stop related services
call :StopService sppsvc 1>nul 2>nul
call :StopService BITS 1>nul 2>nul
call :StopService TrustedInstaller 1>nul 2>nul
call :StopService wuauserv 1>nul 2>nul
exit /b

:sharedE
1>nul 2>nul reg add "%_WUA%" /f /v AUOptions /t REG_DWORD /d %_abak%
1>nul 2>nul reg delete "%_WUA%" /f /v AUOptions_bak
exit /b

:Unpatch
@cls
echo.
echo ____________________________________________________________

call :sharedB

echo.
echo - Remove patched files
if exist "%SysPath%\wuaueng3.dll" del /f /q %SysPath%\wuaueng3.dll
if exist "%SysPath%\kerneles.dll" del /f /q %SysPath%\kerneles.dll
if exist "%SysPath%\sle.dll" del /f /q %SysPath%\sle.dll
if exist "%SysPath%\wbem\slw.dll" del /f /q %SysPath%\wbem\slw.dll
if exist "%SysPath%\wbem\sppwmi.dll" del /f /q %SysPath%\wbem\sppwmi.dll
if exist "%SystemRoot%\WuEsu\" rmdir /s /q "%SystemRoot%\WuEsu\"
echo.
echo - Remove schedule task "Patch WU ESU"
1>nul 2>nul schtasks /Delete /F /TN "Patch WU ESU"
echo.
echo - Restore registry for wuauserv/sppwmi.dll
1>nul 2>nul reg add "%_SVC%" /f /v ServiceDll /t REG_EXPAND_SZ /d ^%%SystemRoot^%%\System32\%_wufile%
1>nul 2>nul reg add "%_SPP%" /f /ve /t REG_EXPAND_SZ /d ^%%SystemRoot^%%\System32\sppwmi.dll
if %_Embed%==0 reg delete %_POS% /f 1>nul 2>nul

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 (
echo.
echo - Restore registry EditionID to "%_ebak%"
1>nul 2>nul reg add "%_EDT%" /f /v EditionID /d %_ebak%
1>nul 2>nul reg delete "%_EDT%" /f /v EditionID_bak
)

call :sharedE
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

:TIcmd
reg delete HKU\.DEFAULT\Console\^%%SystemRoot^%%_system32_cmd.exe /f
reg add HKU\.DEFAULT\Console /f /v FaceName /t REG_SZ /d Consolas
reg add HKU\.DEFAULT\Console /f /v FontFamily /t REG_DWORD /d 0x36
reg add HKU\.DEFAULT\Console /f /v FontSize /t REG_DWORD /d 0x100000
reg add HKU\.DEFAULT\Console /f /v FontWeight /t REG_DWORD /d 0x190
reg add HKU\.DEFAULT\Console /f /v ScreenBufferSize /t REG_DWORD /d 0x12c0050
exit /b

:E_TI
echo %_err%
echo Failed running the script with TrustedInstaller privileges.
goto :TheEnd

:E_WMI
echo %_err%
echo Failed detecting OS SKU.
echo Verify that Windows Management Instrumentation service "WinMgmt" is working.
goto :TheEnd

:E_LPP
echo %_err%
echo ESU Licensing Preparation Package is not detected.
echo.
echo install KB4575903 for Windows Embedded 7 category.
echo install KB4575903 and KB5016892 for Server 2008 R2 category.
goto :TheEnd

:E_WUver
echo %_err%
echo wuaueng.dll version not supported
echo Minimum : 7.6.7601.19161
echo Detected: %_ver%
echo.
echo install KB3138612 at least, and reboot.
goto :TheEnd

:E_Patch
call :sharedE
echo %_err%
echo Failed.
goto :TheEnd

:E_SPP
call :sharedE
if exist "%SysPath%\wuaueng3.dll" del /f /q %SysPath%\wuaueng3.dll
1>nul 2>nul reg add "%_SVC%" /f /v ServiceDll /t REG_EXPAND_SZ /d ^%%SystemRoot^%%\System32\%_wufile%
echo %_err%
echo Failed.
goto :TheEnd

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

:E_Win
echo %_err%
echo This project is for Windows 7 Client/Embedded only.
goto :TheEnd

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