示例代码:
strComputer = "."
Set objWMIService = GetObject("winmgmts://" & strComputer & "/root/cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where MACAddress='00:24:8c:xx:xx:xx'")
strIPAddress = Array("192.168.17.14")
strSubnetMask = Array("255.255.255.192")
strGateway = Array("192.168.17.254")
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
If errEnable = 0 Then
IPChan=1
Else
IPChan=0
End If
Exit For
Next
If IPChan = 1 Then
WScript.Echo "The IP address has been changed to 192网段"
Else
WScript.Echo "The IP address could not be changed."
End If
上述脚本实现Windows的网络配置。
先执行 "Select * from Win32_NetworkAdapterConfiguration where MACAddress='00:24:8c:xx:xx:xx'" 查询出MAC地址为00:24:8c:xx:xx:xx 的网络连接,之后配置为:
IP:192.168.17.14
掩码:255.255.255.192
网关:192.168.17.254
配置成功后,会提示: “The IP address has been changed to 192网段” 否则提示:The IP address could not be changed.
注:Select * from Win32_NetworkAdapterConfiguration where MACAddress='00:24:8c:xx:xx:xx' 可以根据需要修改where的查找条件,具体可以参照上一篇的:ManagementObject 参数 WMI 一些参数(Win32_NetworkAdapterConfiguration)