Solved

How to add an entry with a condition to hosts file?

Posted on 2016-07-27
5
38 Views
Last Modified: 2016-09-01
I need to write a script to verify if one specific entry "10.1.17.21  server1" exists in the hosts file or not. If yes, do nothing, else insert that entry into the hosts file.  
This script will be put in the login script to assure all users with this entry in their hosts file.  

Can you help please?
0
Comment
Question by:Castlewood
  • 2
  • 2
5 Comments
 
LVL 23

Expert Comment

by:NVIT
ID: 41731497
findstr /i "10.1.17.21  server1" c:\windows\system32\drivers\etc\hosts

if errorlevel 1 echo "10.1.17.21  server1" >>c:\windows\system32\drivers\etc\hosts

Open in new window


I don't think you can put it in a users login script because hosts may be protected... Unless user is an admin.

You could put it in a gpo in the Startup script, which should work.
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 41731505
Keep in mind this will need to happen with elevated user permissions on most modern versions of Windows.

~bp
0
 
LVL 52

Assisted Solution

by:Bill Prew
Bill Prew earned 100 total points (awarded by participants)
ID: 41731520
NVIT, I might be overthinking it a bit, I do that, but you might want to make it a bit more robust.  A couple of thoughts that come to mind:

- If for some reason the entry is in the file, but commented out (likely a # on the left part of the line), then we won't add it again, even though we would want to I think.  So perhaps use REGEX for the search and require the IP address to be at the start of the line, preceded only by whitespace.

- Using REGEX, you could handle multiple spaces other than what is assumed between the IP and host name.

- It's possible that we could add the same host name twice to the file, with different IPs, using this approach, but that risk seems too low to code for.  I think Windows would just pick the first matching host name in the file and ignore the second.

~bp
0
 
LVL 23

Accepted Solution

by:
NVIT earned 400 total points (awarded by participants)
ID: 41753316
Bill,

This seems to work.

findstr /r /b "10.1.17.21 *server1" c:\windows\system32\drivers\etc\hosts
if errorlevel 1 echo 10.1.17.21  server1 >>c:\windows\system32\drivers\etc\hosts

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now