想要使用VB脚本配置IP部件。我得到了下面的脚本,它将获得用户输入并相应地更改IP地址,但它只改变IP地址,子网,网关以及我想要添加首选和备用DNS服务器。
Option Explicit 'Enforce strict naming
Dim WSH, FSO
Dim objComputer, objWMIService, objOperatingSystem, objNetAdapter
Dim strTitle, strVersion, strThisPC, strComputername, strCompNameRegPath, strWorkgroup
Dim strCCS, strTcpipParamsRegPath, strIPAddress, strSubnetMask, strGateway, strGatewayMetric
Dim colMEM, colNetAdapters, colOperatingSystems
Dim errEnable, errGateways
'Setup scripting environment
Set WSH = CreateObject ("WSCript.shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
'Set Version info
strVersion = "v04"
'Set target PC for WMI
strThisPC = "."
'Set the Title for any dialogs
strTitle = "XP Clone Changer " & strVersion
'------------------------------------------------------------------------
'Section 1 - Use WMI to change IP address, subnet mask and gateway
WSH.Popup "Preparing to change IP address, subnet mask and gateway... Please wait.",2, strTitle 'Inform user
'Setup WMI
Set objWMIService = GetObject("winmgmts:\\" & strThisPC & "\root\cimv2")
'Set specific WMI query so 1394 adapter is excluded
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
'Get user input into an array
strIPAddress = Array(InputBox("Enter the new IP address"))
strSubnetMask = Array(InputBox("Enter the new Subnet mask"))
strGateway = Array(InputBox("Enter the new Gateway address"))
'Inform user
WSH.Popup "Please be patient whilst changes are made. This could take 10 seconds or more.",3, strTitle 'Inform user
strGatewayMetric = Array(1)
'Make the changes
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
If errEnable = 0 Then
WSH.Popup "The IP address has been changed.",2,strTitle 'Inform user of success
Else
WSH.Popup "The IP address could not be changed automatically." & vbCrLf & vbCrLf &_
"This may be because the PC/Laptop is not connected to an active network." & vbCrLf & vbCrLf &_
"Please check the IP settings manually afterwards.",2,strTitle 'Inform user of failure
End If
Next
Set WSH = Nothing
Set FSO = Nothing
Set objWMIService = Nothing
Set colNetAdapters = Nothing
Set colMEM = Nothing
WScript.Quit
答案 0 :(得分:1)
可以使用SetDNSServerSearchOrder
方法设置DNS服务器:
arrDNSServers = Array("192.168.23.13", "192.168.23.14")
errDNS = objNetAdapter.SetDNSServerSearchOrder(arrDNSServers)
如果希望用户指定名称服务器,您可以执行以下操作:
arrDNSServers = Split(InputBox("Enter the name servers (separated by spaces)"))
输入以空格分隔的名称服务器(例如192.168.23.13 192.168.23.14
)。该字符串将自动转换为带有地址的数组。