首先,如下是我在使用的两段用来修改IP和DNS等的代码
(这两段代码的原创不是我,朋友传给我的,结果他也忘记原作者是谁了,这里先给原作者道歉)
Func __SetIpAddress($IPAddress, $SubnetMask, $Gateway)
;设置IP、MASK、GW
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled =TRUE", "WQL", _
$wbemFlagReturnImmediately + $wbemFlagForwardOnly)
$IPAddress = _ArrayCreate($IPAddress)
$SubnetMask = _ArrayCreate($SubnetMask)
$Gateway = _ArrayCreate($Gateway)
$GatewayMetric = _ArrayCreate("1")
If IsObj($colItems) Then
For $objItem In $colItems
$errEnable = $objItem.EnableStatic ($IPAddress, $SubnetMask)
$errGateways = $objItem.SetGateways ($Gateway, $GatewayMetric)
Next
Else
MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_NetworkAdapterConfiguration")
EndIf
EndFunc
Func __SetDNS($DNS1, $DNS2)
;设置DNS
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled =TRUE", "WQL", _
$wbemFlagReturnImmediately + $wbemFlagForwardOnly)
$DNS = _ArrayCreate($DNS1, $DNS2)
If IsObj($colItems) Then
For $objItem In $colItems
$strDNSServerSearchOrder = $objItem.SetDNSServerSearchOrder ($DNS)
Next
Else
MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_NetworkAdapterConfiguration")
EndIf
EndFunc
下面我提两个问题,朋友们帮帮忙
1、上述代码修改IP地址和DNS时有一定的失败率,尤以DNS设置的失败率为慎。运气好的时候无论做几次修改,都可以成功,运气不好无论运行几次都修改不了。我不知道是否是我的使用有误,请朋友们帮忙测试这个函数我简单的解释一下:
__SetIpAddress($IPAddress, $SubnetMask, $Gateway),IP等的设定:$IPAddress(IP地址),$SubnetMask(子网掩码),$Gateway(网关)
__SetDNS($DNS1, $DNS2),DNS的设定,$DNS1(首选DNS), $DNS2(备用DNS)
2、我看到过一些网络设定程序,有一个选择设定哪块网卡的下拉框。也就是说,程序运行时检测当前计算机有几块网卡,每块网卡分别是什么,然后可选其中任一网卡进行IP等的设定,这个是怎么做的,我没思路了……
先谢谢朋友们了!
[ 本帖最后由 skyfree 于 2008-5-20 18:31 编辑 ] |