1. ホーム
  2. スクリプト・コラム
  3. vbs

ホストファイルのドメインアドレスの内容を削除するためにvbsを使用します。

2022-02-08 01:45:19

要件 元のホストに次の3行が追加されています。

202.102.101.105 intranet.corp
202.102.101.107 mail.intranet.corp
202.102.101.108 sip.intranet.corp

この3行はその後不要になるので、上記のvbsコードを実行して、hostファイルからこの3行を削除してください。

'This script requires that the executing user has local administrator privileges
Const ForReading = 1, ForWriting = 2, ForAppending = 8, ReadOnly = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
'WinDir is the windows installation directory
WinDir =WshShell.ExpandEnvironmentStrings("%WinDir%")
'Set the host file directory

HostsFile = WinDir & "\System32\Drivers\etc\Hosts"
'Check if the host file is read-only, if it is, then modify the file properties
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(HostsFile)
If objFile.Attributes And ReadOnly Then
 Attributes = objFile.Attributes Xor ReadOnly
End If


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(HostsFile, ForReading,true)

'Check whether the host file has been changed inside, if it has been changed, the script will no longer be executed
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.107"))<> 0 Or (InStr (strline, " ;202.102.101.108")) Then
  strline=""
End If
  hostfileline=hostfileline+vbCrLf+strline
Loop
WScript.Echo hostfileline
objFile.Close


'Modify the host file
Set filetxt = fso.OpenTextFile(HostsFile, ForWriting )
filetxt.Write hostfileline
filetxt.Close
WScript.Quit