本文介绍了如何在Vista / Server 2008中使用WMI清除TCP / IP网关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 现在我正在写这篇文章,我已经通过互联网阅读了大约30页,发现没有什么可以接受的。 我在VB.Net中使用WMI( Win32_NetworkAdapterConfigurationonfiguration )编写了一个代码,用于设置和清除特定IPEnabled网络适配器的网关/ DNS。 此代码在Windows XP / 2003 Server上运行良好,但在Vista / Server 2008中无论如何都无法清除网关。 我试图将Null数组传递给SetGatways方法,但没有任何反应 试图为GatewayCostMetrics传递一个Null数组,不成功。 我也试过wbemtest.exe来直接执行这个方法,但那个也没用。 我不知道这是不是一个bug,但我从微软支持中读到的每篇文章都没有结果。 这里我将编写2个VBScript代码块, 首先将网关/ DNS写入首选的IPEnabled网卡,然后第二个将尝试o清除那些。 我对此问题的任何帮助表示感谢。 网关/ DNS设置代码:Hi,Now I'm writing this, I've read about 30 pages over internet and found nothing acceptable.I have written a code using WMI(Win32_NetworkAdapterConfigurationonfiguration) in VB.Net which sets and clears Gateway/DNS for a specific IPEnabled network adapter.This code works great on Windows XP/2003 Server, but in Vista/Server 2008 it cannot CLEAR the gateway anyway.I tried to pass a Null array to SetGatways method, but nothing happenedtried to pass a Null array for GatewayCostMetrics, unsuccessful.I also tried wbemtest.exe to execute the method directly, but that one doesn't help too.I don't know if this is a bug or not, but every article I read from microsoft support, ended with no result.Here I will write 2 VBScript code blocks,first will write a Gateway/DNS to the preferred IPEnabled NIC and second will attempt to clear those.I appreciate any help on this issue.Gateway/DNS Set code:strComputer = "."arrDefaultGateways = Array("192.168.0.1")arrGatewayCostMetrics = Array(1) ' uint16arrDNSServers = Array("192.168.0.1")Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colNicConfigs = objWMIService.ExecQuery _ ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")For Each objNicConfig In colNicConfigs If objNicConfig.IPEnabled Then strText = objNicConfig.Description & vbNewLine _ & objNicConfig.MACAddress & vbNewLine & vbNewLine _ & "Do you want to change Gateway/DNS of this NIC?" strTitle = "Question..." intMsg = MsgBox(strText,vbYesNo,strTitle) Select Case intMsg Case 6 'yes intGWReturn = objNicConfig.SetGateways(arrDefaultGateways, _ arrGatewayCostMetrics) intDNSReturn = objNicConfig.SetDNSServerSearchOrder(arrDNSServers) If (intGWReturn + intDNSReturn = 0) Then WScript.Echo "Successful" Else WScript.Echo "Gateway Return code= " & intGWReturn _ & vbNewLine & "DNS Return code= " & intDNSReturn End If Exit For Case 7 'no End Select End IfNext 网关/ DNS清除代码:Gateway/DNS Clear code:strComputer = "."arrDefaultGateways = Array() ' < Empty ArrayarrGatewayCostMetrics = Array() ' < Empty ArrayarrDNSServers = Array() ' < Empty ArraySet objWMIService = GetObject("winmgmts:" _& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colNicConfigs = objWMIService.ExecQuery _("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")For Each objNicConfig In colNicConfigsIf objNicConfig.IPEnabled ThenstrText = objNicConfig.Description & vbNewLine _& objNicConfig.MACAddress & vbNewLine & vbNewLine _& "Do you want to change Gateway/DNS of this NIC?"strTitle = "Question..."intMsg = MsgBox(strText,vbYesNo,strTitle)Select Case intMsgCase 6 'yesintGWReturn = objNicConfig.SetGateways(arrDefaultGateways, _arrGatewayCostMetrics)intDNSReturn = objNicConfig.SetDNSServerSearchOrder(arrDNSServers)If (intGWReturn + intDNSReturn = 0) ThenWScript.Echo "Successful"ElseWScript.Echo "Gateway Return code= " & intGWReturn _& vbNewLine & "DNS Return code= " & intDNSReturnEnd IfExit ForCase 7 'noEnd SelectEnd IfNext推荐答案ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");ManagementObjectCollection moc = mc.GetInstances();foreach(ManagementObject mo in moc){// Make sure this is a IP enabled device. Not something like memory card or VM Wareif( (bool)mo["IPEnabled"] ){if( mo["Caption"].Equals( nicName ) ){ //Enable DHCP ManagementBaseObject newDNS = mo.GetMethodParameters( "SetDNSServerSearchOrder" );newDNS[ "DNSServerSearchOrder" ] = null;ManagementBaseObject enableDHCP = mo.InvokeMethod( "EnableDHCP", null, null);ManagementBaseObject setDNS = mo.InvokeMethod( "SetDNSServerSearchOrder", newDNS, null); //Save all Gateways into an array string[] gateways = (string[])mo["DefaultIPGateway"]; ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic"); ManagementBaseObject newGate = mo.GetMethodParameters("SetGateways"); //Set last value of the array(always the Gateway recived by DHCP) as the default Gateway newGate["DefaultIPGateway"] = new string[] {gateways[gateways.Length-1]}; newGate["GatewayCostMetric"] = new int[] { 1 }; //Set IP settings back to static ManagementBaseObject setIP = mo.InvokeMethod("EnableStatic", newIP, null); ManagementBaseObject setGateways = mo.InvokeMethod("SetGateways", newGate, null); //Enable DHCP again newDNS["DNSServerSearchOrder"] = null; ManagementBaseObject enableDHCP2 = mo.InvokeMethod("EnableDHCP", null, null); ManagementBaseObject setDNS2 = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);}}} 也许不是最优雅的方式,但它作品Maybe not the most elegant way to do it, but it workspublic static void FixGateway(string networkInterfaceId) { foreach (ManagementObject adapter in new ManagementClass("Win32_NetworkAdapterConfiguration").GetInstances()) { if (adapter["SettingID"] as string == networkInterfaceId) { string[] gateways = (string[])adapter["DefaultIPGateway"]; string gateway = string.Empty; foreach (string gw in gateways) { if (IPAddress.Parse(gw).AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) gateway = gw; } ManagementBaseObject newIP = adapter.GetMethodParameters("EnableStatic"); adapter.InvokeMethod("EnableStatic", newIP, null); ManagementBaseObject newGateway = adapter.GetMethodParameters("SetGateways"); newGateway["DefaultIPGateway"] = new string[] { gateway }; newGateway["GatewayCostMetric"] = new int[] { 1 }; adapter.InvokeMethod("SetGateways", newGateway, null); adapter.InvokeMethod("EnableDHCP", null, null); } } } 这篇关于如何在Vista / Server 2008中使用WMI清除TCP / IP网关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-31 05:24