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
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