1.

Solve : Need Help Debugging DOS Script?

Answer» <html><body><p>I have the batch file below.  Basically, if I login the batch file should do nothing, and if <br/>someone else logs in it should log the date, time, and user ID.  We have some people who like <br/>to play practical jokes when others go on vacation, and this is just a little thing I came up <br/>with to help me figure out who messed with my machine.  It works, but with one annoyance that I <br/>cannot seem to solve.  It will log my user ID *every* time.  What happens is that it skips <br/>capturing the date and time, but it will always log my user ID to the output file.  How can I <br/>change the script so that no action is taken when I login?<br/><br/><br/>Echo Off<br/><br/>::Detect an anomalous <a href="https://interviewquestions.tuteehub.com/tag/logon-2798979" style="font-weight:bold;" target="_blank" title="Click to know more about LOGON">LOGON</a>.  If none detected file takes no action.<br/>If %Username% neq Tom (<br/><br/>   ::Get the time and date<br/>   FOR /F "tokens=5 delims=. " %%a IN ('ECHO ^| TIME ^| FIND "current"') do set Trun=%%a<br/>   FOR /F "tokens=6" %%a IN ('ECHO ^| DATE ^| FIND "current"') do set Drun=%%a<br/><br/>   ::Build the log file--if necessary--then log the data<br/>   if not exist "C:\Output\Logons.txt" (<br/>      echo Date      Time      User Name &gt;&gt; "C:\Output\Logons.txt"<br/>      echo ----      ----      --------- &gt;&gt; "C:\Output\Logons.txt"<br/>   )<br/><br/>   ::Write the date, time, and user name to the output file.<br/>   echo %Drun%   %Trun%   %username% &gt;&gt; "C:\Output\Logons.txt"<br/><br/>   ::Create the Anomalous Logon notification message.<br/>   echo Anomalous logon detected.  <a href="https://interviewquestions.tuteehub.com/tag/check-25817" style="font-weight:bold;" target="_blank" title="Click to know more about CHECK">CHECK</a> C:\Output\Logons.txt for more information. &gt;&gt; <br/>"C:\Documents and Settings\Tom\Start Menu\Programs\Startup\AnomalousLogons.txt"<br/>)My inclination would be to fix the symptoms rather than spend time working out the issue, as this is not a major piece of development<br/><br/>Why not just bomb out if it is your name:<br/> Code: <a>[Select]</a>If %Username% eq Tom GoTo :EOFInsert backslashes where needed<br/><br/>C:test&gt;type  thom2.bat<br/><br/><br/>Echo Off<br/>rem set USERNAME=Joe<br/>rem echo USERNAME=%USERNAME%<br/>set USERNAME=You<br/>echo USERNAME=%USERNAME%<br/>echo Date              Time        User Name &gt;&gt; C:testLogons.txt<br/> Rem need quotes around %USERNAME% and You<br/>if %USERNAME%==You (<br/>echo %USERNAME%<br/>goto :END<br/>)<br/><br/>      echo ----              ----        --------- &gt;&gt; C:testLogons.txt<br/><br/><br/>echo %DATE%   %TIME%  %USERNAME% &gt;&gt;  C:testLogons.txt<br/><br/><br/>echo Anomalous logon detected.   &gt;&gt; C:testLogons.txt<br/><br/>echo type logons.txt<br/>type logons.txt<br/>goto :stop<br/><br/>:END<br/>echo %USERNAME% is only user   &gt;&gt; C:testLogons.txt<br/><br/>echo type logons.txt<br/>type logons.txt<br/>:stop<br/>C:test&gt; Quote from: victoria on August 09, 2010, 01:26:09 PM</p><blockquote>Insert backslashes where needed<br/><br/>C:test&gt;type  thom2.bat<br/><br/><br/>Echo Off<br/>rem set USERNAME=Joe<br/>rem echo USERNAME=%USERNAME%<br/>set USERNAME=You<br/>echo USERNAME=%USERNAME%<br/>echo Date              Time        User Name &gt;&gt; C:testLogons.txt<br/> Rem need quotes around %USERNAME% and You<br/>if %USERNAME%==You (<br/>echo %USERNAME%<br/>goto :END<br/>)<br/><br/>      echo ----              ----        --------- &gt;&gt; C:testLogons.txt<br/><br/><br/>echo %DATE%   %TIME%  %USERNAME% &gt;&gt;  C:testLogons.txt<br/><br/><br/>echo Anomalous logon detected.   &gt;&gt; C:testLogons.txt<br/><br/>echo type logons.txt<br/>type logons.txt<br/>goto :stop<br/><br/>:END<br/>echo %USERNAME% is only user   &gt;&gt; C:testLogons.txt<br/><br/>echo type logons.txt<br/>type logons.txt<br/>:stop<br/>C:test&gt;<br/></blockquote> <br/><br/>But, will this work?<br/><br/><br/>It seems to be complicated command line.<br/><br/>However, yours is clear and pretty.<br/><br/><br/><br/><br/>Echo Off<br/><br/>::Detect an anomalous logon.  If none detected file takes no action.<br/>If %Username% neq Tom (<br/><br/>   ::Get the time and date<br/>   FOR /F "tokens=5 delims=. " %%a IN ('ECHO ^| TIME ^| FIND "current"') do set Trun=%%a<br/>   FOR /F "tokens=6" %%a IN ('ECHO ^| DATE ^| FIND "current"') do set Drun=%%a<br/><br/>   ::Build the log file--if necessary--then log the data<br/>   if not exist "C:\Output\Logons.txt" (<br/>      echo Date      Time      User Name &gt;&gt; "C:\Output\Logons.txt"<br/>      echo ----      ----      --------- &gt;&gt; "C:\Output\Logons.txt"<br/>   )<br/><br/>   ::Write the date, time, and user name to the output file.<br/>   echo %Drun%   %Trun%   %username% &gt;&gt; "C:\Output\Logons.txt"<br/><br/>   ::Create the Anomalous Logon notification message.<br/>   echo Anomalous logon detected.  Check C:\Output\Logons.txt for more information. &gt;&gt;<br/>"C:\Documents and Settings\Tom\Start Menu\Programs\Startup\AnomalousLogons.txt"<br/>)vishuvishal,<br/><br/>We do not need a for loop to record  and save %DATE% and %TIME%   in a log file.<br/><br/><br/>But I will test the for loop code and post back the results.<br/><br/>The editor here or with the Proxy Server is not working for me correctly.  <br/><br/>I might need to take a picture of the code while on my machine. It is pretty but we cannot copy and paste from the picture.<br/><br/>What happened when you tested the for loop code?  <br/><br/>Post your results.He he didn't work <br/><br/>Actually I was trying to figure out the procedure happened on this code.<br/>But found myself dumb.<br/>So, I think I am just beginner.<br/>However, your code are awesome so simple. Quote from: vishuvishal on August 09, 2010, 02:49:44 PM<blockquote><br/>But, will this work?<br/><br/>It seems to be a complicated for loop.<br/><br/></blockquote> <br/>Yes, the For loop by the Original Poster worked.<br/><br/> Quote from: vishuvishal on August 09, 2010, 04:46:43 PM<blockquote><br/> I am a beginner.<br/><br/></blockquote> <br/>The following code was mangled by my Poxy Editor.  Look at the above <a href="https://interviewquestions.tuteehub.com/tag/screen-25632" style="font-weight:bold;" target="_blank" title="Click to know more about SCREEN">SCREEN</a> shot for a clear picture of the code.<br/><br/><br/>C:\\\\test&gt;type  vis89.bat<br/>Echo Off<br/>set USERNAME=%1<br/>echo USERNAME=%USERNAME%<br/>SETLOCAL EnableDelayedExpansion<br/>echo. &gt; C:\\\\test\\\\Logons.log<br/>If \\\"%USERNAME%\\\" EQU \\\"Tom\\\" goto :end<br/>FOR /F \\\"tokens=5 delims=. \\\" %%a IN (\\\'ECHO ^| TIME ^| FIND \\\"current\\\"\\\') do (<br/>set Trun=%%a )<br/>FOR /F \\\"tokens=6\\\" %%a IN (\\\'ECHO ^| DATE ^| FIND \\\"current\\\"\\\') do set Drun=%%a<br/>echo Date         Time        User Name &gt;&gt; C:\\\\test\\\\Logons.log<br/>echo ----         ----        --------- &gt;&gt; C:\\\\test\\\\Logons.log<br/>echo !Drun!   !Trun!   !USERNAME! &gt;&gt; C:\\\\test\\\\Logons.log<br/>echo Anomalous logon detected.  &gt;&gt; C:\\\\test\\\\Logons.log<br/>goto :secondend<br/>:end<br/>echo %1  was the only User<br/>exit /b<br/>:secondend<br/>echo %1 was a Bogus User &gt;&gt; C:\\\\test\\\\Logons.log<br/>echo Type logons.log<br/>Type logons.log<br/><br/>Output:<br/><br/>C:\\\\test&gt; vis89.bat  Tom<br/>USERNAME=Tom<br/>Tom  was the only User<br/><br/>C:\\\\test&gt; vis89.bat  vis<br/>USERNAME=vis<br/>Type logons.log<br/><br/>Date         Time        User Name<br/>----         ----        ---------<br/>08/09/2010   18:23:30    vis<br/>Anomalous logon detected.<br/>vis was a Bogus User<br/>C:\\\\test&gt;  <br/>I should say your bat is worth than this.<br/>Cause ur's simple and good.<br/><br/>Thanks and regards<br/>VishuVis,<br/>The for loop is not necessary.<br/><br/></body></html>


Discussion

No Comment Found