@echo off
setlocal
set spath=%~dp0
cd "%spath%"

if not %spath:~0,2%.==\\. (
%spath:~0,2%
cd "%spath%"
set spath=.\
)
set value=your.kms.server
if not exist %spath%kmsserver.txt goto :nokmsfile

for /f "tokens=*" %%a in ('type %spath%kmsserver.txt 2^>NUL') do set value=%%a
set value=%value: =%


:nokmsfile
if %value%. == your.kms.server. set value=

if %value%. == . goto :kmsnotset

set KmsServer=172.16.252.11
echo KMS伺服器:%KmsServer%
cscript C:\Windows\System32\slmgr.vbs /skms %KmsServer% 
if exist "%programfiles% (x86)\Microsoft Office\OFFICE16\OSPP.VBS" set o16="%programfiles% (x86)\Microsoft Office\OFFICE16\OSPP.VBS"
if exist "%programfiles%\Microsoft Office\OFFICE16\OSPP.VBS" set o16="%programfiles%\Microsoft Office\OFFICE16\OSPP.VBS"


:MENU
@echo off
erase %temp%\kmserror.txt >nul 2>&1
set kmsok=0
cls

if %1.==1. goto win7
if %1.==4. goto office2024

echo -------------------------------------------------------------------------
echo - 微軟作業系統_OFFICE_大量授權啟用程式 (大橋國小王聖閔老師112.1.16原著;南科114.5.8修改)
echo -  
echo - ** 請記得使用滑鼠右鍵 以系統管理員身份執行此程式 **
echo - 
echo - 目前啟用伺服器 %KmsServer%

echo -
echo - 1.啟用Windows 7/8/8.1/10/11
echo - 2.檢查目前Windows授權狀態
echo - 3.啟用Office 2013        
echo - 4.啟用Office 2024/2021/2019(自動判斷)
echo - 5.清除OFFICE 2019/2021/2024大量授權金鑰
echo - 0.Exit      
echo --------------------------------------------------------------------------

set choice=
set /p choice= 選選擇1-5 0:結束 :
if '%choice%'=='1' goto win7
if '%choice%'=='2' goto DLI
if '%choice%'=='3' goto OFFICE2013
if '%choice%'=='4' goto OFFICE2024
if '%choice%'=='5' goto clearlicense
if '%choice%'=='0' exit
echo "%choice%" is not vaild, try again
goto MENU


:WIN7
set LANG=cht
set WIN7ERRMSG1=- 系統缺少 slmgr.vbs 這個用啟用程式
set WIN7ERRMSG2=- 系統無法常啟用。
if not exist %systemroot%\system32\slmgr.vbs goto WIN7ERROR
cls
@echo off
echo Windows 7/8/8.1/10/11 啟用中...
cscript C:\Windows\System32\slmgr.vbs /ipk VK7JG-NPHTM-C97JM-9MPGT-3V66T >nul 2>&1
cscript C:\Windows\System32\slmgr.vbs /ipk W269N-WFGWX-YVC9B-4J6C9-T83GX >nul 2>&1
cscript C:\Windows\System32\slmgr.vbs /ipk M7XTQ-FN8P6-TTKYV-9D4CC-J462D >nul 2>&1
cscript C:\Windows\System32\slmgr.vbs /ipk DCPHK-NFMTC-H88MJ-PFHPY-QJ4BJ >nul 2>&1

cscript C:\Windows\System32\slmgr.vbs /ato >nul 2>&1
cls
cscript C:\Windows\System32\slmgr.vbs /dli 
if %1.==1. goto office2024
pause
goto :menu

:DLI
set LANG=cht
set WIN7ERRMSG1=- 系統缺少 slmgr.vbs 這個用啟用程式
set WIN7ERRMSG2=- 系統無法常啟用。
if not exist %systemroot%\system32\slmgr.vbs goto WIN7ERROR
cls
@echo off
cls
echo 檢查WINDOWS授權中...
cscript C:\Windows\System32\slmgr.vbs /dli 
pause
goto :MENU



:clearlicense
call :clearofficekey
pause
goto :Menu



:WIN7ERROR
echo = !!!ERROR!!! =
echo %WIN7ERRMSG1%
echo %WIN7ERRMSG2%
pause
goto :MENU


