[已解决]WMI获取网卡信息DNS只填写一个出错!
本帖最后由 clang 于 2011-6-7 00:56 编辑使用WMI获取网卡信息,当DNS只填写主DNS时,使用$objItems.DNSServerSearchOrder(1)获取备用DNS信息出错,XP下备用DNS显示乱码,Win7下则程序报错退出,请问有什么办法屏蔽错误让脚本继续执行?谢谢了
代码:Global Const $wbemFlagReturnImmediately = 0x10
Global Const $wbemFlagForwardOnly = 0x20
Global Const $strComputer = "localhost"
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled != 0", "WQL",$wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) then
$i = 1
For $objItem In $colItems
If @error = -1 Then ContinueLoop
$strIPAddress = $objItem.IPAddress(0)
$strIPSubnet = $objItem.IPSubnet(0)
$dns1= $objItem.DNSServerSearchOrder(0)
$dns2= $objItem.DNSServerSearchOrder(1)
MsgBox(0,"", "[网卡" & $i & "]" & @crlf & "网卡型号=" &$objItem.Description & @crlf & "网卡IP=" &$strIPAddress & @crlf & "网卡掩码=" &$strIPSubnet & @crlf & "网卡网关=" &$objItem.DefaultIPGateway(0) & @crlf & "网卡主DNS=" &$dns1 & @crlf & "网卡备DNS=" &$dns2 & @crlf & "网卡地址=" &$objItem.MACAddress)
$i = $i + 1
Next
Endif把所有DNS都写到一行了,有兴趣的可以自己改,更改后的代码:Global Const $wbemFlagReturnImmediately = 0x10
Global Const $wbemFlagForwardOnly = 0x20
Global Const $strComputer = "localhost"
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled != 0", "WQL",$wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) then
$i = 1
For $objItem In $colItems
If @error = -1 Then ContinueLoop
$strIPAddress = $objItem.IPAddress(0)
$strIPSubnet = $objItem.IPSubnet(0)
$dnsnum = UBound($objItem.DNSServerSearchOrder)-1
$sss= ""
;MsgBox(0,"","网卡型号=" &$objItem.Description & @crlf & $dnsnum)
For $i=0 To $dnsnum
$strdns = $objItem.DNSServerSearchOrder($i)
$sss = $sss & $strdns & "/"
$dns = StringTrimRight($sss, 1)
Next
MsgBox(0,"", "[网卡" & $i & "]" & @crlf & "网卡型号=" &$objItem.Description & @crlf & "网卡IP=" &$strIPAddress & @crlf & "网卡掩码=" &$strIPSubnet & @crlf & "网卡网关=" &$objItem.DefaultIPGateway(0) & @crlf & "网卡DNS=" &$dns & @crlf & "网卡地址=" &$objItem.MACAddress)
$i = $i + 1
Next
Endif 加个if else判断下$dns2 加个if else判断下$dns2
sunafter 发表于 2011-6-6 23:08 http://autoitx.com/images/common/back.gif
一样报错! 本帖最后由 amnesiac 于 2011-6-6 23:58 编辑
遇到同样的问题, 一同求教 学习学习!! 在:
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
下边加:
If @error Then
MsgBox(0, "test", "Error Getting an active Object. Error code: " & Hex(@error, 8))
Exit
EndIf
测试一下吧。。。。 可以将获取弄成设置吗?我很需要这个~~
页:
[1]