要求用户手动输入IP,即192.168.0.2 然后网关将更改为192.168.0.254 InStrRev()和Left()函数应该可以正常工作。
Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
strQuery = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE MACAddress > ''"
Set colNetAdapters = objWMIService.ExecQuery _
(strQuery)
strIPAddress = Array(InputBox("IP address"))
strSubnetMask = Array("255.255.255.0")
strGateway = Left(strIPAddress, InStrRev(strIPAddress, ".")) & "254"
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address could not be changed."
End If
next
答案 0 :(得分:2)
使用Split()获取八位字节数组,并更改最后一个:
>> s = "192.168.0.2"
>> a = Split(s, ".")
>> a(3) = "254"
>> WScript.Echo Join(a, ".")
>>
192.168.0.254
答案 1 :(得分:1)
看起来我解决了自己的问题
Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
strQuery = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE MACAddress > ''"
Set colNetAdapters = objWMIService.ExecQuery _
(strQuery)
strIPAddress = (InputBox("IP address"))
strSubnetMask = Array("255.255.255.0")
strGateway = Left(strIPAddress, InStrRev(strIPAddress, ".")) & "254"
strIPAddress = Array(strIPAddress)
strGateway = Array(strGateway)
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address could not be changed."
End If
next
我发现在将变量放入数组之前读取变量是关键