页面

2009年4月21日

用vbs脚本修改IP,DNS地址

 工作需要,经常要在N个固定IP地址间切换。烦。上Script Center,居然发现还可以用脚本来设置IP。这下方便多啦,保存为一个VBS文件,双击就能直接改IP了。
strIP = "192.168.198.4"
strMask = "255.255.255.0"
strGW = "192.168.198.1"
strDNS1 = "202.120.79.34"     
strDNS2 = "202.120.79.35"
''==================以下代码一般不需要修改=====================
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
strIPAddress = Array(strIP)
strSubnetMask = Array(strMask)
strGateway = Array(strGW)
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
 errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
 errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
     arrDNSServers = Array(strDNS1,strDNS2)
    errDNS = objNetAdapter.SetDNSServerSearchOrder(arrDNSServers)
If errEnable = 0 Then
WScript.Echo "IP地址已更改。"
Else
WScript.Echo "更改IP地址失败。"
End If
Next

没有评论: