[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Add new function to trouble-shooting script

Posted on 2015-01-07
7
Medium Priority
?
164 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
  • 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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

Independent Software Vendors: 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

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
Suggested Courses

872 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