本帖最后由 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
|