'該腳本要求執(zhí)行用戶有本地管理員權(quán)限
Const ForReading = 1, ForWriting = 2, ForAppending = 8, ReadOnly = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell=CreateObject("WScript.Shell")
'WinDir 為windows安裝目錄
WinDir =WshShell.ExpandEnvironmentStrings("%WinDir%")
'設(shè)定host 文件目錄
HostsFile = WinDir "\System32\Drivers\etc\Hosts"
'檢查host文件是否為只讀,如為只讀,則修改文件屬性
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(HostsFile)
If objFile.Attributes And ReadOnly Then
objFile.Attributes = objFile.Attributes Xor ReadOnly
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(HostsFile, ForReading,true)
'檢查host文件里面是否已經(jīng)更改過了,如果更改過,則不再執(zhí)行腳本
hostfileline=""
Do Until objFile.AtEndOfStream
strline = objfile.ReadLine
If InStr (strline, "202.102.101.105") > 0 Or (InStr (strline, "202.102.101.107"))>0 Or (InStr (strline, "202.102.101.108")) Then
strline=""
End If
hostfileline=hostfileline+vbCrLf+strline
Loop
WScript.Echo hostfileline
objFile.Close
'修改host文件
Set filetxt = fso.OpenTextFile(HostsFile, ForWriting )
filetxt.Write hostfileline
filetxt.Close
WScript.Quit