:OFFICE14x6432
echo -------------------------------------
echo You are using 64bit OS 32bit office.
echo KMS Server is %kmsServer%
echo 啟用中...
echo -------------------------------------
cscript "%ProgramFiles% (x86)\Microsoft Office\OFFICE14\OSPP.VBS" /osppsvcrestart >nul
cscript "%ProgramFiles% (x86)\Microsoft Office\OFFICE14\OSPP.VBS" /sethst:%KmsServer% >nul
cscript "%ProgramFiles% (x86)\Microsoft Office\OFFICE14\OSPP.VBS" /act
goto office_end

:OFFICE14
echo -------------------------------------
echo KMS Server is %KmsServer%
echo 啟用中...
echo -------------------------------------
'cscript "%ProgramFiles%\Microsoft Office\OFFICE14\OSPP.VBS" /osppsvcrestart >nul 
cscript "%ProgramFiles%\Microsoft Office\OFFICE14\OSPP.VBS" /sethst:%KmsServer% >nul
cscript "%ProgramFiles%\Microsoft Office\OFFICE14\OSPP.VBS" /act
goto office_end


:OFFICE2013
if exist "%programfiles% (x86)\Microsoft Office\OFFICE15\OSPP.VBS" goto OFFICE15x6432
if exist "%programfiles%\Microsoft Office\OFFICE15\OSPP.VBS" goto OFFICE15
goto OFFICEERR


:OFFICE15x64
echo -------------------------------------
echo You are using 64bit OS 32bit OFFICE.
echo KMS Server is %KmsServer% 
echo 啟用中...
echo -------------------------------------
rem cscript "%ProgramFiles% (x86)\Microsoft Office\OFFICE15\OSPP.VBS" /osppsvcrestart >nul
cscript csc /sethst:%KmsServer% >nul
cscript "%ProgramFiles% (x86)\Microsoft Office\OFFICE15\OSPP.VBS" /act
goto office_end

:OFFICE15
echo -------------------------------------
echo KMS Server is %KmsServer%
echo 啟用中...
echo -------------------------------------
rem cscript "%ProgramFiles%\Microsoft Office\OFFICE15\OSPP.VBS" /osppsvcrestart >nul
cscript "%ProgramFiles%\Microsoft Office\OFFICE15\OSPP.VBS" /sethst:%KmsServer% >nul
cscript "%ProgramFiles%\Microsoft Office\OFFICE15\OSPP.VBS" /act
goto office_end




:clearofficekey

IF exist %spath%clearofficekey.cmd (
call %spath%clearofficekey.cmd
) else (

@echo 清除目前電腦OFFICE大量授權金鑰....
@cscript "C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs" /unpkey:6MWKP >nul 2>&1
@cscript "C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs" /unpkey:TCQWG >nul 2>&1
@cscript "C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs" /unpkey:6F7TH >nul 2>&1
@cscript "C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs" /unpkey:P7KB6 >nul 2>&1
@cscript "C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs" /unpkey:WFG99 >nul 2>&1
@cscript "C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs" /unpkey:XQBR2 >nul 2>&1
@cscript "C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs" /unpkey:7H7DH >nul 2>&1
@cscript "C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs" /unpkey:VMFTK >nul 2>&1
@cscript "C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs" /unpkey:GCVGB >nul 2>&1
@cscript "C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs" /unpkey:TX3T2 >nul 2>&1
@cscript "C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs" /unpkey:7PCTP >nul 2>&1
@cscript "C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs" /unpkey:Q32CX >nul 2>&1
@cscript "C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs" /unpkey:XBX6D >nul 2>&1
@cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /unpkey:6MWKP >nul 2>&1
@cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /unpkey:TCQWG >nul 2>&1
@cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /unpkey:6F7TH >nul 2>&1
@cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /unpkey:P7KB6 >nul 2>&1
@cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /unpkey:WFG99 >nul 2>&1
@cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /unpkey:XQBR2 >nul 2>&1
@cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /unpkey:7H7DH >nul 2>&1
@cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /unpkey:VMFTK >nul 2>&1
@cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /unpkey:GCVGB >nul 2>&1
@cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /unpkey:TX3T2 >nul 2>&1
@cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /unpkey:7PCTP >nul 2>&1
@cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /unpkey:Q32CX >nul 2>&1
@cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /unpkey:XBX6D >nul 2>&1
@echo 清除完成
)
exit /b



