Wednesday, July 6, 2011

WSH VBScript WMI Example Using Instances

You can download the scripting program for generating these scripts at:

http://www.planetmps.com/wmiscriptsinstances.html

<?xml version='1.0' encoding='iso-8859-1'?>
<package>
<job>
<script language="vbscript">
<![CDATA[
Dim l
Dim svc
Dim ob
Dim objs
Dim obj
Dim prop
Dim tstr
Set l = CreateObject("WbemScripting.SWbemLocator")
Set svc = l.ConnectServer(".", "root\cimv2")
Set ob = svc.Get("Win32_Process")
Set objs = ob.Instances_
For each obj in objs
    For each prop in obj.Properties_
        tstr = tstr & prop.Name & " = " & GetValue(prop.Name, obj) & vbcrlf
    Next
WScript.Echo(tstr)
tstr = ""
Next
function GetValue(Name, obj)
   Dim tempstr
   Dim pname
   Dim pos
   pname = Name & " = "
   pos = InStr(obj.GetObjectText_, pname)
   If pos Then
      pos = pos + Len(pname)
      tempstr = Mid(obj.GetObjectText_, pos, Len(obj.GetObjectText_))
      pos = InStr(tempstr, ";")
      tempstr = Mid(tempstr, 1, pos - 1)
      tempstr = Replace(tempstr, Chr(34), "")
      tempstr = Replace(tempstr, "{", "")
      tempstr = Replace(tempstr, "}", "")
      If (obj.Properties_(Name).CIMtype = 101 And Len(tempstr) > 14) Then
         GetValue = Mid(tempstr, 5, 2) + "/" + _
                    Mid(tempstr, 7, 2) + "/" + _
                    Mid(tempstr, 1, 4) + " " + _
                    Mid(tempstr, 9, 2) + ":" + _
                    Mid(tempstr, 11, 2) + ":" + _
                    Mid(tempstr, 13, 2)
      Else
         GetValue = tempstr
      End If
   Else
      GetValue = ""
   End If
End Function
]]>
</script>
</job>
</package>

No comments:

Post a Comment