?
Solved

Add new function to trouble-shooting script

Posted on 2015-01-07
7
Medium Priority
?
162 Views
Last Modified: 2015-01-08
Hi Experts.  I have a script that I use for trouble-shooting that I need to modify.  I'd like to show an error message (%Computer% isn't online or pingable) and put it in the log file.  Lastly, the script doesn't continue.

Here is the script currently:

@echo off
setlocal

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

set logfile=\\server\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 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

Open in new window


Thanks for your help Experts!!
0
Comment
Question by:samiam41
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 40536378
It's not quite clear what you mean; something like this?
@echo off
setlocal

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

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

 >"%logfile%" 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 Info]----
>>"%logfile%" echo %Computer%

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

>>"%logfile%" echo ----[Computer Info]----
ping.exe -4 -n 2 %Computer% >>"%logfile%"
>>"%logfile%" echo.

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

Open in new window

0
 
LVL 9

Author Comment

by:samiam41
ID: 40536418
Hey oBdA!  

You have the right idea as far as what I am looking for.  My goal is to verify that the computer is online before the rest of the script continues on.  If the computer can't be pinged, I want to stop the script and output that the computer is offline or can't be pinged.

Here is what I have which is formatted a little differently than what you posted.

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 (
	>>"%logfile%" %Computer% isn't online or pingable
	exit /b 1
)

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

Open in new window


The output of the logfile shows a blank entry under the ----[Computer Online Verification]---- label.  The logfile is not showing what it should and then the script closes out.

I need the %logfile% to be opened when the script ends under this portion (please).
0
 
LVL 9

Author Comment

by:samiam41
ID: 40536428
Ok.  Disregard.  I made this modification to your script.  Here's what I used and it worked.


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

I think I was missing the echo portion.
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 9

Author Closing Comment

by:samiam41
ID: 40536429
Thanks as always!  You're brilliant.
0
 
LVL 85

Expert Comment

by:oBdA
ID: 40536477
To maybe elaborate on my changes ...
Just for fun, paste the following directly into a command prompt:
set Index=2
echo Processing %Index%>test.log

Open in new window

Want to guess what the content of test.log will be?
Hover here for the solution ...

Having the redirection at the beginning prevents these issues without always having a trailing space at the end (or having to put the whole echo command in brackets).
0
 
LVL 9

Author Comment

by:samiam41
ID: 40536497
Thank you for explaining that.  I knew there was a reason you used it that way and it makes sense.  From here on out, I will format my scripts accordingly (the oBdA way!)

I did like the link and message you encoded in there.  Very slick.
0
 
LVL 9

Author Comment

by:samiam41
ID: 40538623
If anyone would like some points, I would appreciate your help!

http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_28593187.html
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question