Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript to edit hosts file 1

Status
Not open for further replies.

jlotz1

Technical User
Feb 1, 2010
6
US
Hi all,
I use a vbscript to modify the hosts file on a group of about 90 field tech laptops on a regular basis. (I know...but using the hosts file is the easiest method in this situation.)

I've been using the below vbscript, running it as a startup script, and it has worked well until today. It allows me to add/delete lines from the hosts file very easily. Until today. Today I was asked to edit the hosts file such that two hosts will have the same IP address. For example,
207.16.29.2 hosta.abc.com
207.16.29.2 hostb.abc.com

Apparently the array can't handle duplicates like this. I don't get an error , but the 2nd entry never gets created. The lines below and above it get created with no issues. Can someone tell me how I can edit my script to allow for duplicate IP's?
THANKS!

Here's the script as it exists today:


--------------------------------------------------------------------------------

Set objShell = CreateObject("WScript.Shell")
strSrcFile = objShell.ExpandEnvironmentStrings("%SYSTEMROOT%") & "\System32\Drivers\Etc\Hosts"

Set objFSO = CreateObject("Scripting.FileSystemObject")

arrHosts = Array(_
"198.217.198.111 webapps2.safari.com", _
"209.116.248.250 maps.google.com", _
"209.116.248.251 auth4.keysholep.com", _
"209.116.248.252 kuh.google.com" _
)

If objFSO.FileExists(strSrcFile) = True Then
Set objFile = objFSO.OpenTextFile(strSrcFile, 1, False)
strContents = objFile.ReadAll
objFile.Close
Set objFile = Nothing
arrContents = Split(strContents, VbCrLf)
For Each strHost In arrHosts
strIPAddress = Split(strHost, vbTab)(0)
strDNSName = Split(strHost, vbTab)(1)
boolHostFound = False
For intLine = LBound(arrContents) To UBound(arrContents)
If InStr(arrContents(intLine), strIPAddress) > 0 Or InStr(arrContents(intLine), strDNSName) > 0 Then
boolHostFound = True
arrContents(intLine) = strIPAddress & vbTab & strDNSName
End If
Next
If boolHostFound = False Then
ReDim Preserve arrContents(UBound(arrContents) + 1)
arrContents(UBound(arrContents)) = strIPAddress & vbTab & strDNSName
End If
Next
Set objFile = objFSO.CreateTextFile(strSrcFile, True)
objFile.Write Join(arrContents, VbCrLf)
objFile.Close
Set objFile = Nothing
'MsgBox "File modified."
Else
'MsgBox "File not found."
End If
 
arrHosts = Array(_
"198.217.198.111" & vbTab & "webapps2.safari.com", _
"209.116.248.250" & vbTab & "maps.google.com", _
"209.116.248.251" & vbTab & "auth4.keysholep.com", _
"209.116.248.252" & vbTab & "kuh.google.com", _
"207.16.29.2" & vbTab & "hosta.abc.com hostb.abc.com" _
)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV: Works perfectly! Thanks so much for your quick response.
 
>Apparently the array can't handle duplicates like this

The array handles it fine. The issue is that the code specificately ignores duplicate IP addresses and hostnames:

Changing
Code:
If InStr(arrContents(intLine), strIPAddress) > 0 Or InStr(arrContents(intLine), strDNSName) > 0 Then

to

Code:
If (InStr(arrContents(intLine), strIPAddress) > 0 [b]And[/b] InStr(arrContents(intLine), strDNSName)) Or InStr(arrContents(intLine), strDNSName)  > 0 Then
should do the trick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top