Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Modifying Host File

$
0
0

Hi,

We have a requirement to modify Hosts file of all the PC's in Domain for which we require a script which can be applied through GPO.

The requirement is to first read the hosts file for 4 Host entries and if missing add those four 4 entries in the hosts file. I have searched and got the script as mentioned below. It is working fine for one host entry but I have to create 4 scripts for creating 4 Host entries.

I want to create a single script which should check the entries one by one and whichever entry is missing it should create it.

==============================================================================

Const ForReading = 1, ForWriting = 2, ForAppending = 8, ReadOnly = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell=CreateObject("WScript.Shell")
WinDir =WshShell.ExpandEnvironmentStrings("%WinDir%")

HostsFile = WinDir & "\System32\Drivers\etc\Hosts"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(HostsFile, ForReading)

Do Until objFile.AtEndOfStream
If InStr (objFile.ReadLine, "192.168.10.105") <> 0 Then
WScript.Quit
End If
i = i + 1
Loop
objFile.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(HostsFile)
If objFile.Attributes AND ReadOnly Then
objFile.Attributes = objFile.Attributes XOR ReadOnly
End If

Set filetxt = fso.OpenTextFile(HostsFile, ForAppending, True)
filetxt.WriteLine(vbNewLine & "192.168.10.105 Host1")
filetxt.Close
WScript.quit 

=========================================================================


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>