'设置IP信息' Sub setIp(ip,mask,gate) strIp = Array(ip) strGateway = Array(gate) strSubnetMask = Array(mask) DNSServer = Array("80.0.1.1","80.0.2.2") strComputer = "." ret = MsgBox("IP:" + strIp(0) + vbcrlf + "Subnet Mask:" + strSubnetMask(0) + vbcrlf + "DNS:" + DNSServer(0) + "," + DNSServer(1), vbokcancel) If ret = vbok Then Set objWMIService = GetObject("winmgmts:\" & strComputer & " ootcimv2") Set colNetAdapters = objWMIService.ExecQuery("Select * from Win2_NetworkAdapterConfiguration where IPEnabled=TRUE") For Each objNetAdapter In colNetAdapters errEnable1 = objNetAdapter.EnableStatic(strIp, strSubnetMask) errEnable2 = objNetAdapter.SetGateways(strGateway) errEnable3 = objNetAdapter.setDNSServerSearchOrder(DNSServer) If errEnable1 = 0 And errEnable2 = 0 And errEnable3 = 0 Then msg = "Set success!" Else If errEnable1 <> 0 Then msg = "Set IP address or subnet mask failed." End If If errEnable2 <> 0 Then msg = msg & "Set default gateway failed." End If If errEnable3 <> 0 Then msg = msg & "Set DNS failed." End If End If WScript.Echo msg Next End If End Sub '用于获取当前IP信息' Function GetIp() GetIp="" Dim objWMIService,colNetAdapters strComputer="." Set objWMIService = GetObject("winmgmts:\" & strComputer & " ootcimv2") Set colNetAdapters = objWMIService.ExecQuery("Select * from Win2_NetworkAdapterConfiguration where IPEnabled=TRUE") If colNetAdapters.Count = 0 Then WScript.Echo "No IP found." Exit Function End If If Ubound(colNetAdapters.ItemIndex(0).IPAddress) = 0 Then WScript.Echo "IP address error." Exit Function End if GetIp = colNetAdapters.ItemIndex(0).IPAddress(0) End Function '主程序入口' Dim objws Set objws = WScript.CreateObject("wscript.shell") currIp=GetIp If currIp="90.0.1.2" Then msg="Now it's WORK environment." Else If CurrIp="91.1.1.3" Then msg="Now it's TEST environment." Else msg="Other environment." End If End If ret = MsgBox(msg & ",reset network ?" & chr(10) & "WORK--Yes; TEST--No", vbyesnocancel) If ret = vbyes Then ip = "90.0.1.2" mask = "255.255.255.0" gate = "90.0.1.254" setIp ip,mask,gate End If If ret = vbno Then ip = "91.0.1.3" mask = "255.255.255.0" gate = "91.0.1.254" setIp ip,mask,gate End If