?
Solved

VB Script help with appending and adding entries to a host file

Posted on 2008-11-07
5
Medium Priority
?
363 Views
Last Modified: 2013-12-26
We had a VB script from Rob Sampson that updates the host file, but doesn't add entries that don't currently exist from the array in the script.  Can anyone provide some help with this?


Set objShell = CreateObject("WScript.Shell")
strSrcFile = objShell.ExpandEnvironmentStrings("%SYSTEMROOT%") & "\system32\drivers\etc\hosts"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
arrHosts = Array(_
	"10.1.1.26	emcmain", _
	"10.1.1.33	visidata", _
	"10.1.1.35	visweb", _
	"10.2.1.5	emcatlapps", _
	"10.2.1.3	emcatl3", _
	"10.2.1.4	emcatlds", _
	"10.4.1.5	emcgainesville1", _
	"10.1.1.20	emclake1", _
	"10.1.1.27	emclakeapps", _
	"10.1.1.30	emcavtap", _
	"10.1.1.73	emcacct", _
	"192.168.1.250	emcraleigh", _
	"10.3.1.20	emccali1" _
	)
 
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

Open in new window

0
Comment
Question by:mbowen18
  • 4
5 Comments
 
LVL 21

Expert Comment

by:AmazingTech
ID: 22911980
It worked for me but possibly you're having issues with case sensitivity.

Try this.
Set objShell = CreateObject("WScript.Shell")
strSrcFile = objShell.ExpandEnvironmentStrings("%SYSTEMROOT%") & "\system32\drivers\etc\hosts"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
arrHosts = Array(_
	"10.1.1.26	emcmain", _
	"10.1.1.33	visidata", _
	"10.1.1.35	visweb", _
	"10.2.1.5	emcatlapps", _
	"10.2.1.3	emcatl3", _
	"10.2.1.4	emcatlds", _
	"10.4.1.5	emcgainesville1", _
	"10.1.1.20	emclake1", _
	"10.1.1.27	emclakeapps", _
	"10.1.1.30	emcavtap", _
	"10.1.1.73	emcacct", _
	"192.168.1.250	emcraleigh", _
	"10.3.1.20	emccali1" _
	)
 
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, vbtextcompare) > 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

Open in new window

0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 22912010
Hmmm.... vbtextcompare didn't work for me.
Try this one if the above fails for you too.
Set objShell = CreateObject("WScript.Shell")
strSrcFile = objShell.ExpandEnvironmentStrings("%SYSTEMROOT%") & "\system32\drivers\etc\hosts"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
arrHosts = Array(_
	"10.1.1.26	emcmain", _
	"10.1.1.33	visidata", _
	"10.1.1.35	visweb", _
	"10.2.1.5	emcatlapps", _
	"10.2.1.3	emcatl3", _
	"10.2.1.4	emcatlds", _
	"10.4.1.5	emcgainesville1", _
	"10.1.1.20	emclake1", _
	"10.1.1.27	emclakeapps", _
	"10.1.1.30	emcavtap", _
	"10.1.1.73	emcacct", _
	"192.168.1.250	emcraleigh", _
	"10.3.1.20	emccali1" _
	)
 
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(lcase(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

Open in new window

0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 2000 total points
ID: 22912069
OK. Figured out my error.

instr has a starting position which I now set to 1.
Set objShell = CreateObject("WScript.Shell")
strSrcFile = objShell.ExpandEnvironmentStrings("%SYSTEMROOT%") & "\system32\drivers\etc\hosts"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
arrHosts = Array(_
	"10.1.1.26	emcmain", _
	"10.1.1.33	visidata", _
	"10.1.1.35	visweb", _
	"10.2.1.5	emcatlapps", _
	"10.2.1.3	emcatl3", _
	"10.2.1.4	emcatlds", _
	"10.4.1.5	emcgainesville1", _
	"10.1.1.20	emclake1", _
	"10.1.1.27	emclakeapps", _
	"10.1.1.30	emcavtap", _
	"10.1.1.73	emcacct", _
	"192.168.1.250	emcraleigh", _
	"10.3.1.20	emccali1" _
	)
 
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(1, arrContents(intLine), strDNSName, vbtextcompare) > 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

Open in new window

0
 

Author Closing Comment

by:mbowen18
ID: 31514571
Thanks Amazing Tech - I was out of touch but found this when back.  It worked great and we have it processing with GPO!  Excellent!
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23011055
Glad it worked. Thanks for the grade.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question