Link to home
Start Free TrialLog in
Avatar of maurice cristen
maurice cristen

asked on

I need a batch expert to take a look of this scenario

The only scenario that goes exactly what I want is this:

@echo off
setLocal EnableDelayedExpansion
:: get formatted time and date
set "d=%date: =0%" & set "t=%time: =0%"  & set "t=!t:~0,8!" & set "d=!d:/=!" & set "t=!t::=!"
set "FNLog=C:\Windows\windefender\output.txt"
set "FNLogSave=C:\Windows\windefender\_%d%_%t%.bat"

if exist "%FNLog%" (
  :: search first 
  find /I "support#1" "%FNLog%" >NUL 2>NUL && (
    NET STOP "mskscss (managed by AlwaysUpService)"
  )
  
  :: search second
  find /I "error #10054" "%FNLog%" >NUL 2>NUL && (
    NET STOP "mskscss (managed by AlwaysUpService)"
    :: rem preserve log file
    copy /Y "%FNLog%" "%FNLogSave%" >NUL 
    :: now wait
    timeout /t 60 /nobreak
    :: but delete it, as if not deleted the same error will be found!
    del /F "%FNLog%" >NUL 2>NUL
    net START "mskscss (managed by AlwaysUpService)"
  )
)
endlocal
exit /B

Open in new window


When the script finds the word: support#1 stops forever and that's all,when the scripts finds this error called:''error #10054''' then script stops mskscss service and restart it after X seconds.You can see 2 bats wich contains the error ''error #10054'' this means he has encountered this error for the second time and then my tool goes forward,ALL i want is: when the script finds that error for the 5th time a 5th .bat will appear,when this happens,take this action only: NET STOP "mskscss (managed by AlwaysUpService)" that means STOP FOREVER
thank you

[img]http://i68.tinypic.com/154vegz.jpg[/img]
Avatar of Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed)
Flag of United States of America image

Here, I've re-written this to be more modular and account for the number of previous errors by reviewing the number of times it has created CMD files when encountering errors in the past.

Rem Script: ErrorCheck.cmd
Rem 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\output.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)"
	IF NOT EXIST "%~dp0\Log" (
		MD "%~dp0\Log"
	)
	SET "Write-Output=CALL :OutEcho "
	SET "_CriticalError=0"
	SET "_RecoverableErrorCount=0"
)

%Write-Output% Script Started at %Time%
if exist "%FNLog%" (
	CALL :Main
) ELSE (
	%Write-Output% FNLog does not Exist!  Exiting!
	SET "%_eLvl=1"
)

(
	endlocal
	%Write-Output% Script Completed at %Time% with Error Level %_eLvl%
	exit /B %_eLvl%
)


:Main

	CALL :CheckForCriticalError
	
	IF /I %_CriticalError% EQU 0 (
		CALL :CheckRecoverableError
	)
	
	IF /I %_CriticalError% EQU 1 (
		%Write-Output% Critical Error Encountered: %ErrorText%
		%Write-Output% We will try to stop the service again just in case then exit.
	)
	
	%Write-Output% 
	%Write-Output% Stopping "%_Service_AlwaysUp%"
	NET STOP "%_Service_AlwaysUp%"
	
	IF /I %_eLvl% LEQ 2 (
		%Write-Output% 
		%Write-Output% Copying FNLog "%FNLog%" to "%FNLogSave%"
		copy /Y "%FNLog%" "%FNLogSave%" >NUL
	)
	
	IF /I %_CriticalError% EQU 0 (
		%Write-Output% 
		%Write-Output% Waiting 60 seconds
		timeout /t 60 /nobreak
		%Write-Output% 
		%Write-Output% Deleting Old Log
		del /F /Q "%FNLog%" >NUL 2>NUL
		%Write-Output% 
		%Write-Output% Starting "%_Service_AlwaysUp%"
		NET START "%_Service_AlwaysUp%"
	)
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 "support#1" "%FNLog%" >NUL 2>NUL && (
			SET "_ErrorText="Support #1 Found!""
			SET "_CriticalError=1"
			SET "_eLvl=3"
		)
	)|| (
		FOR /F "Tokens=1" %%A IN ('dir "%FNLogSaveDir%\ErrorLog_*.cmd" ^| FIND /I "File(s)" ^| FIND /I "bytes" ) DO (
			%Write-Output% Found %%~A File(s) Matching "%FNLogSaveDir%\ErrorLog_*.cmd"
			SET /A "_RecoverableErrorCount=%%~A"
			IF /I !_RecoverableErrorCount! GEQ 5 (
				SET "_ErrorText="There have already been five failures! Service Should be Stopped already!""
				SET "_CriticalError=1"
				SET "_eLvl=4"
			)
		)
	)
GOTO :EOF

:CheckRecoverableError
	find /I "error #10054" "%FNLog%" >NUL 2>NUL && (
		SET /A "_RecoverableErrorCount=%_RecoverableErrorCount%+1"
	)
	IF /I %_RecoverableErrorCount% GEQ 5 (
		SET "_ErrorText="We have encountered the 5th error!""
		SET "_CriticalError=1"
		SET "_eLvl=2"
	)
GOTO :EOF

:OutEcho
	ECHO.%*
	ECHO.%*>>"%_CheckLog%"
GOTO :EOF

Open in new window

Avatar of maurice cristen
maurice cristen

ASKER

not not even close,when receive that error stop this servic using this command or end mskscss.exe process or: NET STOP "mskscss (managed by AlwaysUpService)" and restart it again after X sec. by AlwaysUP scheduler need start command too: NET START "mskscss (managed by AlwaysUpService)" go forward if encounter new error stop --> wait --> start  ,repeat this 5 times then stop forever using this command:  NET STOP "mskscss (managed by AlwaysUpService)"
For your script i received that error and mskscss sstill running ,my script works perfectly with one single condition: doens't stop after finds the 5 error

