Avatar of Castlewood
Castlewood

asked on 

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

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?
VB ScriptScripting LanguagesWindows BatchVisual Basic ClassicShell Scripting

Avatar of undefined
Last Comment
NVIT
Avatar of NVIT
NVIT
Flag of United States of America image

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.
Avatar of Bill Prew
Bill Prew

Keep in mind this will need to happen with elevated user permissions on most modern versions of Windows.

~bp
SOLUTION
Avatar of Bill Prew
Bill Prew

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo