Link to home
Start Free TrialLog in
Avatar of samiam41
samiam41Flag for United States of America

asked on

Clean up log file from batch script

Hi Experts.  I need to clean up a log file from a trouble-shooting tool.  It looks like a long question but it really is just details and examples.

Existing code
@echo off
setlocal

set /p "Computer=Please enter computer name: "
if "%Computer%" EQU "" (
  echo No computer name entered, ending.
  exit /b
)


set logfile=\\w2k3nas01\public\FG_Diagnostic\%computer%.log

echo ----[Diagnostic Run Time]----> %logfile%
echo %date:~4,2%/%date:~7,2%/%date:~10,4%>> %logfile%
echo %time:~0,2%:%time:~3,2%>> %logfile%
echo. >> %logfile%

echo ----[Computer Online Verification]---->>"%logfile%" 
ping.exe -4 -n 2 %Computer% | find.exe /i "TTL" >NUL
if errorlevel 1 (
	echo %Computer% isn't online or pingable >>"%logfile%" 
	start notepad %logfile%
	exit /b 1
)

echo ----[Computer Info]---->> %logfile%
echo %Computer% >> %logfile%
ping -n 2 %computer%>> %logfile%
echo. >> %logfile%

echo ----[Forward nslookup of IP '%Computer%']---->> %logfile%
nslookup.exe %Computer% >>"%LogFile%" 2>&1
set IP=
for /f "tokens=2 delims=[]" %%a in ('ping.exe -n 2 %computer% ^| find.exe /i "Pinging %computer%"') do set IP=%%a

echo ----[Pinging by name '%computer%']---->> %logfile%
ping.exe -n 2 %computer% >>"%LogFile%"
if "%IP%"=="" (
	>>"%LogFile%" echo Could not determine the IP.
) else (
	echo. >> %logfile%
	echo ----[Reverse nslookup of IP '%IP%']---->> %logfile%
	nslookup.exe %IP% >>"%LogFile%" 2>&1	
)

start notepad %logfile%
:eof

Open in new window


Which yields:


----[Diagnostic Run Time]----
01/08/2015
14:30
 
----[Computer Online Verification]----
----[Computer Info]----
csv7shared13

Pinging csv7shared13 [172.24.80.192] with 32 bytes of data:
Reply from 172.24.80.192: bytes=32 time<1ms TTL=128
Reply from 172.24.80.192: bytes=32 time<1ms TTL=128

Ping statistics for 172.24.80.192:
    Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
 
----[Forward nslookup of IP 'csv7shared13']----
Non-authoritative answer:

Server:  server
Address:  172.24.80.6

Name:    csv7shared13
Address:  172.24.80.192

----[Pinging by name 'csv7shared13']----

Pinging csv7shared13 [172.24.80.192] with 32 bytes of data:
Reply from 172.24.80.192: bytes=32 time<1ms TTL=128
Reply from 172.24.80.192: bytes=32 time<1ms TTL=128

Ping statistics for 172.24.80.192:
    Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
 
----[Reverse nslookup of IP '172.24.80.192']----
Server:  server
Address:  172.24.80.6

Name:    csv4shared5
Address:  172.24.80.192

What I want it to look like:

----[Diagnostic Run Time]----
01/08/2015
14:30
 
----[Computer Online Verification]----
----[Computer Info]----
csv7shared13

Pinging csv7shared13 [172.24.80.192] with 32 bytes of data:
Reply from 172.24.80.192: bytes=32 time<1ms TTL=128
Reply from 172.24.80.192: bytes=32 time<1ms TTL=128

----[Forward nslookup of IP 'csv7shared13']----

Name:    csv7shared13
Address:  172.24.80.192

----[Pinging by name 'csv7shared13']----

Pinging csv7shared13 [172.24.80.192] with 32 bytes of data:
Reply from 172.24.80.192: bytes=32 time<1ms TTL=128
Reply from 172.24.80.192: bytes=32 time<1ms TTL=128
 
----[Reverse nslookup of IP '172.24.80.192']----

Name:    csv4shared5
Address:  172.24.80.192

Let me know if this can be done.  Thanks experts for all your help!
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

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
Avatar of samiam41

ASKER

Hey oBdA!

Thank you for the information.  I am going to use your format with this new script and going forward so I appreciate your patience as I ask questions.

Testing your suggestion now.
You already have an answer there by the looks but you may also want to consider using a ( before the first

(echo ----[Diagnostic Run Time]----

and then an extra ) at the end of your logged bit:

      echo ----[Reverse nslookup of IP '%IP%']---->> %logfile%
      for /f "skip=4 delims=" %%a in ('nslookup.exe %IP%') do echo %%a
)
)>"%logfile%"

and redirect once the whole lot to your log file and remove all references to >>%logfile%.

If you want to display something to the screen in your script in the middle you can use echo Whatever > CON at it goes to the screen instead of the log

Steve
oBdA, it works like a charm.

Steve, I appreciate the information and suggestion.  I will have to test that out but I won't be able to do that now.  I'm still trying to figure out this scripting thing and I don't want to hold up this question as I try to figure out your suggestion.  Unless anyone objects, I will close this one out and open a new question which should allow you to post that information and I can figure it out.

Thanks for everyone's help and time with this question!
Clean looking log file that will make trouble-shooting much easier.  Thank you!
Greetings!  If anyone has any time and would like some points, I would appreciate your help.

https://www.experts-exchange.com/questions/28601914/list-users-in-groups-in-OU.html