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("# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.")
  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("# Additionally, comments (such as these) may be inserted on individual")
  tf.WriteLine("# lines or following the machine name denoted by a '#' symbol.")
  tf.WriteLine("# For example:")  
  tf.WriteLine("#     rino.acme.com           # source server")  
  tf.WriteLine("#     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("#       localhost")
  tf.WriteLine("#    ::1             localhost ")
  tf.WriteLine("   myserver1") # example of entry
  tf.WriteLine("    myserver2")
  tf.Write ("# File changed by Script for all clients over GPO policy")

Hope that this script can be useful


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.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: