mbowen18
asked on
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
Hmmm.... vbtextcompare didn't work for me.
Try this one if the above fails for you too.
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
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks Amazing Tech - I was out of touch but found this when back. It worked great and we have it processing with GPO! Excellent!
Glad it worked. Thanks for the grade.
Try this.
Open in new window