2024-04-20 14:35:05 +02:00
@ set masver = 2.6
2022-07-25 20:03:00 +02:00
@ echo off
2023-11-15 22:03:20 +01:00
2022-07-25 20:03:00 +02:00
: :============================================================================
: :
2023-09-25 15:46:19 +02:00
: : Homepage: mass grave[.]dev
2024-08-16 22:11:42 +02:00
: : Email: mas.help@outlook.com
2022-07-25 20:03:00 +02:00
: :
: :============================================================================
: :========================================================================================================================================
2024-08-16 22:11:42 +02:00
: : Set Environment variables, it helps if they are misconfigured in the system
setlocal EnableExtensions
setlocal DisableDelayedExpansion
set " PathExt=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC "
2023-09-25 15:46:19 +02:00
2024-08-16 22:11:42 +02:00
set " SysPath= %SystemRoot% \System32 "
set " Path= %SystemRoot% \System32; %SystemRoot% ; %SystemRoot% \System32\Wbem; %SystemRoot% \System32\WindowsPowerShell\v1.0\ "
2023-09-25 15:46:19 +02:00
if exist " %SystemRoot% \Sysnative\reg.exe " (
2024-08-16 22:11:42 +02:00
set " SysPath= %SystemRoot% \Sysnative "
set " Path= %SystemRoot% \Sysnative; %SystemRoot% ; %SystemRoot% \Sysnative\Wbem; %SystemRoot% \Sysnative\WindowsPowerShell\v1.0\; %Path% "
2023-09-25 15:46:19 +02:00
)
2024-08-16 22:11:42 +02:00
set " ComSpec= %SysPath% \cmd.exe "
set " PSModulePath= %ProgramFiles% \WindowsPowerShell\Modules; %SysPath% \WindowsPowerShell\v1.0\Modules "
2022-07-25 20:03:00 +02:00
set " _cmdf= %~f0 "
for %% # in ( %* ) do (
if /i " %% # " == " r1 " set r1 = 1
if /i " %% # " == " r2 " set r2 = 1
)
2024-08-16 22:11:42 +02:00
: : Re-launch the script with x64 process if it was initiated by x86 process on x64 bit Windows
: : or with ARM64 process if it was initiated by x86/ARM32 process on ARM64 Windows
2022-07-25 20:03:00 +02:00
if exist %SystemRoot% \Sysnative\cmd.exe if not defined r1 (
setlocal EnableDelayedExpansion
start %SystemRoot% \Sysnative\cmd.exe /c " " !_cmdf! " %* r1 "
exit /b
)
: : Re-launch the script with ARM32 process if it was initiated by x64 process on ARM64 Windows
if exist %SystemRoot% \SysArm32\cmd.exe if %PROCESSOR_ARCHITECTURE% == AMD64 if not defined r2 (
setlocal EnableDelayedExpansion
start %SystemRoot% \SysArm32\cmd.exe /c " " !_cmdf! " %* r2 "
exit /b
)
2023-09-25 15:46:19 +02:00
: :========================================================================================================================================
2022-07-25 20:03:00 +02:00
2023-09-25 15:46:19 +02:00
set " blank= "
2023-09-30 22:24:15 +02:00
set " mas=ht %blank% tps %blank% ://mass %blank% grave.dev/ "
2023-09-25 15:46:19 +02:00
: : Check if Null service is working, it's important for the batch script
sc query Null | find /i " RUNNING "
if %errorlevel% NEQ 0 (
echo :
echo Null service is not running, script may crash...
echo :
echo :
2024-08-16 22:11:42 +02:00
echo Help - %mas% troubleshoot
2023-09-25 15:46:19 +02:00
echo :
echo :
2024-08-16 22:11:42 +02:00
ping 127.0.0.1 -n 20
2022-07-25 20:03:00 +02:00
)
2023-09-25 15:46:19 +02:00
cls
2022-07-25 20:03:00 +02:00
: : Check LF line ending
pushd " %~dp0 "
2023-09-25 15:46:19 +02:00
> nul findstr /v " $ " " %~nx0 " && (
2022-07-25 20:03:00 +02:00
echo :
2024-08-16 22:11:42 +02:00
echo Error - Script either has LF line ending issue or an empty line at the end of the script is missing.
echo :
echo :
echo Help - %mas% troubleshoot
echo :
2022-07-25 20:03:00 +02:00
echo :
2024-08-16 22:11:42 +02:00
ping 127.0.0.1 -n 20 > nul
2022-07-25 20:03:00 +02:00
popd
exit /b
)
popd
: :========================================================================================================================================
cls
color 07
2023-10-16 04:50:03 +02:00
title Troubleshoot %masver%
2022-07-25 20:03:00 +02:00
2023-09-25 15:46:19 +02:00
set _args =
2022-07-25 20:03:00 +02:00
set _elev =
2024-08-16 22:11:42 +02:00
set _unattended = 0
2022-07-25 20:03:00 +02:00
2023-09-25 15:46:19 +02:00
set _args = %*
if defined _args set _args = %_args:"=%
if defined _args (
for %% A in ( %_args% ) do (
if /i " %% A " == " -el " set _elev = 1
)
)
set " nul1=1>nul "
set " nul2=2>nul "
set " nul6=2 ^ >nul "
2022-07-25 20:03:00 +02:00
set " nul=>nul 2>&1 "
2023-09-25 15:46:19 +02:00
2024-08-16 22:11:42 +02:00
call : dk_setvar
2022-07-25 20:03:00 +02:00
set " line=_________________________________________________________________________________________________ "
: :========================================================================================================================================
if %winbuild% LSS 7600 (
%nceline%
2023-09-25 15:46:19 +02:00
echo Unsupported OS version detected [%winbuild% ].
2022-07-25 20:03:00 +02:00
echo Project is supported only for Windows 7/8/8.1/10/11 and their Server equivalent.
2024-08-16 22:11:42 +02:00
goto dk_done
2022-07-25 20:03:00 +02:00
)
: :========================================================================================================================================
2024-01-01 03:09:18 +01:00
: : Fix special characters limitation in path name
2022-07-25 20:03:00 +02:00
set " _work= %~dp0 "
if " %_work:~-1% " == " \ " set " _work= %_work:~0,-1% "
set " _batf= %~f0 "
set " _batp= %_batf:'=''% "
set _PSarg = " " " %~f0 " " " -el %_args%
2024-08-16 22:11:42 +02:00
set _PSarg = %_PSarg:'=''%
2022-07-25 20:03:00 +02:00
2023-10-16 04:50:03 +02:00
set " _ttemp= %userprofile% \AppData\Local\Temp "
2022-07-25 20:03:00 +02:00
setlocal EnableDelayedExpansion
: :========================================================================================================================================
2023-09-25 15:46:19 +02:00
echo " !_batf! " | find /i " !_ttemp! " %nul1% && (
2022-07-25 20:03:00 +02:00
if /i not " !_work! " == " !_ttemp! " (
2024-08-16 22:11:42 +02:00
%eline%
2022-07-25 20:03:00 +02:00
echo Script is launched from the temp folder,
echo Most likely you are running the script directly from the archive file.
echo :
echo Extract the archive file and launch the script from the extracted folder.
2024-08-16 22:11:42 +02:00
goto dk_done
)
)
: :========================================================================================================================================
: : Check PowerShell
REM :PowerShellTest: $ExecutionContext.SessionState.LanguageMode :PowerShellTest:
cmd /c " %psc% " $f=[io.file]::ReadAllText('!_batp!') -split ':PowerShellTest:\s*';iex ($f[1])" " | find /i " FullLanguage " %nul1% || (
%eline%
cmd /c " %psc% " $ExecutionContext.SessionState.LanguageMode" "
echo :
cmd /c " %psc% " $ExecutionContext.SessionState.LanguageMode" " | find /i " FullLanguage " %nul1% && (
echo Failed to run Powershell command but Powershell is working.
call : dk_color %Blue% " Check if your antivirus is blocking the script. "
echo :
set fixes = %fixes% %mas% troubleshoot
call : dk_color2 %Blue% " Help - " %_Yellow% " %mas% troubleshoot "
) || (
echo PowerShell is not working. Aborting...
echo If you have applied restrictions on Powershell then undo those changes.
echo :
set fixes = %fixes% %mas% fix_powershell
call : dk_color2 %Blue% " Help - " %_Yellow% " %mas% fix_powershell "
2022-07-25 20:03:00 +02:00
)
2024-08-16 22:11:42 +02:00
goto dk_done
2022-07-25 20:03:00 +02:00
)
: :========================================================================================================================================
: : Elevate script as admin and pass arguments and preventing loop
2023-09-25 15:46:19 +02:00
%nul1% fltmc || (
2024-08-16 22:11:42 +02:00
if not defined _elev %psc% " start cmd.exe -arg '/c \ " !_PSarg! \" ' -verb runas " && exit /b
%eline%
2024-01-01 03:09:18 +01:00
echo This script needs admin rights.
2022-07-25 20:03:00 +02:00
echo To do so, right click on this script and select 'Run as administrator'.
2024-08-16 22:11:42 +02:00
goto dk_done
2022-07-25 20:03:00 +02:00
)
: :========================================================================================================================================
2024-08-16 22:11:42 +02:00
: : Disable QuickEdit and launch from conhost.exe to avoid Terminal app
2023-09-25 15:46:19 +02:00
2024-08-16 22:11:42 +02:00
if %winbuild% GEQ 17763 (
set terminal = 1
) else (
set terminal =
)
2023-09-25 15:46:19 +02:00
2024-08-16 22:11:42 +02:00
: : Check if script is running in Terminal app
set r1 = $TB = [AppDomain]::CurrentDomain.DefineDynamicAssembly(4, 1).DefineDynamicModule(2, $False).DefineType(0);
set r2 = %r1% [void]$TB.DefinePInvokeMethod('GetConsoleWindow', 'kernel32.dll', 22, 1, [IntPtr], @(), 1, 3).SetImplementationFlags(128);
set r3 = %r2% [void]$TB.DefinePInvokeMethod('SendMessageW', 'user32.dll', 22, 1, [IntPtr], @([IntPtr], [UInt32], [IntPtr], [IntPtr]), 1, 3).SetImplementationFlags(128);
set d1 = %r3% $hIcon = $TB.CreateType(); $hWnd = $hIcon::GetConsoleWindow();
set d2 = %d1% echo $($hIcon::SendMessageW($hWnd, 127, 0, 0) -ne [IntPtr]::Zero);
if defined terminal (
%psc% " %d2% " %nul2% | find /i " True " %nul1% && set terminal =
)
if %_unattended% == 1 goto : skipQE
for %% # in ( %_args% ) do ( if /i " %% # " == " -qedit " goto : skipQE )
if defined terminal (
set " launchcmd=start conhost.exe %psc% "
) else (
set " launchcmd= %psc% "
2023-09-25 15:46:19 +02:00
)
2024-08-16 22:11:42 +02:00
: : Disable QuickEdit in current session
set " d1=$t=[AppDomain]::CurrentDomain.DefineDynamicAssembly(4, 1).DefineDynamicModule(2, $False).DefineType(0); "
set " d2=$t.DefinePInvokeMethod('GetStdHandle', 'kernel32.dll', 22, 1, [IntPtr], @([Int32]), 1, 3).SetImplementationFlags(128); "
set " d3=$t.DefinePInvokeMethod('SetConsoleMode', 'kernel32.dll', 22, 1, [Boolean], @([IntPtr], [Int32]), 1, 3).SetImplementationFlags(128); "
set " d4=$k=$t.CreateType(); $b=$k::SetConsoleMode($k::GetStdHandle(-10), 0x0080); "
%launchcmd% " %d1% %d2% %d3% %d4% & cmd.exe '/c' '!_PSarg! -qedit' " && ( exit /b) || ( set terminal = 1)
: skipQE
2023-09-25 15:46:19 +02:00
: :========================================================================================================================================
2023-10-16 04:50:03 +02:00
: : Check for updates
set - =
set old =
2023-10-21 02:01:26 +02:00
for /f " delims=[] tokens=2 " %% # in ( 'ping -4 -n 1 updatecheck.mass %-% grave.dev' ) do (
if not [%% #]== [] ( echo " %% # " | find " 127.69 " %nul1% && ( echo " %% # " | find " 127.69. %masver% " %nul1% || set old = 1) )
2023-10-16 04:50:03 +02:00
)
if defined old (
echo ________________________________________________
%eline%
2024-08-16 22:11:42 +02:00
echo Version %masver% of MAS is outdated.
2023-10-16 04:50:03 +02:00
echo ________________________________________________
echo :
2024-08-16 22:11:42 +02:00
if not %_unattended% == 1 (
2023-11-15 22:03:20 +01:00
echo [1] Get Latest MAS
2023-10-16 04:50:03 +02:00
echo [0] Continue Anyway
echo :
2024-08-16 22:11:42 +02:00
call : dk_color %_Green% " Enter a menu option in the Keyboard [1,0] : "
2023-10-16 04:50:03 +02:00
choice /C:10 /N
if !errorlevel! == 2 rem
if !errorlevel! == 1 ( start ht%-% tps://github.com/mass%-% gravel/Microsoft-Acti%-% vation-Scripts & start %mas% & exit /b)
)
2024-08-16 22:11:42 +02:00
)
2023-10-16 04:50:03 +02:00
: :========================================================================================================================================
2022-07-25 20:03:00 +02:00
setlocal DisableDelayedExpansion
: : Check desktop location
set desktop =
for /f " skip=2 tokens=2* " %% a in ( 'reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop' ) do call set " desktop= %% b "
if not defined desktop for /f " delims= " %% a in ( ' %psc% "& {write-host $([Environment]::GetFolderPath('Desktop' ) ) }" ') do call set " desktop=%% a"
if not defined desktop (
%eline%
echo Desktop location was not detected, aborting...
2024-08-16 22:11:42 +02:00
goto dk_done
2022-07-25 20:03:00 +02:00
)
setlocal EnableDelayedExpansion
: :========================================================================================================================================
: at_menu
cls
color 07
2023-10-16 04:50:03 +02:00
title Troubleshoot %masver%
2024-08-16 22:11:42 +02:00
if not defined terminal mode 77, 30
2022-07-25 20:03:00 +02:00
2023-03-16 19:11:04 +01:00
echo :
echo :
2022-07-25 20:03:00 +02:00
echo :
echo :
echo : _______________________________________________________________
echo :
2024-08-16 22:11:42 +02:00
call : dk_color2 %_White% " [1] " %_Green% " Help "
2022-11-07 21:33:01 +01:00
echo : ___________________________________________________
2022-07-25 20:03:00 +02:00
echo :
2022-11-07 21:33:01 +01:00
echo : [2] Dism RestoreHealth
echo : [3] SFC Scannow
2022-07-25 20:03:00 +02:00
echo :
2023-03-16 19:11:04 +01:00
echo : [4] Fix WMI
echo : [5] Fix Licensing
echo : [6] Fix WPA Registry
2022-11-07 21:33:01 +01:00
echo : ___________________________________________________
echo :
echo : [0] %_exitmsg%
2022-07-25 20:03:00 +02:00
echo : _______________________________________________________________
echo :
2024-08-16 22:11:42 +02:00
call : dk_color2 %_White% " " %_Green% " Enter a menu option in the Keyboard : "
2023-03-16 19:11:04 +01:00
choice /C:1234560 /N
2022-07-25 20:03:00 +02:00
set _erl = %errorlevel%
2023-03-16 19:11:04 +01:00
if %_erl% == 7 exit /b
2024-08-16 22:11:42 +02:00
if %_erl% == 6 start %mas% fix-wpa-registry & goto at_menu
2023-03-16 19:11:04 +01:00
if %_erl% == 5 goto : retokens
if %_erl% == 4 goto : fixwmi
2022-07-25 20:03:00 +02:00
if %_erl% == 3 goto : sfcscan
if %_erl% == 2 goto : dism_rest
2023-09-30 22:24:15 +02:00
if %_erl% == 1 start %mas% troubleshoot.html & goto at_menu
2022-07-25 20:03:00 +02:00
goto : at_menu
: :========================================================================================================================================
: dism_rest
cls
2024-08-16 22:11:42 +02:00
if not defined terminal mode 98, 30
2023-03-16 19:11:04 +01:00
title Dism /English /Online /Cleanup-Image /RestoreHealth
2022-07-25 20:03:00 +02:00
if %winbuild% LSS 9200 (
%eline%
echo Unsupported OS version Detected.
echo This command is supported only for Windows 8/8.1/10/11 and their Server equivalent.
goto : at_back
)
set _int =
2023-03-16 19:11:04 +01:00
for %% a in ( l.root-servers.net resolver1.opendns.com download.windowsupdate.com google.com) do if not defined _int (
for /f " delims=[] tokens=2 " %% # in ( 'ping -n 1 %% a' ) do ( if not [%% #]== [] set _int = 1)
)
2022-07-25 20:03:00 +02:00
echo :
if defined _int (
echo Checking Internet Connection [Connected]
) else (
2024-08-16 22:11:42 +02:00
call : dk_color2 %_White% " " %Red% " Checking Internet Connection [Not connected] "
2022-07-25 20:03:00 +02:00
)
echo %line%
echo :
echo Dism uses Windows Update to provide the files required to fix corruption.
echo This will take 5-15 minutes or more..
echo %line%
echo :
echo Notes:
echo :
2024-08-16 22:11:42 +02:00
call : dk_color2 %_White% " - " %Gray% " Make sure the Internet is connected. "
call : dk_color2 %_White% " - " %Gray% " Make sure the Windows update is properly working. "
2022-07-25 20:03:00 +02:00
echo :
echo %line%
echo :
2022-11-07 21:33:01 +01:00
choice /C:09 /N /M " > [9] Continue [0] Go back : "
2022-07-25 20:03:00 +02:00
if %errorlevel% == 1 goto at_menu
cls
2024-08-16 22:11:42 +02:00
if not defined terminal mode 110, 30
2024-08-16 22:58:32 +02:00
for /f %% a in ( ' %psc% "(Get-Date).ToString('yyyyMMdd-HHmmssfff' ) " ') do set _time= %% a
2023-11-15 22:03:20 +01:00
%psc% Stop-Service TrustedInstaller -force %nul%
2022-07-25 20:03:00 +02:00
2024-08-16 22:58:32 +02:00
copy /y /b " %SystemRoot% \logs\cbs\cbs.log " " %SystemRoot% \logs\cbs\backup_cbs_ %_time% .log " %nul%
copy /y /b " %SystemRoot% \logs\DISM\dism.log " " %SystemRoot% \logs\DISM\backup_dism_ %_time% .log " %nul%
del /f /q " %SystemRoot% \logs\cbs\cbs.log " %nul%
del /f /q " %SystemRoot% \logs\DISM\dism.log " %nul%
2022-07-25 20:03:00 +02:00
echo :
echo Applying the command,
2023-03-16 19:11:04 +01:00
echo dism /english /online /cleanup-image /restorehealth
2023-09-25 15:46:19 +02:00
dism /english /online /cleanup-image /restorehealth
2024-08-16 22:58:32 +02:00
timeout /t 5 %nul1%
copy /y /b " %SystemRoot% \logs\cbs\cbs.log " " %SystemRoot% \logs\cbs\cbs_ %_time% .log " %nul%
copy /y /b " %SystemRoot% \logs\DISM\dism.log " " %SystemRoot% \logs\DISM\dism_ %_time% .log " %nul%
2022-07-25 20:03:00 +02:00
if not exist " !desktop! \AT_Logs\ " md " !desktop! \AT_Logs\ " %nul%
2024-08-16 22:58:32 +02:00
call : compresslog cbs\cbs_%_time% .log AT_Logs\RHealth_CBS %nul%
call : compresslog DISM\dism_%_time% .log AT_Logs\RHealth_DISM %nul%
2023-09-25 15:46:19 +02:00
if not exist " !desktop! \AT_Logs\RHealth_CBS_ %_time% .cab " (
copy /y /b " %SystemRoot% \logs\cbs\cbs.log " " !desktop! \AT_Logs\RHealth_CBS_ %_time% .log " %nul%
)
if not exist " !desktop! \AT_Logs\RHealth_DISM_ %_time% .cab " (
copy /y /b " %SystemRoot% \logs\DISM\dism.log " " !desktop! \AT_Logs\RHealth_DISM_ %_time% .log " %nul%
)
2022-07-25 20:03:00 +02:00
echo :
2024-08-16 22:58:32 +02:00
call : dk_color %Gray% " CBS and DISM logs are copied to the AT_Logs folder on the desktop. "
2022-07-25 20:03:00 +02:00
goto : at_back
: :========================================================================================================================================
: sfcscan
cls
2024-08-16 22:11:42 +02:00
if not defined terminal mode 98, 30
2022-07-25 20:03:00 +02:00
title sfc /scannow
echo :
echo %line%
echo :
echo System File Checker will repair missing or corrupted system files.
echo This will take 10-15 minutes or more..
echo :
echo If SFC could not fix something, then run the command again to see if it may be able
echo to the next time. Sometimes it may take running the sfc /scannow command 3 times
echo restarting the PC after each time to completely fix everything that it's able to.
echo :
echo %line%
echo :
2022-11-07 21:33:01 +01:00
choice /C:09 /N /M " > [9] Continue [0] Go back : "
2022-07-25 20:03:00 +02:00
if %errorlevel% == 1 goto at_menu
cls
2024-08-16 22:58:32 +02:00
for /f %% a in ( ' %psc% "(Get-Date).ToString('yyyyMMdd-HHmmssfff' ) " ') do set _time= %% a
2023-11-15 22:03:20 +01:00
%psc% Stop-Service TrustedInstaller -force %nul%
2022-07-25 20:03:00 +02:00
2024-08-16 22:58:32 +02:00
copy /y /b " %SystemRoot% \logs\cbs\cbs.log " " %SystemRoot% \logs\cbs\backup_cbs_ %_time% .log " %nul%
del /f /q " %SystemRoot% \logs\cbs\cbs.log " %nul%
2022-07-25 20:03:00 +02:00
echo :
echo Applying the command,
echo sfc /scannow
sfc /scannow
2024-08-16 22:58:32 +02:00
timeout /t 5 %nul1%
copy /y /b " %SystemRoot% \logs\cbs\cbs.log " " %SystemRoot% \logs\cbs\cbs_ %_time% .log " %nul%
2023-09-25 15:46:19 +02:00
2022-07-25 20:03:00 +02:00
if not exist " !desktop! \AT_Logs\ " md " !desktop! \AT_Logs\ " %nul%
2024-08-16 22:58:32 +02:00
call : compresslog cbs\cbs_%_time% .log AT_Logs\SFC_CBS %nul%
2023-09-25 15:46:19 +02:00
if not exist " !desktop! \AT_Logs\SFC_CBS_ %_time% .cab " (
copy /y /b " %SystemRoot% \logs\cbs\cbs.log " " !desktop! \AT_Logs\SFC_CBS_ %_time% .log " %nul%
)
2022-07-25 20:03:00 +02:00
echo :
2024-08-16 22:58:32 +02:00
call : dk_color %Gray% " CBS log is copied to the AT_Logs folder on the desktop. "
2022-07-25 20:03:00 +02:00
goto : at_back
: :========================================================================================================================================
2023-03-16 19:11:04 +01:00
: retokens
2022-07-25 20:03:00 +02:00
cls
2024-08-16 22:11:42 +02:00
if not defined terminal mode 125, 32
2023-09-25 15:46:19 +02:00
%psc% " &{$W=$Host.UI.RawUI.WindowSize;$B=$Host.UI.RawUI.BufferSize;$W.Height=31;$B.Height=200;$Host.UI.RawUI.WindowSize=$W;$Host.UI.RawUI.BufferSize=$B;} "
2023-03-16 19:11:04 +01:00
title Fix Licensing ^( ClipSVC ^+ Office vNext ^+ SPP ^+ OSPP^)
2022-07-25 20:03:00 +02:00
echo :
echo %line%
echo :
2023-03-16 19:11:04 +01:00
echo Notes:
echo :
echo - It helps in troubleshooting activation issues.
echo :
echo - This option will,
echo - Deactivate Windows and Office, you may need to reactivate
2023-10-16 04:50:03 +02:00
echo If Windows is activated with motherboard / OEM / Digital license then don't worry
echo :
2023-03-16 19:11:04 +01:00
echo - Clear ClipSVC, Office vNext, SPP and OSPP licenses
echo - Fix SPP permissions of tokens folder and registries
echo - Trigger the repair option for Office.
echo :
2024-08-16 22:11:42 +02:00
call : dk_color2 %_White% " - " %Red% " Apply it only when it is necessary. "
2023-03-16 19:11:04 +01:00
echo :
2022-07-25 20:03:00 +02:00
echo %line%
echo :
2022-11-07 21:33:01 +01:00
choice /C:09 /N /M " > [9] Continue [0] Go back : "
2022-07-25 20:03:00 +02:00
if %errorlevel% == 1 goto at_menu
2023-03-16 19:11:04 +01:00
: :========================================================================================================================================
: : Rebuild ClipSVC Licences
2022-07-25 20:03:00 +02:00
cls
2023-03-16 19:11:04 +01:00
: cleanlicensing
echo :
echo %line%
echo :
2024-08-16 22:11:42 +02:00
call : dk_color %Blue% " Rebuilding ClipSVC Licences "
2023-03-16 19:11:04 +01:00
echo :
if %winbuild% LSS 10240 (
echo ClipSVC Licence rebuilding is supported only on Win 10/11 and Server equivalent.
echo Skipping...
goto : cleanvnext
)
2023-09-25 15:46:19 +02:00
%psc% " (([WMISEARCHER]'SELECT Name FROM SoftwareLicensingProduct WHERE LicenseStatus=1 AND GracePeriodRemaining=0 AND PartialProductKey IS NOT NULL').Get()).Name " %nul2% | findstr /i " Windows " %nul1% && (
2023-03-16 19:11:04 +01:00
echo Windows is permanently activated.
echo Skipping rebuilding ClipSVC licences...
goto : cleanvnext
)
echo Stopping ClipSVC service...
2023-11-15 22:03:20 +01:00
%psc% Stop-Service ClipSVC -force %nul%
2023-03-16 19:11:04 +01:00
timeout /t 2 %nul%
echo :
echo Applying the command to Clean ClipSVC Licences...
echo rundll32 clipc.dll,ClipCleanUpState
rundll32 clipc.dll,ClipCleanUpState
if %winbuild% LEQ 10240 (
echo [Successful]
) else (
if exist " %ProgramData% \Microsoft\Windows\ClipSVC\tokens.dat " (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " [Failed] "
2023-03-16 19:11:04 +01:00
) else (
echo [Successful]
)
)
: : Below registry key (Volatile & Protected) gets created after the ClipSVC License cleanup command, and gets automatically deleted after
: : system restart. It needs to be deleted to activate the system without restart.
set " RegKey=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ClipSVC\Volatile\PersistedSystemState "
set " _ident=HKU\S-1-5-19\SOFTWARE\Microsoft\IdentityCRL "
reg query " %RegKey% " %nul% && %nul% call :regownstart
reg delete " %RegKey% " /f %nul%
echo :
echo Deleting a Volatile ^& Protected Registry Key...
echo [%RegKey% ]
reg query " %RegKey% " %nul% && (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " [Failed] "
2023-03-16 19:11:04 +01:00
echo Restart the system, that will delete this registry key automatically.
) || (
echo [Successful]
)
: : Clear HWID token related registry to fix activation incase if there is any corruption
echo :
echo Deleting a IdentityCRL Registry Key...
echo [%_ident% ]
reg delete " %_ident% " /f %nul%
reg query " %_ident% " %nul% && (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " [Failed] "
2023-03-16 19:11:04 +01:00
) || (
echo [Successful]
)
2023-11-15 22:03:20 +01:00
%psc% Stop-Service ClipSVC -force %nul%
2023-03-16 19:11:04 +01:00
: : Rebuild ClipSVC folder to fix permission issues
echo :
if %winbuild% GTR 10240 (
echo Deleting Folder %ProgramData% \Microsoft\Windows\ClipSVC\
rmdir /s /q " C:\ProgramData\Microsoft\Windows\ClipSvc " %nul%
if exist " %ProgramData% \Microsoft\Windows\ClipSVC\ " (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " [Failed] "
2023-03-16 19:11:04 +01:00
) else (
echo [Successful]
)
echo :
echo Rebuilding Folder %ProgramData% \Microsoft\Windows\ClipSVC\
2023-11-15 22:03:20 +01:00
%psc% Start-Service ClipSVC %nul%
2023-03-16 19:11:04 +01:00
timeout /t 3 %nul%
if not exist " %ProgramData% \Microsoft\Windows\ClipSVC\ " timeout /t 5 %nul%
if not exist " %ProgramData% \Microsoft\Windows\ClipSVC\ " (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " [Failed] "
2023-03-16 19:11:04 +01:00
) else (
echo [Successful]
)
)
echo :
echo Restarting [wlidsvc LicenseManager] services...
2023-11-15 22:03:20 +01:00
for %% # in ( wlidsvc LicenseManager) do ( %psc% Restart-Service %% # %nul% )
2023-03-16 19:11:04 +01:00
: :========================================================================================================================================
2023-10-16 04:50:03 +02:00
: : Find remnants of Office vNext license block and remove it because it stops non vNext licenses from appearing
: : https://learn.microsoft.com/en-us/office/troubleshoot/activation/reset-office-365-proplus-activation-state
2023-03-16 19:11:04 +01:00
: cleanvnext
2022-07-25 20:03:00 +02:00
echo :
echo %line%
echo :
2024-08-16 22:11:42 +02:00
call : dk_color %Blue% " Clearing Office vNext License "
2022-07-25 20:03:00 +02:00
echo :
setlocal DisableDelayedExpansion
2022-11-07 21:33:01 +01:00
set " _Local= %LocalAppData% "
2022-07-25 20:03:00 +02:00
setlocal EnableDelayedExpansion
2022-11-07 21:33:01 +01:00
attrib -R " !ProgramData! \Microsoft\Office\Licenses " %nul%
attrib -R " !_Local! \Microsoft\Office\Licenses " %nul%
2023-03-16 19:11:04 +01:00
if exist " !ProgramData! \Microsoft\Office\Licenses\ " (
rd /s /q " !ProgramData! \Microsoft\Office\Licenses\ " %nul%
2022-11-07 21:33:01 +01:00
if exist " !ProgramData! \Microsoft\Office\Licenses\ " (
echo Failed To Delete - !ProgramData! \Microsoft\Office\Licenses\
) else (
echo Deleted Folder - !ProgramData! \Microsoft\Office\Licenses\
)
2023-03-16 19:11:04 +01:00
) else (
echo Not Found - !ProgramData! \Microsoft\Office\Licenses\
)
2022-11-07 21:33:01 +01:00
2023-03-16 19:11:04 +01:00
if exist " !_Local! \Microsoft\Office\Licenses\ " (
rd /s /q " !_Local! \Microsoft\Office\Licenses\ " %nul%
2022-11-07 21:33:01 +01:00
if exist " !_Local! \Microsoft\Office\Licenses\ " (
echo Failed To Delete - !_Local! \Microsoft\Office\Licenses\
) else (
echo Deleted Folder - !_Local! \Microsoft\Office\Licenses\
)
2023-03-16 19:11:04 +01:00
) else (
echo Not Found - !_Local! \Microsoft\Office\Licenses\
)
2022-07-25 20:03:00 +02:00
2023-10-16 04:50:03 +02:00
2022-07-25 20:03:00 +02:00
echo :
2023-11-15 22:03:20 +01:00
for /f " tokens=* delims= " %% a in ( ' %psc% "Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' | ForEach-Object { Split-Path -Path $_.PSPath -Leaf }" %nul6% ' ) do ( if defined _sid ( set " _sid=!_sid! HKU\ %% a " ) else ( set " _sid=HKU\ %% a " ) )
2023-10-16 04:50:03 +02:00
set regfound =
for %% # in ( HKCU !_sid! ) do (
for %% A in (
%% #\Software\Microsoft\Office\16.0\Common\Licensing
%% #\Software\Microsoft\Office\16.0\Common\Identity
%% #\Software\Microsoft\Office\16.0\Registration
2022-07-25 20:03:00 +02:00
) do (
2023-10-16 04:50:03 +02:00
reg query %% A %nul% && (
set regfound = 1
reg delete %% A /f %nul% && (
echo Deleted Registry - %% A
2022-07-25 20:03:00 +02:00
) || (
2023-10-16 04:50:03 +02:00
echo Failed to Delete - %% A
)
2022-07-25 20:03:00 +02:00
)
)
)
2023-10-16 04:50:03 +02:00
if not defined regfound echo Not Found - Office vNext Registry Keys
2022-07-25 20:03:00 +02:00
: :========================================================================================================================================
2023-03-16 19:11:04 +01:00
: : Rebuild SPP Tokens
2022-07-25 20:03:00 +02:00
echo :
echo %line%
echo :
2024-08-16 22:11:42 +02:00
call : dk_color %Blue% " Rebuilding SPP Licensing Tokens "
2022-07-25 20:03:00 +02:00
echo :
call : scandat check
if not defined token (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " tokens.dat file not found. "
2022-07-25 20:03:00 +02:00
) else (
echo tokens.dat file: [%token% ]
)
2023-03-16 19:11:04 +01:00
echo :
2023-11-15 22:03:20 +01:00
set wpainfo =
for /f " delims= " %% a in ( ' %psc% "$f=[io.file]::ReadAllText('!_batp!' ) -split ':wpatest\:.*';iex ($f[1]);" %nul6% ' ) do ( set wpainfo = %% a)
echo " %wpainfo% " | find /i " Error Found " %nul% && (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " WPA Registry Error: %wpainfo% "
2023-11-15 22:03:20 +01:00
) || (
echo WPA Registry Count: %wpainfo%
2023-03-16 19:11:04 +01:00
)
set tokenstore =
2023-09-25 15:46:19 +02:00
for /f " skip=2 tokens=2* " %% a in ( 'reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform" /v TokenStore %nul6% ' ) do call set " tokenstore= %% b "
2023-03-16 19:11:04 +01:00
: : Check sppsvc permissions and apply fixes
if %winbuild% GEQ 10240 (
echo :
echo Checking SPP permission related issues...
call : checkperms
if defined permerror (
mkdir " %tokenstore% " %nul%
set " d=$sddl = 'O:BAG:BAD:PAI(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICIIO;GR;;;BU)(A;;FR;;;BU)(A;OICI;FA;;;S-1-5-80-123231216-2592883651-3715271367-3753151631-4175906628)'; "
set " d=!d! $AclObject = New-Object System.Security.AccessControl.DirectorySecurity; "
set " d=!d! $AclObject.SetSecurityDescriptorSddlForm($sddl); "
set " d=!d! Set-Acl -Path %tokenstore% -AclObject $AclObject; "
%psc% " !d! " %nul%
for %% # in (
" HKLM:\SYSTEM\WPA_QueryValues, EnumerateSubKeys, WriteKey "
" HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform_SetValue "
) do for /f "tokens=1,2 delims=_" %%A in (%%#) do (
set " d=$acl = Get-Acl ' %% A'; "
set " d=!d! $rule = New-Object System.Security.AccessControl.RegistryAccessRule ('NT Service\sppsvc', ' %% B', 'ContainerInherit, ObjectInherit','None','Allow'); "
set " d=!d! $acl.ResetAccessRule($rule); "
set " d=!d! $acl.SetAccessRule($rule); "
set " d=!d! Set-Acl -Path ' %% A' -AclObject $acl "
%psc% " !d! " %nul%
)
call : checkperms
if defined permerror (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " [Failed To Fix] "
2023-03-16 19:11:04 +01:00
) else (
echo [Successfully Fixed]
)
) else (
2023-09-25 15:46:19 +02:00
echo [No Error Found]
2023-03-16 19:11:04 +01:00
)
)
2022-07-25 20:03:00 +02:00
echo :
echo Stopping sppsvc service...
2023-11-15 22:03:20 +01:00
%psc% Stop-Service sppsvc -force %nul%
2022-07-25 20:03:00 +02:00
echo :
call : scandat delete
call : scandat check
if defined token (
echo :
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " Failed to delete .dat files. "
2022-07-25 20:03:00 +02:00
echo :
)
echo :
echo Reinstalling System Licenses [slmgr /rilc]...
cscript //nologo %windir% \system32\slmgr.vbs /rilc %nul%
if %errorlevel% NEQ 0 cscript //nologo %windir% \system32\slmgr.vbs /rilc %nul%
if %errorlevel% EQU 0 (
echo [Successful]
) else (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " [Failed] "
2022-07-25 20:03:00 +02:00
)
call : scandat check
echo :
if not defined token (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " Failed to rebuilt tokens.dat file. "
2022-07-25 20:03:00 +02:00
) else (
2023-03-16 19:11:04 +01:00
echo tokens.dat file was rebuilt successfully.
2022-07-25 20:03:00 +02:00
)
: :========================================================================================================================================
: : Rebuild OSPP Tokens
echo :
echo %line%
echo :
2024-08-16 22:11:42 +02:00
call : dk_color %Blue% " Rebuilding OSPP Licensing Tokens "
2023-03-16 19:11:04 +01:00
echo :
2022-07-25 20:03:00 +02:00
sc qc osppsvc %nul% || (
2023-03-16 19:11:04 +01:00
echo OSPP based Office is not installed
echo Skipping rebuilding OSPP tokens...
2022-11-07 21:33:01 +01:00
goto : repairoffice
2022-07-25 20:03:00 +02:00
)
call : scandatospp check
if not defined token (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " tokens.dat file not found. "
2022-07-25 20:03:00 +02:00
) else (
echo tokens.dat file: [%token% ]
)
echo :
echo Stopping osppsvc service...
2023-11-15 22:03:20 +01:00
%psc% Stop-Service osppsvc -force %nul%
2022-07-25 20:03:00 +02:00
echo :
call : scandatospp delete
call : scandatospp check
if defined token (
echo :
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " Failed to delete .dat files. "
2022-07-25 20:03:00 +02:00
echo :
)
echo :
echo Starting osppsvc service to generate tokens.dat
2023-11-15 22:03:20 +01:00
%psc% Start-Service osppsvc %nul%
2022-07-25 20:03:00 +02:00
call : scandatospp check
if not defined token (
2023-11-15 22:03:20 +01:00
%psc% Stop-Service osppsvc -force %nul%
%psc% Start-Service osppsvc %nul%
2022-07-25 20:03:00 +02:00
timeout /t 3 %nul%
)
call : scandatospp check
echo :
if not defined token (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " Failed to rebuilt tokens.dat file. "
2022-07-25 20:03:00 +02:00
) else (
2023-03-16 19:11:04 +01:00
echo tokens.dat file was rebuilt successfully.
2022-07-25 20:03:00 +02:00
)
: :========================================================================================================================================
: repairoffice
echo :
echo %line%
echo :
2024-08-16 22:11:42 +02:00
call : dk_color %Blue% " Repairing Office Licenses "
2022-07-25 20:03:00 +02:00
echo :
for /f " skip=2 tokens=2* " %% a in ( 'reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PROCESSOR_ARCHITECTURE' ) do set arch = %% b
if /i " %arch% " == " ARM64 " (
echo :
echo ARM64 Windows Found.
echo You need to use repair option in Windows settings for Office.
echo :
start ms-settings:appsfeatures
goto : repairend
)
if /i " %arch% " == " x86 " (
set arch = X86
) else (
set arch = X64
)
for %% # in ( 68 86) do (
for %% A in ( msi14 msi15 msi16 c2r14 c2r15 c2r16) do ( set %% A_ %% # = & set %% Arepair %% # = )
)
set _68 = HKLM\SOFTWARE\Microsoft\Office
set _86 = HKLM\SOFTWARE\Wow6432Node\Microsoft\Office
2023-11-15 22:03:20 +01:00
reg query %_68% \14.0\CVH /f Click2run /k %nul% && ( set " c2r14_68=Office 14.0 C2R x86/x64 " & set " c2r14repair68= " )
reg query %_86% \14.0\CVH /f Click2run /k %nul% && ( set " c2r14_86=Office 14.0 C2R x86 " & set " c2r14repair86= " )
for /f " skip=2 tokens=2* " %% a in ( '"reg query %_86% \14.0\Common\InstallRoot /v Path" %nul6% ' ) do if exist " %% b\EntityPicker.dll " ( set " msi14_86=Office 14.0 MSI x86 " & set " msi14repair86= %systemdrive% \Program Files (x86)\Common Files\Microsoft Shared\OFFICE14\Office Setup Controller\Setup.exe " )
for /f " skip=2 tokens=2* " %% a in ( '"reg query %_68% \14.0\Common\InstallRoot /v Path" %nul6% ' ) do if exist " %% b\EntityPicker.dll " ( set " msi14_68=Office 14.0 MSI x86/x64 " & set " msi14repair68= %systemdrive% \Program Files\Common Files\microsoft shared\OFFICE14\Office Setup Controller\Setup.exe " )
for /f " skip=2 tokens=2* " %% a in ( '"reg query %_86% \15.0\Common\InstallRoot /v Path" %nul6% ' ) do if exist " %% b\EntityPicker.dll " ( set " msi15_86=Office 15.0 MSI x86 " & set " msi15repair86= %systemdrive% \Program Files (x86)\Common Files\Microsoft Shared\OFFICE15\Office Setup Controller\Setup.exe " )
for /f " skip=2 tokens=2* " %% a in ( '"reg query %_68% \15.0\Common\InstallRoot /v Path" %nul6% ' ) do if exist " %% b\EntityPicker.dll " ( set " msi15_68=Office 15.0 MSI x86/x64 " & set " msi15repair68= %systemdrive% \Program Files\Common Files\microsoft shared\OFFICE15\Office Setup Controller\Setup.exe " )
for /f " skip=2 tokens=2* " %% a in ( '"reg query %_86% \16.0\Common\InstallRoot /v Path" %nul6% ' ) do if exist " %% b\EntityPicker.dll " ( set " msi16_86=Office 16.0 MSI x86 " & set " msi16repair86= %systemdrive% \Program Files (x86)\Common Files\Microsoft Shared\OFFICE16\Office Setup Controller\Setup.exe " )
for /f " skip=2 tokens=2* " %% a in ( '"reg query %_68% \16.0\Common\InstallRoot /v Path" %nul6% ' ) do if exist " %% b\EntityPicker.dll " ( set " msi16_68=Office 16.0 MSI x86/x64 " & set " msi16repair68= %systemdrive% \Program Files\Common Files\Microsoft Shared\OFFICE16\Office Setup Controller\Setup.exe " )
for /f " skip=2 tokens=2* " %% a in ( '"reg query %_86% \15.0\ClickToRun /v InstallPath" %nul6% ' ) do if exist " %% b\root\Licenses\ProPlus*.xrm-ms " ( set " c2r15_86=Office 15.0 C2R x86 " & set " c2r15repair86= %systemdrive% \Program Files\Microsoft Office 15\Client %arch% \integratedoffice.exe " )
for /f " skip=2 tokens=2* " %% a in ( '"reg query %_68% \15.0\ClickToRun /v InstallPath" %nul6% ' ) do if exist " %% b\root\Licenses\ProPlus*.xrm-ms " ( set " c2r15_68=Office 15.0 C2R x86/x64 " & set " c2r15repair68= %systemdrive% \Program Files\Microsoft Office 15\Client %arch% \integratedoffice.exe " )
for /f " skip=2 tokens=2* " %% a in ( '"reg query %_86% \ClickToRun /v InstallPath" %nul6% ' ) do if exist " %% b\root\Licenses16\ProPlus*.xrm-ms " ( set " c2r16_86=Office 16.0 C2R x86 " & set " c2r16repair86= %systemdrive% \Program Files\Microsoft Office 15\Client %arch% \OfficeClickToRun.exe " )
for /f " skip=2 tokens=2* " %% a in ( '"reg query %_68% \ClickToRun /v InstallPath" %nul6% ' ) do if exist " %% b\root\Licenses16\ProPlus*.xrm-ms " ( set " c2r16_68=Office 16.0 C2R x86/x64 " & set " c2r16repair68= %systemdrive% \Program Files\Microsoft Office 15\Client %arch% \OfficeClickToRun.exe " )
2022-07-25 20:03:00 +02:00
set uwp16 =
2022-11-07 21:33:01 +01:00
if %winbuild% GEQ 10240 (
2023-09-25 15:46:19 +02:00
%psc% " Get-AppxPackage -name " Microsoft.Office.Desktop" " | find /i " Office " %nul1% && set uwp16 = Office 16.0 UWP
2022-07-25 20:03:00 +02:00
)
set /a counter = 0
echo Checking installed Office versions...
echo :
for %% # in (
" %msi14_68% "
" %msi14_86% "
" %msi15_68% "
" %msi15_86% "
" %msi16_68% "
" %msi16_86% "
" %c2r14_68% "
" %c2r14_86% "
" %c2r15_68% "
" %c2r15_86% "
" %c2r16_68% "
" %c2r16_86% "
" %uwp16% "
) do (
if not " %% # " == " " " " (
set insoff = %% #
set insoff = !insoff:"=!
echo [!insoff!]
set /a counter += 1
)
)
if %counter% GTR 1 (
%eline%
echo Multiple office versions found.
echo It's recommended to install only one version of office.
echo ________________________________________________________________
echo :
)
if %counter% EQU 0 (
echo :
echo Installed Office is not found.
goto : repairend
echo :
) else (
echo :
2024-08-16 22:11:42 +02:00
call : dk_color %_Yellow% " A Window will popup, in that Window you need to select [Quick] Repair Option... "
call : dk_color %_Yellow% " Press any key to continue... "
2022-07-25 20:03:00 +02:00
echo :
2023-09-25 15:46:19 +02:00
pause %nul1%
2022-07-25 20:03:00 +02:00
)
if defined uwp16 (
echo :
echo Note: Skipping repair for Office 16.0 UWP.
echo You need to use reset option in Windows settings for it.
echo ________________________________________________________________
echo :
start ms-settings:appsfeatures
)
set c2r14 =
if defined c2r14_68 set c2r14 = 1
if defined c2r14_86 set c2r14 = 1
if defined c2r14 (
echo :
echo Note: Skipping repair for Office 14.0 C2R
echo You need to use Repair option in Windows settings for it.
echo ________________________________________________________________
echo :
start appwiz.cpl
)
if defined msi14_68 if exist " %msi14repair68% " echo Running - " %msi14repair68% " & " %msi14repair68% "
if defined msi14_86 if exist " %msi14repair86% " echo Running - " %msi14repair86% " & " %msi14repair86% "
if defined msi15_68 if exist " %msi15repair68% " echo Running - " %msi15repair68% " & " %msi15repair68% "
if defined msi15_86 if exist " %msi15repair86% " echo Running - " %msi15repair86% " & " %msi15repair86% "
if defined msi16_68 if exist " %msi16repair68% " echo Running - " %msi16repair68% " & " %msi16repair68% "
if defined msi16_86 if exist " %msi16repair86% " echo Running - " %msi16repair86% " & " %msi16repair86% "
if defined c2r15_68 if exist " %c2r15repair68% " echo Running - " %c2r15repair68% " REPAIRUI RERUNMODE & " %c2r15repair68% " REPAIRUI RERUNMODE
if defined c2r15_86 if exist " %c2r15repair86% " echo Running - " %c2r15repair86% " REPAIRUI RERUNMODE & " %c2r15repair86% " REPAIRUI RERUNMODE
if defined c2r16_68 if exist " %c2r16repair68% " echo Running - " %c2r16repair68% " scenario=Repair & " %c2r16repair68% " scenario=Repair
if defined c2r16_86 if exist " %c2r16repair86% " echo Running - " %c2r16repair86% " scenario=Repair & " %c2r16repair86% " scenario=Repair
: repairend
echo :
echo %line%
echo :
echo :
2024-08-16 22:11:42 +02:00
call : dk_color %Green% " Finished "
2022-11-07 21:33:01 +01:00
goto : at_back
: :========================================================================================================================================
2023-03-16 19:11:04 +01:00
: fixwmi
2022-11-07 21:33:01 +01:00
cls
2024-08-16 22:11:42 +02:00
if not defined terminal mode 98, 34
2023-03-16 19:11:04 +01:00
title Fix WMI
2022-11-07 21:33:01 +01:00
2023-03-16 19:11:04 +01:00
: : https://techcommunity.microsoft.com/t5/ask-the-performance-team/wmi-repository-corruption-or-not/ba-p/375484
2022-11-07 21:33:01 +01:00
2023-03-16 19:11:04 +01:00
if exist " %SystemRoot% \Servicing\Packages\Microsoft-Windows-Server*Edition~*.mum " (
2022-11-07 21:33:01 +01:00
%eline%
2023-03-16 19:11:04 +01:00
echo WMI rebuild is not recommended on Windows Server. Aborting...
2022-11-07 21:33:01 +01:00
goto : at_back
)
2023-03-16 19:11:04 +01:00
for %% # in ( wmic.exe) do @ if " %% ~$PATH:# " == " " (
2022-11-07 21:33:01 +01:00
%eline%
2023-03-16 19:11:04 +01:00
echo wmic.exe file is not found in the system. Aborting...
2022-11-07 21:33:01 +01:00
goto : at_back
)
echo :
2023-03-16 19:11:04 +01:00
echo Checking WMI
2023-09-25 15:46:19 +02:00
call : checkwmi
2022-11-07 21:33:01 +01:00
2023-09-25 15:46:19 +02:00
: : Apply basic fix first and check
if defined error (
2023-11-15 22:03:20 +01:00
%psc% Stop-Service Winmgmt -force %nul%
2023-09-25 15:46:19 +02:00
winmgmt /salvagerepository %nul%
call : checkwmi
)
2022-11-07 21:33:01 +01:00
2023-03-16 19:11:04 +01:00
if not defined error (
echo [Working]
echo No need to apply this option. Aborting...
goto : at_back
2022-11-07 21:33:01 +01:00
)
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " [Not Responding] "
2022-11-07 21:33:01 +01:00
2023-09-25 15:46:19 +02:00
set _corrupt =
sc start Winmgmt %nul%
if %errorlevel% EQU 1060 set _corrupt = 1
sc query Winmgmt %nul% || set _corrupt = 1
for %% G in ( DependOnService Description DisplayName ErrorControl ImagePath ObjectName Start Type) do if not defined _corrupt ( reg query HKLM\SYSTEM\CurrentControlSet\Services\Winmgmt /v %% G %nul% || set _corrupt = 1)
2022-11-07 21:33:01 +01:00
echo :
2023-09-25 15:46:19 +02:00
if defined _corrupt (
2022-11-07 21:33:01 +01:00
%eline%
2023-09-25 15:46:19 +02:00
echo Winmgmt service is corrupted. Aborting...
2022-11-07 21:33:01 +01:00
goto : at_back
)
2023-03-16 19:11:04 +01:00
echo Disabling Winmgmt service
2022-11-07 21:33:01 +01:00
sc config Winmgmt start= disabled %nul%
if %errorlevel% EQU 0 (
2023-03-16 19:11:04 +01:00
echo [Successful]
2022-11-07 21:33:01 +01:00
) else (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " [Failed] Aborting... "
2023-03-16 19:11:04 +01:00
sc config Winmgmt start= auto %nul%
goto : at_back
2022-11-07 21:33:01 +01:00
)
echo :
2023-03-16 19:11:04 +01:00
echo Stopping Winmgmt service
2023-11-15 22:03:20 +01:00
%psc% Stop-Service Winmgmt -force %nul%
%psc% Stop-Service Winmgmt -force %nul%
%psc% Stop-Service Winmgmt -force %nul%
2023-09-25 15:46:19 +02:00
sc query Winmgmt | find /i " STOPPED " %nul% && (
2023-03-16 19:11:04 +01:00
echo [Successful]
2022-11-07 21:33:01 +01:00
) || (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " [Failed] "
2023-03-16 19:11:04 +01:00
echo :
2024-08-16 22:11:42 +02:00
call : dk_color %Blue% " Its recommended to select [Restart] option and then apply Fix WMI option again. "
2023-03-16 19:11:04 +01:00
echo %line%
echo :
choice /C:21 /N /M " > [1] Restart [2] Revert Back Changes : "
if !errorlevel! == 1 ( sc config Winmgmt start= auto %nul% & goto : at_back )
echo :
echo Restarting...
shutdown -t 5 -r
exit
2022-11-07 21:33:01 +01:00
)
echo :
2023-03-16 19:11:04 +01:00
echo Deleting WMI repository
rmdir /s /q " %windir% \System32\wbem\repository\ " %nul%
2022-11-07 21:33:01 +01:00
if exist " %windir% \System32\wbem\repository\ " (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " [Failed] "
2022-11-07 21:33:01 +01:00
) else (
2023-03-16 19:11:04 +01:00
echo [Successful]
2022-11-07 21:33:01 +01:00
)
echo :
2023-03-16 19:11:04 +01:00
echo Enabling Winmgmt service
2022-11-07 21:33:01 +01:00
sc config Winmgmt start= auto %nul%
if %errorlevel% EQU 0 (
2023-03-16 19:11:04 +01:00
echo [Successful]
2022-11-07 21:33:01 +01:00
) else (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " [Failed] "
2022-11-07 21:33:01 +01:00
)
2023-09-25 15:46:19 +02:00
call : checkwmi
if not defined error (
2022-11-07 21:33:01 +01:00
echo :
2023-03-16 19:11:04 +01:00
echo Checking WMI
2024-08-16 22:11:42 +02:00
call : dk_color %Green% " [Working] "
2022-11-07 21:33:01 +01:00
goto : at_back
2023-03-16 19:11:04 +01:00
)
2022-11-07 21:33:01 +01:00
2023-03-16 19:11:04 +01:00
echo :
echo Registering .dll's and Compiling .mof's, .mfl's
call : registerobj %nul%
2022-11-07 21:33:01 +01:00
echo :
2023-03-16 19:11:04 +01:00
echo Checking WMI
2023-09-25 15:46:19 +02:00
call : checkwmi
if defined error (
2024-08-16 22:11:42 +02:00
call : dk_color %Red% " [Not Responding] "
2023-03-16 19:11:04 +01:00
echo :
echo Run [Dism RestoreHealth] and [SFC Scannow] options and make sure there are no errors.
2022-11-07 21:33:01 +01:00
) else (
2024-08-16 22:11:42 +02:00
call : dk_color %Green% " [Working] "
2022-11-07 21:33:01 +01:00
)
goto : at_back
2023-03-16 19:11:04 +01:00
: registerobj
2022-11-07 21:33:01 +01:00
2023-03-16 19:11:04 +01:00
: : https://eskonr.com/2012/01/how-to-fix-wmi-issues-automatically/
2022-11-07 21:33:01 +01:00
2023-11-15 22:03:20 +01:00
%psc% Stop-Service Winmgmt -force %nul%
2023-03-16 19:11:04 +01:00
cd /d %systemroot% \system32\wbem\
regsvr32 /s %systemroot% \system32\scecli.dll
regsvr32 /s %systemroot% \system32\userenv.dll
mofcomp cimwin32.mof
mofcomp cimwin32.mfl
mofcomp rsop.mof
mofcomp rsop.mfl
for /f %% s in ( 'dir /b /s *.dll' ) do regsvr32 /s %% s
for /f %% s in ( 'dir /b *.mof' ) do mofcomp %% s
for /f %% s in ( 'dir /b *.mfl' ) do mofcomp %% s
winmgmt /salvagerepository
winmgmt /resetrepository
exit /b
2022-07-25 20:03:00 +02:00
2023-09-25 15:46:19 +02:00
: checkwmi
: : https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-error-constants
set error =
wmic path Win32_ComputerSystem get CreationClassName /value %nul2% | find /i " computersystem " %nul1%
if %errorlevel% NEQ 0 ( set error = 1& exit /b)
winmgmt /verifyrepository %nul%
if %errorlevel% NEQ 0 ( set error = 1& exit /b)
cscript //nologo %windir% \system32\slmgr.vbs /dlv %nul%
cmd /c exit /b %errorlevel%
echo " 0x %=ExitCode% " | findstr /i " 0x800410 0x800440 " %nul1%
if %errorlevel% EQU 0 set error = 1
exit /b
2022-07-25 20:03:00 +02:00
: :========================================================================================================================================
: at_back
echo :
echo %line%
echo :
2024-08-16 22:11:42 +02:00
call : dk_color %_Yellow% " Press any key to go back... "
2023-09-25 15:46:19 +02:00
pause %nul1%
2022-07-25 20:03:00 +02:00
goto : at_menu
: :========================================================================================================================================
2023-09-25 15:46:19 +02:00
: compresslog
: : https://stackoverflow.com/a/46268232
set " ddf= " %SystemRoot% \Temp\ddf" "
%nul% del /q /f %ddf%
echo /.New Cabinet> %ddf%
echo /.set Cabinet=ON>> %ddf%
echo /.set CabinetFileCountThreshold=0;>> %ddf%
echo /.set Compress=ON>> %ddf%
echo /.set CompressionType=LZX>> %ddf%
echo /.set CompressionLevel=7;>> %ddf%
echo /.set CompressionMemory=21;>> %ddf%
echo /.set FolderFileCountThreshold=0;>> %ddf%
echo /.set FolderSizeThreshold=0;>> %ddf%
echo /.set GenerateInf=OFF>> %ddf%
echo /.set InfFileName=nul>> %ddf%
echo /.set MaxCabinetSize=0;>> %ddf%
echo /.set MaxDiskFileCount=0;>> %ddf%
echo /.set MaxDiskSize=0;>> %ddf%
echo /.set MaxErrors=1;>> %ddf%
echo /.set RptFileName=nul>> %ddf%
echo /.set UniqueFiles=ON>> %ddf%
for /f " tokens=* delims= " %% D in ( 'dir /a:-D/b/s " %SystemRoot% \logs\ %1 "' ) do (
echo /" %% ~fD " /inf=no;>> %ddf%
)
2024-08-16 22:58:32 +02:00
makecab /F %ddf% /D DiskDirectory1=" " /D CabinetNameTemplate=" !desktop! \ %2 _ %_time% .cab "
2023-09-25 15:46:19 +02:00
del /q /f %ddf%
2022-07-25 20:03:00 +02:00
exit /b
: :========================================================================================================================================
2023-11-15 22:03:20 +01:00
: : This code checks for invalid registry keys in HKLM\SYSTEM\WPA. This issue may appear even on healthy systems
: wpatest :
$wpaKey = [Microsoft.Win32.RegistryKey]::OpenBaseKey('LocalMachine', 'Registry64').OpenSubKey(" SYSTEM\\WPA " )
$count = $wpaKey.SubKeyCount
$osVersion = [System.Environment]::OSVersion.Version
$minBuildNumber = 14393
if ($osVersion.Build - g e $ m i n B u i l d N u m b e r ) {
$subkeyHashTable = @{}
foreach ($subkeyName in $wpaKey.GetSubKeyNames()) {
$keyNumber = $subkeyName -replace '.*-', ''
$subkeyHashTable[$keyNumber] = $true
}
for ($i=1; $i -le $count; $i++) {
if (-not $ s u b k e y H a s h T a b l e . C o n t a i n s K e y ( " $ i " ) ) {
Write-Host " Total Keys $count. Error Found- $i key does not exist "
$wpaKey.Close()
exit
}
}
}
$wpaKey.GetSubKeyNames() | ForEach-Object {
$subkey = $wpaKey.OpenSubKey($_)
$p = $subkey.GetValueNames()
if (($p | W h e r e - O b j e c t { $ s u b k e y . G e t V a l u e K i n d ( $ _ ) - e q [ M i c r o s o f t . W i n 3 2 . R e g i s t r y V a l u e K i n d ] : : B i n a r y } ) . C o u n t - e q 0 ) {
Write-Host " Total Keys $count. Error Found- Binary Data is corrupt "
$wpaKey.Close()
exit
}
}
$count
$wpaKey.Close()
: wpatest :
2022-07-25 20:03:00 +02:00
: :========================================================================================================================================
2023-03-16 19:11:04 +01:00
: checkperms
set permerror =
if not exist " %tokenstore% \ " set permerror = 1
for %% # in (
" %tokenstore% "
" HKLM:\SYSTEM\WPA "
" HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform "
) do if not defined permerror (
%psc% " $acl = Get-Acl ' %% #'; if ($acl.Access.Where{ $_.IdentityReference -eq 'NT SERVICE\sppsvc' -and $_.AccessControlType -eq 'Deny' -or $acl.Access.IdentityReference -notcontains 'NT SERVICE\sppsvc'}) {Exit 2} " %nul%
if !errorlevel! == 2 set permerror = 1
)
exit /b
: :========================================================================================================================================
2022-07-25 20:03:00 +02:00
: scandat
set token =
for %% # in (
%Systemdrive% \Windows\System32\spp\store_test\2.0\
%Systemdrive% \Windows\System32\spp\store\
%Systemdrive% \Windows\System32\spp\store\2.0\
%Systemdrive% \Windows\ServiceProfiles\NetworkService\AppData\Roaming\Microsoft\SoftwareProtectionPlatform\
) do (
if %1 == check (
if exist %% #tokens.dat set token = %% #tokens.dat
)
if %1 == delete (
if exist %% # (
%nul% dir /a-d /s " %% #*.dat " && (
attrib -r -s -h " %% #*.dat " /S
del /S /F /Q " %% #*.dat "
)
)
)
)
exit /b
: scandatospp
set token =
for %% # in (
%ProgramData% \Microsoft\OfficeSoftwareProtectionPlatform\
) do (
if %1 == check (
if exist %% #tokens.dat set token = %% #tokens.dat
)
if %1 == delete (
if exist %% # (
%nul% dir /a-d /s " %% #*.dat " && (
attrib -r -s -h " %% #*.dat " /S
del /S /F /Q " %% #*.dat "
)
)
)
)
exit /b
2022-11-07 21:33:01 +01:00
: :========================================================================================================================================
: regownstart
%psc% " $f=[io.file]::ReadAllText('!_batp!') -split ':regown\:.*';iex ($f[1]); "
exit /b
: : Below code takes ownership of a volatile registry key and deletes it
: : HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ClipSVC\Volatile\PersistedSystemState
: regown :
2023-03-16 19:11:04 +01:00
$AssemblyBuilder = [AppDomain]::CurrentDomain.DefineDynamicAssembly(4, 1)
$ModuleBuilder = $AssemblyBuilder.DefineDynamicModule(2, $False)
$TypeBuilder = $ModuleBuilder.DefineType(0)
$TypeBuilder.DefinePInvokeMethod('RtlAdjustPrivilege', 'ntdll.dll', 'Public, Static', 1, [int], @([int], [bool], [bool], [bool].MakeByRefType()), 1, 3) | Out-Null
$TypeBuilder.CreateType()::RtlAdjustPrivilege(9, $true, $false, [ref]$false) | Out-Null
2022-11-07 21:33:01 +01:00
$SID = New-Object System.Security.Principal.SecurityIdentifier('S-1-5-32-544')
$IDN = ($SID.Translate([System.Security.Principal.NTAccount])).Value
$Admin = New-Object System.Security.Principal.NTAccount($IDN)
$path = 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\ClipSVC\Volatile\PersistedSystemState'
$key = [Microsoft.Win32.RegistryKey]::OpenBaseKey('LocalMachine', 'Registry64').OpenSubKey($path, 'ReadWriteSubTree', 'takeownership')
$acl = $key.GetAccessControl()
$acl.SetOwner($Admin)
$key.SetAccessControl($acl)
$rule = New-Object System.Security.AccessControl.RegistryAccessRule($Admin," FullControl " ," Allow " )
$acl.SetAccessRule($rule)
$key.SetAccessControl($acl)
: regown :
: :========================================================================================================================================
2022-07-25 20:03:00 +02:00
2024-08-16 22:11:42 +02:00
: dk_done
echo :
if defined fixes (
call : dk_color2 %Blue% " Press [1] To Open Troubleshoot Page " %Gray% " Press [0] To Ignore "
choice /C:10 /N
if !errorlevel! == 1 ( for %% # in ( %fixes% ) do ( start %% #) )
)
if defined terminal (
call : dk_color %_Yellow% " Press 0 key to %_exitmsg% ... "
choice /c 0 /n
) else (
call : dk_color %_Yellow% " Press any key to %_exitmsg% ... "
pause %nul1%
)
exit /b
: :========================================================================================================================================
: dk_color
2022-07-25 20:03:00 +02:00
if %_NCS% EQU 1 (
echo %esc% [%~1 %~2 %esc% [0m
) else (
2024-08-16 22:11:42 +02:00
if not exist %psc% ( echo %~3 ) else ( %psc% write-host -back '%1 ' -fore '%2 ' '%3 ')
2022-07-25 20:03:00 +02:00
)
exit /b
2024-08-16 22:11:42 +02:00
: dk_color2
2022-07-25 20:03:00 +02:00
if %_NCS% EQU 1 (
echo %esc% [%~1 %~2 %esc% [%~3 %~4 %esc% [0m
) else (
2024-08-16 22:11:42 +02:00
if not exist %psc% ( echo %~3 %~6 ) else ( %psc% write-host -back '%1 ' -fore '%2 ' '%3 ' -NoNewline; write-host -back '%4 ' -fore '%5 ' '%6 ')
2022-07-25 20:03:00 +02:00
)
exit /b
2024-08-16 22:11:42 +02:00
: :========================================================================================================================================
2022-07-25 20:03:00 +02:00
2024-08-16 22:11:42 +02:00
: : Set variables
2022-07-25 20:03:00 +02:00
2024-08-16 22:11:42 +02:00
: dk_setvar
2022-07-25 20:03:00 +02:00
2024-08-16 22:11:42 +02:00
set psc = powershell.exe
set winbuild = 1
for /f " tokens=6 delims=[]. " %% G in ( 'ver' ) do set winbuild = %% G
2022-07-25 20:03:00 +02:00
2024-08-16 22:11:42 +02:00
set _NCS = 1
if %winbuild% LSS 10586 set _NCS = 0
if %winbuild% GEQ 10586 reg query " HKCU\Console " /v ForceV2 %nul2% | find /i " 0x0 " %nul1% && ( set _NCS = 0)
2022-07-25 20:03:00 +02:00
if %_NCS% EQU 1 (
for /F %% a in ( 'echo prompt $E ^| cmd' ) do set " esc= %% a "
set " Red= " 41;97m" "
set " Gray= " 100;97m" "
set " Green= " 42;97m" "
set " Blue= " 44;97m" "
set " _Red= " 40;91m" "
set " _White= " 40;37m" "
2024-08-16 22:11:42 +02:00
set " _Green= " 40;92m" "
2022-07-25 20:03:00 +02:00
set " _Yellow= " 40;93m" "
2024-08-16 22:11:42 +02:00
) else (
set " Red= " Red" " white" "
set " Gray= " Darkgray" " white" "
set " Green= " DarkGreen" " white" "
set " Blue= " Blue" " white" "
set " _Red= " Black" " Red" "
set " _White= " Black" " Gray" "
set " _Green= " Black" " Green" "
set " _Yellow= " Black" " Yellow" "
2022-07-25 20:03:00 +02:00
)
2024-08-16 22:11:42 +02:00
set " nceline=echo: &echo ==== ERROR ==== &echo: "
set " eline=echo: &call :dk_color %Red% " ==== ERROR ====" &echo: "
if %~z0 GEQ 200000 (
set " _exitmsg=Go back "
set " _fixmsg=Go back to Main Menu, select Troubleshoot and run Fix Licensing option. "
) else (
set " _exitmsg=Exit "
set " _fixmsg=In MAS folder, run Troubleshoot script and select Fix Licensing option. "
)
2022-07-25 20:03:00 +02:00
exit /b
2023-09-25 15:46:19 +02:00
: :========================================================================================================================================
: : Leave empty line below