通过更改IP的最后八位字节,使用vbscript更改默认网关

通过更改IP的最后八位字节,使用vbscript更改默认网关,vbscript,static-ip-address,Vbscript,Static Ip Address,要求用户手动输入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 colNetAda

要求用户手动输入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
使用Split()获取八位字节数组,并更改最后一个八位字节:

>> s = "192.168.0.2"
>> a = Split(s, ".")
>> a(3) = "254"
>> WScript.Echo Join(a, ".")
>>
192.168.0.254

看来我解决了自己的问题

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
我发现在将变量放入数组之前读取变量是关键