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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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!
AmazingTechCommented:
Glad it worked. Thanks for the grade.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.