Tuesday, June 7, 2011

WMI Code example using VBScript

The following is a code example using VBScript and WMI:

More examples can be found at:

http://www.planetmps.com/



Dim svc
Dim objs
Dim obj
Dim prop
Dim Name
Dim Value

Set svc = GetObject("winmgmts:\\.\root\cimv2")
Set objs = svc.InstancesOf("Win32_Bios")
For Each obj In objs
    For Each prop In obj.Properties_
   
        Name = prop.Name
        Value = GetValue(prop.Name, obj)
        Debug.Print Name & " " & Value
   
    Next
   
Next


Private Function GetValue(ByVal Name, ByVal obj)
Dim pName
Dim pos
Dim tempstr
    pName = Name & " = "
    tempstr = obj.GetObjectText_
    pos = InStr(tempstr, pName)
   
    If pos <= 0 Then
        GetValue = ""
    Else
        tempstr = Mid(tempstr, pos + Len(pName), Len(tempstr))
        pos = InStr(tempstr, ";")
        tempstr = Mid(tempstr, 1, pos - 1)
        tempstr = Replace(tempstr, Chr(34), "")
        tempstr = Replace(tempstr, "{", "")
        tempstr = Replace(tempstr, "}", "")
        tempstr = Trim(tempstr)
        If obj.Properties_(Name).CIMType = 101 And Len(tempstr) > 14 Then
            tempstr = Mid(tempstr, 5, 2) & "/" & Mid(tempstr, 7, 2) & "/" & Mid(tempstr, 1, 4) & " " & Mid(tempstr, 9, 2) & ":" & Mid(tempstr, 11, 2) & ":" & Mid(tempstr, 13, 2)
        End If
        GetValue = tempstr
    End If
       
End Function

No comments:

Post a Comment