Vbscript Change Hosts files content for all clients


This script should be add to a group policy to run at logon, and ensure that you can change in all clients at the same time.

Dim fso, tf
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.OpenTextFile("C:\WINDOWS\system32\drivers\etc\hosts", ForWriting, True)
  tf.WriteLine("# Copyright (c) 1993-2009 Microsoft Corp.") 
  tf.WriteLine("#")
  tf.WriteLine("# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.")
  tf.WriteLine("#")
  tf.WriteLine("# This file contains the mappings of IP addresses to host names. Each")
  tf.WriteLine("# entry should be kept on an individual line. The IP address should")
  tf.WriteLine("# be placed in the first column followed by the corresponding host name.")
  tf.WriteLine("# The IP address and the host name should be separated by at least one space.")
  tf.WriteLine("#")
  tf.WriteLine("# Additionally, comments (such as these) may be inserted on individual")
  tf.WriteLine("# lines or following the machine name denoted by a '#' symbol.")
  tf.WriteLine("#")
  tf.WriteLine("# For example:")  
  tf.WriteLine("#")
  tf.WriteLine("#      102.54.94.97     rino.acme.com           # source server")  
  tf.WriteLine("#")
  tf.WriteLine("#       38.25.63.10     x.acme.com              # x client host")
  tf.WriteLine("#localhost name resolution is handled within DNS itself.")
  tf.WriteLine("# localhost name resolution is handled within DNS itself.")
  tf.WriteLine("#    127.0.0.1       localhost")
  tf.WriteLine("#    ::1             localhost ")
  tf.WriteBlankLines(3)
  tf.WriteLine("10.0.0.1   myserver1") # example of entry
  tf.WriteLine("10.0.0.2    myserver2")
  tf.Write ("# File changed by Script for all clients over GPO policy")
  tf.Close

Hope that this script can be useful

Advertisements

About rodvars
Been working in IT Services/Consulting for the past 15 years. My main areas of work are planning, development, managing and administration System infrastructures focusing on optimizing user processes, enforcing business security, performance enhancements, high availabilty and infrastucture scalability.

3 Responses to Vbscript Change Hosts files content for all clients

  1. Reza says:

    Does the privilege affect the script execution?
    I would like to make this script to run at users logon but they dont have administrator privilege.

    Thank you.

    • rodvars says:

      Hi,
      You need to have permitions to change the file. You Can use a GPO to do it.

      Best Regards

      • Reza says:

        Thank you rodvars. :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: