-1

I am writing a batch script which needs to compare registry data value string with ±3 days of todays date.

My REG QUERY returns a value:

HKEY_LOCAL_MACHINE\Software\Wow6432Node\KasperskyLab\Components\34\1103\1.0.0.0\Statistics\AVState
Protection_BasesDate    REG_SZ    27-08-2018 08-53-00

I need to output to a file, depending if it is within the range or not.

Script:

REG QUERY "HKLM\Software\Wow6432Node\KasperskyLab\Components\34\1103\1.0.0.0\Statistics\AVState" /v "Protection_BasesDate" | Find "2018"
IF %ERRORLEVEL% == 1 goto end
If %ERRORLEVEL% == 0 goto makefile

:makefile
echo "{"product":"Override Antivirus","running":true,"upToDate":true}" > c:\ProgramData\CentraStage\AEMAgent\antivirus.json

:end
@exit
Compo
  • 36,585
  • 5
  • 27
  • 39
  • 1
    Please properly format your question https://stackoverflow.com/help/formatting – Capricorn Aug 27 '18 at 12:41
  • did it, sorry, first post here. – Stas Isackov Aug 27 '18 at 12:44
  • 1
    A string value is not a date object, batch files without the use of other utilities or languages cannot easily make the conversion between data types in order to then make a comparison. I would certainly consider instead using/leveraging `WSH` or `PowerShell` instead. – Compo Aug 27 '18 at 12:48
  • what is the format of your date on your system, if you run `cmd.exe` and type `echo %date%`. is it `2018-08-27`? – Gerhard Aug 27 '18 at 13:29
  • Compo, i will try to make it via powershell, but i have so little experience with it. I was thinking maybe output key to a file, then %date% output to a file too, then to compare. Gerhard, the output is 27.08.2018. but i have many systems to check, so i am not sure all will give the same result. – Stas Isackov Aug 27 '18 at 13:43

2 Answers2

1

This solution takes this answer as base, so please review such an answer before post further questions here...

@echo off
setlocal EnableDelayedExpansion

rem Define the "Date to Julian Day Number" conversion function
set "DateToJDN(YMD)=( a=(YMD), y=a/10000, a%%=10000, m=a/100, d=a%%100, a=(m-14)/12, (1461*(y+4800+a))/4+(367*(m-2-12*a))/12-(3*((y+4900+a)/100))/4+d-32075 )"

rem Get the JDN of today's date minus/plus 3
for /F "tokens=2 delims==" %%t in ('wmic os get localdatetime /value') do set "dateTime=%%t"
set /A "todayMinus3=!DateToJDN(YMD):YMD=%dateTime:~0,8%!-3, todayPlus3=todayMinus3+6"

reg Get the date from REG QUERY command; the assumed output format is: Protection_BasesDate    REG_SZ    27-08-2018 08-53-00
for /F "tokens=3-5 delims=- " %%a in (
   'REG QUERY "HKLM\Software\Wow6432Node\KasperskyLab\Components\34\1103\1.0.0.0\Statistics\AVState" /v "Protection_BasesDate"'
) do set /A "BasesDate=!DateToJDN(YMD):YMD=%%c%%b%%a!"

if %BasesDate% geq %todayMinus3% if %basesDate% leq %todayPlus3% (
   echo Date in range
)
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

There are many languages that can do much better than batch, but this might be a solution.

@echo off
set day=-3
for /f  "delims=" %%i in ('reg query "HKLM\Software\Wow6432Node\KasperskyLab\Components\34\1103\1.0.0.0\Statistics\AVState" /v Protection_BasesDate" ^| findstr "2018"') do set "regdate=%%i"
for /f "tokens=1-3" %%a in ("%regdate%") do set "actual=%%c"
:check
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "yyyy=%result:~0,4%"
set "mm=%result:~4,2%"
set "dd=%result:~6,2%"
set "final=%dd%-%mm%-%yyyy%"
echo %final%
if %day% == 1 goto :EOF
if %actual% == %final% (echo Within 3 days!!) else (set /a day+=1 & goto :check)

We actually get the date of the system using cscript (yes I pipe to file, but you can run hybrid without the temp file) Then we take the past 3 days' dates and match them in the format of the registry key, if the key matches within 3 days, I just echo Within 3 days! There is also an if statement, if the day counter = 1, I exit the script, as we do not want to go into future and this loop forever if the counter if we do not stop it somewhere.

If it does work for you, you can simply remove the line print %final% and replace this line:

if %actual% == %final% (echo Within 3 days!!) else (set /a day+=1 & goto :check)

with this line:

if %actual% == %final% (echo "{"product":"Override Antivirus","running":true,"upToDate":true}" > "c:\ProgramData\CentraStage\AEMAgent\antivirus.json") else (set /a day+=1 & goto :check)
Gerhard
  • 22,678
  • 7
  • 27
  • 43