Solved

Using script to manage host file

Posted on 2012-03-23
3
660 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
3 Comments
 
LVL 17

Assisted Solution

by:bigeven2002
bigeven2002 earned 250 total points
Comment Utility
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 51

Accepted Solution

by:
Bill Prew earned 250 total points
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Before I go to far, let's explain HA (High Availability) and why you should consider it.  High availability is the mechanism used to provide redundancy to any service at the same site and appears as a single service to the users of that service.  As…
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 this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

12 Experts available now in Live!

Get 1:1 Help Now