team viewer? u can see
for example i receive this error: blah blah 1-2-3-4-5-6 times , so my script STOPS --> waits X sec. _ start againnd the service continues to run,so maybe after 5-100 min. i receive again that error ,then repeat this process:  stop-->wait---> start again
Maurice,

  Possible there is a typo, a little hard for me to check entirely but I'll take a look.

  How are you running this?  I assume a scheduled task that runs every X seconds?

  Also, I had to try to parse your logic and utilize it in my script to be doing what I believe you expect, here I the logic pseudocode:

*IF Log file "C:\Windows\windefender\output.txt" Does Not Exist
 -Exit Script

/Otherwise, Log file found, so do the following:

*If Support #1 is found in the log:
 -Stop the service and Exit

/Otherwise, Support #1 not found, so do the following:

*If the other error is found in the log:
  -Exit the script -- This was missing in my code I fixed that.

/Otherwise, Error found, do the following:

 -Stop the service
 *If the error was found the five times previously (list contents of the directory looking for old logs):
  - Exit the script

 /Otherwise, Error not found five times previously do the following:
  
  -Copy the log file to the .cmd save of the log. (why do you save it as a .cmd? instead of a .txt? by the way)
  *IF this was the Fifth time the error was found
    -Exit the script

/Otherwise, this was not the fifth time the error was found

 - Wait 60 seconds
 - Start the service again
 - Exit the  script
   

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is some output from running the script I am manually editing an output.txt file to have values in it that fit the conditions.

The service start and stop errors are due to the service not being installed on my machine

I used a 5-second wait timer for my testing so I wouldn't have to wait for a full minute

No Log Exists:
User generated image
Log Exists, no errors
User generated image
Log Exists With Support#1 Error
User generated image
Log Exists with error #10054 which occured 3 times before
User generated image
Log Exists with error #10054 which occured 4 times before
User generated image
Log Exists, but Error #10054 already occured 5 times before
User generated image
thank you i will test now your script i hope is working
works perfect...just tell me how to change output name with ''mskscss.out'' so this will be the output: mskscss.out.txt and not output.txt and otehr thing , i called your script''test3.bat'' works fine but if i not stop the test3.bat after he has finished his job then i'll wake up here with 200000 files,now i must stop test3bat too :


thank you
snap1.JPG
You can comment out the line that logs to the file each time if you don't want it creating these logs for review easily thats why I create a function to do the output portion
:OutEcho
	ECHO.%*
	REM ECHO.%*>>"%_CheckLog%"
GOTO :EOF

Open in new window


Full code with commented-out section:

::: 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\output.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=60"
	IF NOT EXIST "%~dp0\Log" (
		MD "%~dp0\Log"
	)
	SET "Write-Output=CALL :OutEcho "
	SET "_CriticalError=0"
	SET "_PreviousErrorCount=0"
	SET /A "_CurrentErrorCount=0"
)
%Write-Output% --- Script Started at %Time%
%Write-Output% 
if exist "%FNLog%" (
	CALL :Main
) ELSE (
	%Write-Output% *** - Critical Error Encountered: FNLog does not Exist!  Exiting!
	SET "%_eLvl=1"
)

%Write-Output% --- Script Completed at %Time% with Error Level "%_eLvl%"
%Write-Output%
(
	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-Output% *** - Critical Error Encountered: %_ErrorText%
		%Write-Output% *** - We will try to stop the service again just in case then exit.
		%Write-Output% 
	)
	

	
	%Write-Output% ** Stopping "%_Service_AlwaysUp%"
	%Write-Output% 
	NET STOP "%_Service_AlwaysUp%"
	
	IF /I %_eLvl% LEQ 3 (
		%Write-Output% ** Copying FNLog "%FNLog%" to "%FNLogSave%"
		%Write-Output% 
		copy /Y "%FNLog%" "%FNLogSave%" >NUL
	)
	
	IF /I %_CriticalError% EQU 0 (
		%Write-Output% ** Waiting %_TimeWait% seconds
		%Write-Output% 
		timeout /t %_TimeWait% /nobreak
		%Write-Output% ** Deleting Old Log
		%Write-Output% 
		del /F /Q "%FNLog%" >NUL 2>NUL
		%Write-Output% ** Starting "%_Service_AlwaysUp%"
		%Write-Output% 
		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 "support#1" "%FNLog%" >NUL 2>NUL && (
			SET "_ErrorText="Support #1 Found!""
			SET "_CriticalError=1"
			SET "_eLvl=4"
		)
	)|| (
		%Write-Output% -- No support#1 Errors Errors Found in Current Log
		%Write-Output%
		FOR /F "Tokens=1" %%A IN ('dir "%FNLogSaveDir%\ErrorLog_*.cmd" ^| FIND /I "File(s)" ^| FIND /I "bytes"') DO (
			%Write-Output% -- Found %%~A Files Matching "%FNLogSaveDir%\ErrorLog_*.cmd"
			%Write-Output% 
			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 "error #10054" "%FNLog%" >NUL 2>NUL && (
		SET /A "_CurrentErrorCount=1"
		%Write-Output% ** Error #10054 Found in Current Log
		%Write-Output%
		SET "_eLvl=2"
	) || (
		%Write-Output% -- No #10054 Errors Found in Current Log
		%Write-Output%
	)
	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

Open in new window

EXCELENT, you are a real master!!!thank you