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

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

mbowen18Asked:
Who is Participating?
 
AmazingTechCommented:
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
 
AmazingTechCommented:
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
 
AmazingTechCommented:
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
 
mbowen18Author Commented:
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
 
AmazingTechCommented:
Glad it worked. Thanks for the grade.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.