Solved

Using script to manage host file

Posted on 2012-03-23
3
666 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 54

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

Industry Leaders: 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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…

742 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