1

我有一个脚本,它利用 VBScript 来识别运行它的机器的 DNS。如果正在使用我正在寻找的 DNS,它会提醒我。我希望更进一步,如果找到了指定的 DNS,则将该特定 DNS 更改为另一个 DNS。我发现了一些似乎是基本思想的脚本,但我认为它们不会取代已识别的脚本,只会取代列表顶部的脚本。

这是我的识别指定 DNS 的 VBScript:

'Bind to Shell
Set objShell = WScript.CreateObject("WScript.Shell")

'Read Servers NetbiosName
'strComputer = objShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName\ComputerName")

strComputer = "."
wscript.echo strComputer

Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

For Each objNicConfig In colNicConfigs
    If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
        For Each strDNSServer In objNicConfig.DNSServerSearchOrder
            if strDNSServer = "8.8.8.8" Then
                wscript.echo "Works!"
            End if
            wscript.echo strDNSServer
        Next
    End If
Next

为了澄清,我需要帮助的部分是脚本打印出“作品”的地方。我特别希望将该 DNS 更改为另一个指定的 DNS。

这是我还发现的一些声称更改 DNS 的代码,但我担心如果我将其插入其中,它只会将更改 DNS 放在列表的顶部,而不是我确定的 DNS:

Set objShell = WScript.CreateObject("Wscript.Shell")
objShell.Run "netsh interface ip set address name=""Local Area Connection"" static " & strIPAddress & " " & strSubnetMask & " " & strGateway & " " & intGatewayMetric, 0, True

让我知道我是否可以澄清任何事情!提前致谢!

4

1 回答 1

1

You can set DNS servers using the SetDNSServerSearchOrder method (see here for an example). However, that method expects an array with all DNS servers you want to use, so you need to read the current DNS servers into an array, modify the address(es) you want to change, then call SetDNSServerSearchOrder with the modified array.

If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
  dns = objNicConfig.DNSServerSearchOrder
  For i = 0 To UBound(dns)
    if dns(i) = "8.8.8.8" Then dns(i) = "4.4.4.4"
  Next
  objNicConfig.SetDNSServerSearchOrder(dns)
End If
于 2013-06-27T18:48:25.170 回答