Script to create or replace Hosts file at logon.

Hello,

Im searching a script to replace the hosts file at logon. This must be use in XP, W7 and W8 computers.

If the hosts file is not in C:\Windows\System32\drivers\etc it must be create.

If the hosts file exists in C:\Windows\System32\drivers\etc it must be replace to a default state.

I think this is very simple but im very noob with scripting.. sorry.

Thanks, regards


  • Edited by P_Agudo Thursday, June 18, 2015 9:53 AM
June 18th, 2015 6:14am

You can do this very easily using Group Policy Preferences. Read more: Configure a File Item

GPP is supported on XP if Group Policy Preference Client Side Extensions for Windows XP (KB943729) is installed. 

Edit:
XMLLite for Windows XP (KB915865) is also required on XP.

  • Edited by Leif-Arne Helland Thursday, June 18, 2015 7:01 AM
  • Proposed as answer by jrv Thursday, June 18, 2015 8:50 AM
Free Windows Admin Tool Kit Click here and download it now
June 18th, 2015 6:51am

You can do this very easily using Group Policy Preferences. Read more: Configure a File Item

GPP is supported on XP if Group Policy Preference Client Side Extensions for Windows XP (KB943729) is installed. 

Edit:
XMLLite for Windows XP (KB915865) is also required on XP.

June 18th, 2015 6:51am

Thanks, now works fine.

I do not understand what you're trying to say. I want to replace the hosts file in all network clients at logon to ensure that the content of the file is correct and it has not been modified by a malicious program.

What is the problem ?

Regards ;)

Free Windows Admin Tool Kit Click here and download it now
June 24th, 2015 3:55am

It would be better if you fixed the problem.  A logon script should not be able to change the hosts file.  If you are allowing all users to be admins then you are causing the problem and not fixing it.
June 24th, 2015 6:30am

No, users are not admins, only few of them. You mean that if they are not administrators, the hosts file can not be written, no ?

I think just like you, but the rules are issued by the company :)

Regards




  • Edited by P_Agudo 18 hours 45 minutes ago
Free Windows Admin Tool Kit Click here and download it now
June 24th, 2015 8:31am

Have you considered using a Scheduled task to do this

June 24th, 2015 8:40am

No, users are not admins, only few of them. You mean that if they are not administrators, the hosts file can not be written, no ?

I think just like you, but the rules are issued by the company :)

Regards




Only admins can write to the hosts file by design.  If you change this the file can be altered by malware.

I think you need to learn the basics of Windows Administration before trying to alter these things or you will create issues that may not be fixable.

Free Windows Admin Tool Kit Click here and download it now
June 24th, 2015 8:57am

No, users are not admins, only few of them. You mean that if they are not administrators, the hosts file can not be written, no ?

I think just like you, but the rules are issued by the company :)

Regards




  • Edited by P_Agudo Wednesday, June 24, 2015 12:23 PM
June 24th, 2015 12:22pm

The file is set to "read only" after replace it and have the correct ntfs permissions.

Free Windows Admin Tool Kit Click here and download it now
June 25th, 2015 2:30am

The file is set to "read only" after replace it and have the correct ntfs permissions.

  • Edited by P_Agudo Thursday, June 25, 2015 7:03 AM
June 25th, 2015 6:21am

We need create the Hosts file to avoid this error

DNS Client Events Event ID 1012

As i was saying, there was a script to rename the hosts file. Now they want a hostst file in his place to avoid the dns error.

Finally i configured "Creating a file system" with GPO.

Regards

Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 2:41am

THat event iD does not apply to systems after Vista.  The function on the default hosts file is built into the system now.

In vista and later the hosts file that exists has no entries. 

Here is a copy.  Note that there are no entries:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#       127.0.0.1       localhost
#       ::1             localhost
All entries are commented out.

July 6th, 2015 2:52am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics