Solved

Using script to manage host file

Posted on 2012-03-23
3
669 Views
Last Modified: 2012-03-23
I need a script that I can run to append to add multiple entries to the host files on my network.

The current script I use is

REM Appendhost.bat
echo 192.168.30.1 server1 >> %SYSTEMDRIVE%\Windows\System32\Drivers\Etc\Hosts
echo 192.168.35.2 server2 >> %SYSTEMDRIVE%\Windows\System32\Drivers\Etc\Hosts
echo 192.168.40.3 server3 >> %SYSTEMDRIVE%\Windows\System32\Drivers\Etc\Hosts

Open in new window


Up until we started using windows 7 this script functioned. After we updated to windows 7 however anytime this is run it appears to overwrite the entire file and only leave a blank file with the entry
192.168.40.3 server3

Entries 1 & 2 do not appear
0
Comment
Question by:itguy565
[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
3 Comments
 
LVL 17

Assisted Solution

by:bigeven2002
bigeven2002 earned 250 total points
ID: 37758302
Hello,

Not sure how to fix the script above, but you can try the PowerShell method alternatively:

http://gallery.technet.microsoft.com/scriptcenter/51ea84d2-717f-467a-8153-cf9bd02573ff
0
 
LVL 55

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 37758325
As long as you are using >> rather than > I don't see any reason why you would get the results you mentioned.

Try copying the hosts file to a different location, say c:\temp, then adjust the BAT script to reference that directory, and see if you have the same problem when you run it.

~bp
0
 
LVL 11

Author Comment

by:itguy565
ID: 37759245
Experts,

I was able to figure out what the issue was with my script. My problem was not with my script above. It works fine as long as you run it locally on each machine. When you attempt to push the same script out using our Kaseya managed service platform it appears that the >> was being treated as > and was causing our script to overwrite the files rather than append.

Thanks for your assistance and quick replies.
0

Featured Post

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

724 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