Solved

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

Posted on 2016-07-27
5
42 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 24

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 54

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 54

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 24

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

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

Suggested Solutions

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

685 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