从Win32_DiskDrive获取数据

时间:2015-01-18 12:23:40

标签: vbscript wmi

当我尝试通过VBScript从Win32_DiskDrive获取磁盘数据时,我遇到了问题。

这是我的代码:

dim objService, colDiskDrives, objItem 
dim DiskInfo, objClassProperty  

Set objService = GetObject("winmgmts:\\.\Root\CIMV2")
Set colDiskDrives = objService.ExecQuery("SELECT * FROM Win32_DiskDrive")
DiskInfo = "Disk Drive Info" & vbCrLf & vbCrLf   

For Each objItem in colDiskDrives          
  For Each objClassProperty In objItem.Properties_  
    DiskInfo = DiskInfo & objClassProperty.Name &" = " & objClassProperty.value &vbCrLf          
  Next     
  DiskInfo = DiskInfo & "----------------------------------------------" &vbCrLf &vbCrLf 
Next   
WScript.Echo DiskInfo

objClassProperty.value的值始终为null

1 个答案:

答案 0 :(得分:2)

值并非总是 null,但有时。有时它们也是数组,就像null值一样,数组也无法打印。

您的代码不检查任何一种情况。以下代码执行此操作,并检查对象和Nothing值。

Dim objService, colDiskDrives, objItem 
Dim DiskInfo, objClassProperty  

Set objService = GetObject("winmgmts:\\.\Root\CIMV2")
Set colDiskDrives = objService.ExecQuery("SELECT * FROM Win32_DiskDrive")

DiskInfo = "Disk Drive Info" & vbCrLf & vbCrLf   

For Each objItem in colDiskDrives
    For Each objClassProperty In objItem.Properties_
        DiskInfo = DiskInfo & objClassProperty.Name & " = " & StringVal(objClassProperty.value) & vbCrLf
    Next
    DiskInfo = DiskInfo & "----------------------------------------------" & vbCrLf & vbCrLf
Next

WScript.Echo DiskInfo

Function StringVal(value)
    If IsNull(value) Then
        StringVal = "[null]"
    ElseIf IsObject(value) Then
        If value Is Nothing Then StringVal = "[nothing]" Else StringVal = "[object]"
    ElseIf IsArray(value) Then
        StringVal = "[array]"
    Else
        StringVal = CStr(value)
    End If
End Function

为我打印

Availability = [null]
BytesPerSector = 512
Capabilities = [array]
CapabilityDescriptions = [array]
Caption = SAMSUNG SSD 830 Series
CompressionMethod = [null]
 ... more ...
-------------------------------