如何在Vista / Server 2008中使用WMI清除TCP / IP网关? [英] How to clear TCP/IP Gateway using WMI in Vista/Server 2008?
问题描述
现在我正在写这篇文章,我已经通过互联网阅读了大约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 happened
tried 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) ' uint16
arrDNSServers = 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 If
Next
网关/ DNS清除代码:
Gateway/DNS Clear code:
strComputer = "."
arrDefaultGateways = Array() ' < Empty Array
arrGatewayCostMetrics = Array() ' < Empty Array
arrDNSServers = Array() ' < Empty Array
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 If
Next
推荐答案
我在C#中遇到了同样的问题。我做了以下事情:
首先我启用了DHCP。然后我将Geatways列表复制到一个数组中。
之后我手动将Geatway重置为最后一个值数组(这是网关从DHCP服务器中获取的总数)。
作为最后一步,我再次启用DHCP。
这是我的C#中的代码:
Hi,
I had the same problem in C#. I did the following:
First I enabled DHCP.Then i copy the List of Geatways into an array.
After that I reset the Geatway manually to the last value of the array(Which is allways the Gateway recived from the DHCP server).
As a last step I enable DHCP again.
Here is my Code in C#:
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 Ware
if( (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 works
基于其他解决方案,但这将忽略IPv6而没有DNS ...
1)启用DHCP < br $>
2)RenewDHCPLease
3)FixGateway()
Based on the other solution, but this will ignore IPv6 and without DNS...
1) Enable DHCP
2) RenewDHCPLease
3) FixGateway()
public 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网关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!