:checkoffice16
if %2.==0. (
set kmsok=%1
goto :office_end
)
echo 再次嘗試 %1 啟用中請稍候.....
echo ======================================== >> %temp%\kmserror.txt
echo 嘗試 %1 啟用 >> %temp%\kmserror.txt
echo ======================================== >> %temp%\kmserror.txt
cscript %o16% /act >> %temp%\kmserror.txt
echo ======================================== >> %temp%\kmserror.txt
type %temp%\kmserror.txt | find /i "Product activation successful" 
if %errorlevel%. == 0. (
  echo ＜＜%1產品已成功啟用＞＞
  erase %temp%\kmserror.txt
  set kmsok=%1
  ) else (
  set kmsok=-1
  echo ＜＜Office %1啟用失敗＞＞
)
exit /b


:OFFICE2019
@echo off
cls
set office=OFFICE_2019大量授權版
@echo 嘗試 %office% 啟用中請稍候.....
call :clearofficekey
cscript C:\Windows\System32\slmgr.vbs -ipk Q6G86-RKJ9F-KCKXM-JTRVF-7PCTP | find /i "成功"
echo 嘗試 %office% 啟用中請稍候.....
echo.
cscript C:\Windows\System32\slmgr.vbs -ato 85dd8b5f-eaa4-4af3-a628-cce9e77c9a03 | find /i "成功"
call :checkoffice16 %office% %errorlevel%
goto :office_end

:office2021warring
echo 提醒!!
echo. 
echo Office2021 或 Office2024_大量授權版不能在安裝OFFICE_2016/OFFICE 2019之環境下啟用
echo 若執行OFFICE出現『授權發生問題』訊息，請移除OFFICE，重新安裝OFFICE 2021 或 OFFICE 2024
echo 或改啟用OFFICE_2019或OFFICE_2016
echo ==============================================================================   
exit /b

:OFFICE2021
@echo off
cls
set office=OFFICE_2021大量授權版
if %o16%. == . goto :officeerr
@echo 嘗試 %office% 啟用中請稍候.....
call :clearofficekey
cscript C:\Windows\System32\slmgr.vbs -ipk BQ73Q-PNJK6-K4R7B-HC6HP-Q32CX | find /i "成功"
echo 嘗試 %office% 啟用中請稍候.....
echo.
cscript C:\Windows\System32\slmgr.vbs -ato fbdb3e18-a8ef-4fb3-9183-dffd60bd0984 | find /i "成功"
call :checkoffice16 %office% %errorlevel%
if %kmsok%.==-1. goto :office2019 
goto :office_end


:OFFICE2024
@echo off
cls
set office=OFFICE_2024大量授權版
if %o16%. == . goto :officeerr
@echo 嘗試 %office% 啟用中請稍候.....
call :clearofficekey
cscript C:\Windows\System32\slmgr.vbs -ipk W6N49-47BXD-JGMQY-6MM2B-XBX6D | find /i "成功"
echo 嘗試 %office% 啟用中請稍候.....
echo.
cscript C:\Windows\System32\slmgr.vbs -ato fceda083-1203-402a-8ec4-3d7ed9f3648c | find /i "成功"
call :checkoffice16 %office% %errorlevel%
if %kmsok%.==-1. goto :office2021 
goto :office_end


:office_end
if %kmsok%.==0. (
echo ==============================================================================   
echo 請注意：上方需有 ＜Product activation successful＞ 或 ＜產品已成功啟用＞ 出現,
echo 才表示您的 Office  啟動成功!!
echo ==============================================================================   
goto :office_end1
) 

if not %kmsok%.==-1. (
echo ==============================================================================   
echo Office %kmsok% 啟動成功!!
echo ==============================================================================   
)

rem echo %kmsok% | find /i "2021" >nul && call :office2021warring


if %kmsok%.==-1. (
echo ==============================================================================   
echo Office 啟動失敗!!
echo ==============================================================================   
)

echo.
if exist %temp%\kmserror.txt (
echo OFFICE 啟動過程訊息如下:
echo.
@type %temp%\kmserror.txt 
)



:office_end1
pause
erase %temp%\kmserror.text
set kmsok=0
if %1.==4. goto :end
goto :MENU


:OFFICEERR
set LANG=cht
set OFFICEERRMSG1=- 請確認您已完成OFFICE 安裝
echo = !!!ERROR!!! =
echo %OFFICEERRMSG1%
pause
goto :MENU


:kmsnotset
echo ==============================================================================   
echo 尚未設定 KMS伺服器位址
echo 請直接輸入 KMS伺服器位址或編輯 KmsServer.txt，設定KMS伺服器
echo ==============================================================================   
echo - 
set kms1=
set /p kms1= 請輸入kms大量授權伺服器位址:
set kmsserver=%kms1: =%
echo %kmsserver%>%spath%kmsserver.txt

goto :MENU

@pause

:end
exit

