Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ::: Script: ErrorCheck.cmd
- :: Description: Checks the status log from Windows defender and takes actions based on certain info found
- @(
- SETLOCAL EnableDelayedExpansion
- echo off
- CALL :GetDateTime
- SET "_eLvl=0"
- SET "FNLog=C:\Windows\windefender\mskscss.out.txt"
- SET "FNLogSaveDir=C:\Windows\windefender"
- REM SET "FNLogSaveFilePrefix=ErrorLog_"
- SET "FNLogSave=!FNLogSaveDir!\ErrorLog_!d!_!t!.cmd"
- SET "_CheckLog=%~dp0\Log\%~n0_Log_!d!_!t!.log"
- SET "_Service_AlwaysUp=mskscss (managed by AlwaysUpService)"
- SET "_TimeWait=20"
- IF NOT EXIST "%~dp0\Log" (
- MD "%~dp0\Log"
- )
- SET "Write-mskscss=CALL :OutEcho "
- SET "_CriticalError=0"
- SET "_PreviousErrorCount=0"
- SET /A "_CurrentErrorCount=0"
- )
- %Write-mskscss% --- Script Started at %Time%
- %Write-mskscss%
- if exist "%FNLog%" (
- CALL :Main
- ) ELSE (
- %Write-mskscss% *** - Critical Error Encountered: FNLog does not Exist! Exiting!
- SET "%_eLvl=1"
- )
- %Write-mskscss% --- Script Completed at %Time% with Error Level "%_eLvl%"
- %Write-mskscss%
- (
- endlocal
- exit /B %_eLvl%
- )
- :Main
- CALL :CheckForCriticalError
- IF /I %_CriticalError% EQU 0 (
- CALL :CheckCurrentError
- )
- IF /I %_CriticalError% EQU 0 (
- IF /I %_CurrentErrorCount% EQU 0 (
- GOTO :EOF
- )
- )
- IF /I %_CriticalError% EQU 1 (
- %Write-mskscss% *** - Critical Error Encountered: %_ErrorText%
- %Write-mskscss% *** - We will try to stop the service again just in case then exit.
- %Write-mskscss%
- )
- %Write-mskscss% ** Stopping "%_Service_AlwaysUp%"
- %Write-mskscss%
- NET STOP "%_Service_AlwaysUp%"
- IF /I %_eLvl% LEQ 3 (
- %Write-mskscss% ** Copying FNLog "%FNLog%" to "%FNLogSave%"
- %Write-mskscss%
- copy /Y "%FNLog%" "%FNLogSave%" >NUL
- )
- IF /I %_CriticalError% EQU 0 (
- %Write-mskscss% ** Waiting %_TimeWait% seconds
- %Write-mskscss%
- timeout /t %_TimeWait% /nobreak
- %Write-mskscss% ** Deleting Old Log
- %Write-mskscss%
- del /F /Q "%FNLog%" >NUL 2>NUL
- %Write-mskscss% ** Starting "%_Service_AlwaysUp%"
- %Write-mskscss%
- NET START "%_Service_AlwaysUp%"
- echo.there
- )
- GOTO :EOF
- :GetDateTime
- :: get formatted time and date
- set "d=%date: =0%" & set "d=!d:/=!"
- set "t=%time: =0%" & set "t=!t:~0,8!" & set "t=!t::=!"
- GOTO :EOF
- :CheckForCriticalError
- (
- find /I "Discovered" "%FNLog%" >NUL 2>NUL && (
- SET "_ErrorText="Discovered Found!""
- SET "_CriticalError=1"
- SET "_eLvl=4"
- )
- )|| (
- %Write-mskscss% -- No Discovered Errors Errors Found in Current Log
- %Write-mskscss%
- FOR /F "Tokens=1" %%A IN ('dir "%FNLogSaveDir%\ErrorLog_*.cmd" ^| FIND /I "File(s)" ^| FIND /I "bytes"') DO (
- %Write-mskscss% -- Found %%~A Files Matching "%FNLogSaveDir%\ErrorLog_*.cmd"
- %Write-mskscss%
- SET /A "_PreviousErrorCount=%%~A"
- IF /I !_PreviousErrorCount! GEQ 5 (
- SET "_ErrorText="There have already been five failures! Service Should be Stopped already!""
- SET "_CriticalError=1"
- SET "_eLvl=5"
- )
- )
- )
- GOTO :EOF
- :CheckCurrentError
- find /I "rate 0.00" "%FNLog%" >NUL 2>NUL && (
- SET /A "_CurrentErrorCount=1"
- %Write-mskscss% ** rate 0.00 Found in Current Log
- %Write-mskscss%
- SET "_eLvl=2"
- ) || (
- %Write-mskscss% -- No rate 0.00 Errors Found in Current Log
- %Write-mskscss%
- )
- SET /A "_TotalErrorCount=%_PreviousErrorCount%+%_CurrentErrorCount%"
- IF /I %_TotalErrorCount% GEQ 5 (
- SET "_ErrorText="We have encountered the 5th error!""
- SET "_CriticalError=1"
- SET "_eLvl=3"
- )
- GOTO :EOF
- :OutEcho
- ECHO.%*
- REM ECHO.%*>>"%_CheckLog%"
- GOTO :EOF
Advertisement
Add Comment
Please, Sign In to add comment