本帖最后由 fpquenya 于 2012-8-4 22:38 编辑
回复 1# lon91ong
以前写的一个代码,供你参考.
;-------------------------
;WMI方法查询非系统隐藏网卡信息(包含HyperV/WMWare服务器虚拟化的网卡)
;关键是NetConnectionID名来判断;
;网上通过PhysicalAdapter来判断的话有系统限制,xp或win server 2003不支持.
;-------------------------
MsgBox(0,"网卡信息",get_network_info())
Func get_network_info()
Local $sNetInfo = ""
;查询本机网卡信息
Local $oWMI = ObjGet("winmgmts:\\.\root\cimv2")
Local $colNet= $oWMI.ExecQuery("SELECT * FROM Win32_NetworkAdapter")
Local $oNet
Local $colConfig
For $oNet In $colNet
If $oNet.NetConnectionID <> "" Then ;关键的一句,有NetConnectionID名的就不是系统虚拟网卡! (物理网卡/HyperV虚拟网卡,非系统隐藏网卡)
$sNetInfo &= @CRLF & "网卡名称:" & $oNet.Name & @CRLF & _
"网卡索引号:" & $oNet.Index & @CRLF & _
"PNP设备ID:" & $oNet.PnPDeviceID & @CRLF
;得到网卡状态
Local $iStatus = $oNet.NetConnectionStatus
Local $sStatus = ""
Switch $iStatus
Case 0,7
$sStatus = "断开"
Case 2
$sStatus = "连通"
Case 4,5
$sStatus = "硬件不存在或禁用"
Case 1
$sStatus = "正在连接"
Case 3
$sStatus = "正在断开"
Case 6
$sStatus = "硬件故障"
Case 8
$sStatus = "正在验证"
Case 9
$sStatus = "验证成功"
Case 10
$sStatus = "验证失败"
Case 11
$sStatus = "非法地址"
Case 12
$sStatus = "要求凭证"
EndSwitch
$sNetInfo &= "网卡状态:" & $sStatus & @CRLF
If _bIsWin7Win2008() = True Then ;2008/Win7/2008R2系统才支持PhysicalAdapter属性
If $oNet.PhysicalAdapter = True Then
$sNetInfo &= "物理网卡:是" & @CRLF
Else
$sNetInfo &= "物理网卡:否" & @CRLF
EndIf
EndIf
$sNetInfo &= "MAC和IP信息:" & @CRLF
$colConfig = $oWMI.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index='" & $oNet.DeviceID & "'") ;属性太多,用*
Local $oConfig
Local $sIP,$sSubnet,$sGateway,$sDNSServer
Local $sIP_Sum = ""
Local $sDNSServer_sum = ""
For $oConfig In $colConfig ;网络配置也是集合
If $oConfig.IPAddress <> "" Then ;防止IP为Null报错
For $sIP In $oConfig.IPAddress ;一块网卡IP地址可能有多个,是集合
$sNetInfo &= @TAB & "IP地址:" & $sIP & @CRLF
Next
EndIf
If $oConfig.IPSubnet <> "" Then ;掩码也有多个,防止Null报错(同一块网卡即使多IP,但掩码都相同)
For $sSubnet In $oConfig.IPSubnet
$sNetInfo &= @TAB & "子网掩码:" & $sSubnet & @CRLF
Next
EndIf
If $oConfig.DefaultIPGateway <> "" Then ;默认网关也有多个,防止Null报错(同一块网卡即使多IP,但默认网关都相同)
For $sGateway In $oConfig.DefaultIPGateway
$sNetInfo &= @TAB & "默认网关:" & $sGateway & @CRLF
Next
EndIf
If $oConfig.DHCPEnabled = True And $oConfig.IPEnabled = True Then ;一块网卡上的DHCP服务器是唯一的
$sNetInfo &= @TAB & "DHCP服务器:" & $oConfig.DHCPServer & @CRLF & _
@TAB & "DHCP租约获取时间:" & _FormatDateTime($oConfig.DHCPLeaseObtained) & @CRLF & _
@TAB & "DHCP租约过期时间:" & _FormatDateTime($oConfig.DHCPLeaseExpires) & @CRLF
EndIf
If $oConfig.DNSServerSearchOrder <> "" Then ;DNS服务器也有多个,防止Null报错
For $sDNSServer In $oConfig.DNSServerSearchOrder
$sNetInfo &= @TAB & "DNS服务器:" & $sDNSServer & @CRLF
Next
EndIf
If $oConfig.WINSPrimaryServer <> "" Then ;唯一
$sNetInfo &= @TAB & "主WINS服务器:" & $oConfig.WINSPrimaryServer & @CRLF
EndIf
If $oConfig.WINSSecondaryServer <> "" Then ;唯一
$sNetInfo &= @TAB & "备WINS服务器:" & $oConfig.WINSSecondaryServer & @CRLF
EndIf
Next
EndIf
Next
Return $sNetInfo
EndFunc
Func _bIsWin7Win2008()
Local $oWMI = ObjGet("winmgmts:\\.\root\cimv2")
Local $bIsWin7Win2008 = False
Local $colOS = $oWMI.InstancesOf("Win32_OperatingSystem")
Local $sOSVersion
For $oFoundItem In $colOS
$sOSVersion = $oFoundItem.Version
ExitLoop
Next
If StringLeft($sOSVersion,1) = "6" Then $bIsWin7Win2008 = True
Return $bIsWin7Win2008
EndFunc
Func _FormatDateTime($sDateTime)
Local $sFormatedDateTime = ""
If StringLeft($sDateTime,8) <> 0 And StringLeft($sDateTime,8) <> "" Then
$sFormatedDateTime = StringLeft($sDateTime,4) & "/" & StringMid($sDateTime,5,2) & "/" & StringMid($sDateTime,7,2) & " "
EndIf
If StringMid($sDateTime,9,6) <> 0 And StringMid($sDateTime,9,6) <> ""Then
$sFormatedDateTime = $sFormatedDateTime & StringMid($sDateTime,9,2) & ":" & StringMid($sDateTime,11,2) & ":" & StringMid($sDateTime,13,2)
EndIf
Return $sFormatedDateTime
EndFunc ;<==格式化输出日期和时间
|