稍微改了一下,让他适合我的情况(自动判断我的ip)
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each objNetAdapter in colNetAdapters
sip=objNetAdapter.IPAddress
if sip(0)="10.35.4.34" then '判断要替换的ip
strIPAddress = Array("192.168.8.4") '替换ip
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.8.1") '修改网关
else
strIPAddress = Array("10.35.5.34")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("10.35.5.1")
end if
strGatewayMetric = Array(1)
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed as " & strIPAddress(0)
Else
WScript.Echo "The IP address could not be changed."
End If
exit for '只修改第一个网卡的设置
Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each objNetAdapter in colNetAdapters
sip=objNetAdapter.IPAddress
if sip(0)="10.35.4.34" then '判断要替换的ip
strIPAddress = Array("192.168.8.4") '替换ip
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.8.1") '修改网关
else
strIPAddress = Array("10.35.5.34")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("10.35.5.1")
end if
strGatewayMetric = Array(1)
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed as " & strIPAddress(0)
Else
WScript.Echo "The IP address could not be changed."
End If
exit for '只修改第一个网卡的设置
Next