Widows objects discovery from WMI

All development related issues welcome

Moderator: Moderator Team

Locked
alexei
Posts: 137
Joined: Wed Oct 19, 2005 5:29 pm

Widows objects discovery from WMI

Post by alexei »

ROS developers may already know about that, but just in case...
With WMI you can enumerate all Namespaces, then go down to Objects and get their Properties and Methods.
For properties and Methods you have Qualifiers, and also Parameters for Methods.
There are a lot of enumerations available, as well as detailed descriptions.
They are available in plain text.
It's rather simple to write a script, which would go through all that stuff and generate C code to use in ROS.
That would save a lot of typing, assure name-compatibility, and be perfectly legal being made out of program output, not from a source code.
Just couple examples, as I played with that a while ago. (Sorry :) pascal syntax generated, descriptions shortened).

Code: Select all

  Class RsopPlanningModeProvider  /*Implemented;Static*/
    RsopCreateSession(computerGPOFilters:string;
          computerName:string;
          computerSecurityGroups:string;
          computerSOM:string;
          flags:uint32;
          site:string;
          userGPOFilters:string;
          userName:string;
          userSecurityGroups:string;
          userSOM:string;);  /*Implemented;Static*/
    RsopDeleteSession(namespace:string;);

Code: Select all

  Class MSFT_CliConnection
    Authority: STRING; //      DSCR:Optional. If the strAuthority paramet...
    Locale: STRING; //      DSCR:Optional. String that specifies the l...
    NameSpace: STRING; //      DSCR:Optional. String that specifies the n...
    Password: STRING; //      DSCR:Optional. String that specifies the p...
    Server: STRING; //      DSCR:Optional. For access to a remote comp...
    User: STRING; //      DSCR:Optional. String that specifies the u...
andrewweb
Posts: 263
Joined: Fri Jun 01, 2007 3:59 pm
Location: United Kingdom
Contact:

Re: Widows objects discovery from WMI

Post by andrewweb »

This is a really good idea!
zed260
Posts: 105
Joined: Thu Oct 02, 2008 2:10 am
Location: cleveland tn
Contact:

Re: Widows objects discovery from WMI

Post by zed260 »

impressive
Haos
Test Team
Posts: 2954
Joined: Thu Mar 22, 2007 5:42 am
Contact:

Re: Widows objects discovery from WMI

Post by Haos »

It would still need a huge amount of research, especially in low level WMI components.
alexei
Posts: 137
Joined: Wed Oct 19, 2005 5:29 pm

Re: Widows objects discovery from WMI

Post by alexei »

It's not 100% complete, see notes at the bottom.

usage:
cscript /nologo WMI.VBS > yourwmi.txt

WMI.VBS

Code: Select all

' see http://msdn.microsoft.com/en-us/library/aa393650(VS.85).aspx
' http://msdn.microsoft.com/en-us/library/aa384827(v=VS.85).aspx
' http://technet.microsoft.com/en-us/library/ee156571.aspx

'ModelCorrespondence	Data type: string array
'  Applies to: properties
'Set of values that indicate correspondence between an object's
'property and other properties in the CIM schema.
'The default is NULL.
'Object properties are identified using the following syntax.
'<schema name> "_" <class or association name> "." <property name>
option explicit
dim strComputer

strComputer = "."

dim strQual
strQual = vbCRLF

call EnumNameSpaces("root")

strQual = Join(SortArray(Split(strQual,vbCRLF)),vbCRLF)
wscript.echo strQual

sub EnumNameSpaces(strNameSpace)
  dim objWMIService,colNameSpaces,objNameSpace
'  On Error Resume next
  wscript.echo "**********************************************************"
  wscript.echo strNameSpace
  call ListClasses(strNameSpace)
  set objWMIService=GetObject( _
     "winmgmts:{impersonationLevel=impersonate}\\" & _
      strComputer & "\" & strNameSpace)
  set colNameSpaces = objWMIService.InstancesOf("__NAMESPACE")
  for each objNameSpace in colNameSpaces
    if strNameSpace & "\" & objNameSpace.Name <> "root\directory" then
      call EnumNameSpaces(strNameSpace & "\" & objNameSpace.Name)
    else
      wscript.echo "SKIP root\directory"
    end if
  next
end sub

'AMENDMENT=True
'LOCALE=1033
sub ListClasses(strNameSpace)
  dim objWMIService,objclass,objClassProperty,objQualifier
  set objWMIService = GetObject( _
    "winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\" & strNameSpace)
  for each objclass in objWMIService.SubclassesOf()
    wscript.echo "  Class " & objClass.Path_.Class
    call DumpProp(objClass,"Class")
    call DumpMethod(objClass)
  next
  set objWMIService = Nothing
end sub

function Shorten(str,lmax) ' to show descriptions (DBG?)
  dim wl,ws
  wl = Len(str)
  ws = Left(str,lmax)
  if wl > Len(ws) then
    Shorten = ws & "..."
  else
    Shorten = str
  end if
end function

' Qualifier "ValueMap" corresponds to qualifier "Values" or "DefineValues" or both
' also "ValueDescriptions" may be present
' cpp_quote - looks like C++ quotation - should go to description

' Should return a record:
' - name
' - type
' - description
' - other comments, like /* Key=true */
' - enumeration functions
' -
'=================================
'QLF:CIMTYPE=uint64
'QLF:CounterType=1073741824
'QLF:PerfDefault=True
'QLF:read=True
'QLF:WmiDataId=449
'QLF:CIMTYPE
      'QLF:CIM_Key=True
      'QLF:CIMTYPE=string
      'QLF:MaxLen=256
      'QLF:Propagated=CIM_System.Name
      'QLF:read=True
       'QLF:not_null=True
'/*Class Property*/ SettingID: string
'QLF:MaxLen=256
'QLF:Override=SettingId
'QLF:read=True

'  /*Class Property*/ Arg4: uint64
'    QLF:DisplayInHex=True
'    QLF:read=True
'    QLF:WmiDataId=9

function CutCRLF(str)
  if Right(str,Len(vbCRLF)) = vbCRLF then
    CutCRLF = Left(str,Len(str) - Len(vbCRLF))
  else
    CutCRLF = str
  end if
end function

function NmOpt(opt,val)
  if TypeName(val) <> "Boolean" then
'wscript.echo "+++++ " & opt & "TN=" & TypeName(val)
'wscript.echo "+++++ E=" & IsEmpty(val)
'wscript.echo "+++++ N=" & IsNull(val)
'wscript.echo "+++++ A=" & IsArray(val)
'wscript.echo "+++++ O=" & IsObject(val)
    if IsArray(val) then
      NmOpt = Opt & "=" & Join(val," ")
    else
      NmOpt = Opt & "=" & val
    end if
    exit function
  end if
  if val then
    NmOpt = opt
  else
    NmOpt = "Not-" & opt
  end if
end function

function DumpQual(obj,param,name)
  dim objQual,wout,wqlf,wrslt
  dim wval,wmap,wdef,wk,welm,wdscr,ws,woname,wf,wopt,wopt2,wsmap
  wrslt = ""
  wout = ""
  wqlf = ""
  wval = Null
  wf = ""
  wopt = ""
  wopt2 = ""
  wsmap = ""
  woname = obj.Name
  for each objQual in obj.Qualifiers_
    select case Lcase(objQual.Name)
      case "cimtype"
        if objQual.Name <> "CIMTYPE" then
          wscript.echo "ERROR on CIMTYPE"
        end if
      case "values"
        wval = objQual.Value
      case "valuemap"
        wmap = objQual.Value
      case "definevalues"
        wdef = objQual.Value
      case "valuedescriptions"
        wdscr = objQual.Value
      case "description"
        wout = wout & Shorten(StrArr(objQual.Value,vbCRLF & "      DSCR:"),50) & vbCRLF
      case "displayname"
        wout = wout & "      SHOW:""" & objQual.Value & """"
      case "bitvalues"
        wout = wout & StrArr(objQual.Value,"      BITS:") & vbCRLF
      case "mappingstrings"
        if VarType(objQual.Value) = (vbVariant + vbArray) then
          wsmap = wsmap & "MAP:" & _
            """" & Join(objQual.Value,""",""") & """" & vbCRLF
        else
          wsmap = wsmap & "MAP:" & _
            "(""" & Replace(objQual.Value,"|",""",""") & """)" & vbCRLF
        end if
      case "units"
        wopt = wopt & "UNITS:" & objQual.Value & ";"
      case "subtype"
        wopt = wopt & "SUBTYPE:" & objQual.Value & ";"
      case "maxlen"
        wopt = wopt & "MaxLen=" & objQual.Value & ";"
      case "override"
        wopt = wopt & "Override=" & objQual.Value & ";"
      case "arraytype"
        wopt = wopt & "ArrayType=" & objQual.Value & ";"
      case "key"
        wopt = wopt & NmOpt("Key",objQual.Value) & ";"
      case "cim_key"
        wopt = wopt & NmOpt("CIM_Key",objQual.Value) & ";"
      case "write"
        wopt = wopt & NmOpt("Write",objQual.Value) & ";"
      case "read"
        wopt = wopt & NmOpt("Read",objQual.Value) & ";"
      case "template"
        wopt = wopt & NmOpt("Template",objQual.Value) & ";"
      case "deprecated"
        wopt = wopt & NmOpt("Deprecated",objQual.Value) & ";"
      case "static"
        wopt = wopt & NmOpt("Static",objQual.Value) & ";"
      case "implemented"
        wopt = wopt & NmOpt("Implemented",objQual.Value) & ";"
      case "experimental"
        wopt = wopt & NmOpt("Experimental",objQual.Value) & ";"
      case "not_null"
        wopt = wopt & NmOpt("NotNULL",objQual.Value) & ";"
      case "fixed"
        wopt = wopt & NmOpt("Fixed",objQual.Value) & ";"
      case "constructor"
        wopt = wopt & NmOpt("Constructor",objQual.Value) & ";"
      case "destructor"
        wopt = wopt & NmOpt("Destructor",objQual.Value) & ";"
      case "propagated"
        wopt2 = wopt2 & "Propagated=" & objQual.Value & ";"
      case "modelcorrespondence"
        wopt2 = wopt2 & StrArr(objQual.Value,"ModelCorrespondence=") & ";"
      case else
        if VarType(objQual.Value) = (vbVariant + vbArray) then
          wqlf = wqlf & "      QLF:" & _
            objQual.Name & "=" & Join(objQual.Value,vbCRLF & "      ")
        else
          wqlf = wqlf & "      QLF:" & _
          objQual.Name & "=" & objQual.Value & vbCRLF
        end If
        call CollectQlf(param & "-" & objQual.Name,objQual.Value)
    end select
  next
  if wopt2 <> "" then
    wopt = wopt & vbCRLF & "      " & wopt2
  end if
  if wopt <> "" then wopt = Replace(" /*" & wopt & "*/",";*/","*/")
  if not IsNull(wval) then
    '''wscript.echo "VALtn=" & TypeName(wval)
    '''wscript.echo "MAPtn=" & TypeName(wmap)
    '''wscript.echo "DEFtn=" & TypeName(wdef)
    if IsEmpty(wmap) then 'enumerated (kind of)
      ' Array Constant
      wscript.echo "    const " & _
        name & "_ENUM_" & woname & " = (""" &  Join(wval,""",""") & """);"
    else
      ' Decoding Function
      wk = 0
      wf = wf & "select case " & woname & vbCRLF
      for each welm in wval
        '''wscript.echo "ETN=" & TypeName(welm)
        wf = wf & "  case " & wmap(wk) & " RSLT=""" & welm & """"
        '''wscript.echo "TN=" & TypeName(wmap)
        if not IsEmpty(wdef) then
          if wk <= Ubound(wdef) then
            wf = wf & " defval=" & wdef(wk)
          end if
        end if
        if not IsEmpty(wdscr) then
          if wk <= Ubound(wdscr) then
            wf = wf &  " /* " & wdscr(wk) & " */"
          end if
        end if
        wf = wf & vbCRLF
        wk = wk+1
      next
      wf = wf & "end case '" & woname & vbCRLF
    end if
  end if
  if wqlf <> "" then wrslt = wrslt & wqlf
  if wout <> "" then wrslt = wrslt & wout
  if wf <> "" then wrslt = wrslt & wf
  if wopt <> "" then wrslt = wrslt & wopt
  if wsmap <> "" then
    wrslt = wrslt & vbCRLF & "      /*" &_
              Replace(wsmap & "*/",vbCRLF & "*/","*/" & vbCRLF)
  end if
  DumpQual = wrslt
end function

sub CollectQlf(name,val)
  if InStr(strQual,vbCRLF & name & "=") = 0 then
    strQual = strQual & name & "=" & Shorten(StrArr(val,""),30)  & vbCRLF
  end if
end sub

sub DumpProp(obj,param)
  dim objProp,ws
  for each objProp in obj.Properties_
    'wscript.echo "    /*" & param & " Property*/ " & objProp.Name & ": " &_
    '                               objProp.Qualifiers_("CIMTYPE")
    ws = DumpQual(objProp,param,obj.Path_.Class)
    ws = Replace(ws,";);",");")
    wscript.echo "    " &  objProp.Name & ": " & _
                 objProp.Qualifiers_("CIMTYPE") & "; " & _
                 CutCRLF(ws)
  next
end sub

function ObjType(obj)
  on error resume next
  ObjType = ""
  ObjType = obj.Qualifiers_("CIMTYPE")
end function


'Method-bypass_getobject
'Method-cpp_quote
'Method-HeaderName
'Method-Privileges
'Method-Schema
'Method-WmiMethodId
sub DumpMethod(obj)
  dim objMethod,objInParam,wres,ws
  for each objMethod in obj.Methods_
    wres = "    " & objMethod.Name & "("
    wscript.echo DumpQual(objMethod,"Method",obj.Path_.Class)
    '''wscript.echo " VT=" & VarType(objMethod.inParameters) & " TN=" & TypeName(objMethod.inParameters)
    if TypeName(objMethod.inParameters) <> "Nothing" then
      set objInParam = objMethod.inParameters.SpawnInstance_()
      '''wscript.echo "Param TypeName=" & TypeName(objInParam)
      wres = wres & DumpParams(objInParam)  ' parameters
    end if
    ws = ObjType(obj)
    if ws = "" then ' it's procedure
      wres = CutCRLF(wres) & ");"
    else ' it's function
      wres = CutCRLF(wres) & "):" & ws & ";" ' return type
    end if
    set objInParam = Nothing
    wscript.echo CutCRLF(wres)
  next
end sub


function StrArr(val,str) ' handles both strings and string arrays
    on error resume next
    StrArr = "?????"
    if VarType(Val) = (vbVariant + vbArray) then
      StrArr = str & "[" & Join(Val,";") & "]"
    else
      StrArr = str & Val
    end if
end function

function DumpParams(obj)
  dim objProp,objQual,wtype,wdesc,wid,wres
  wres = ""
  for each objProp in obj.Properties_ ' individual parameters
    wtype = ""
    wdesc = ""
    wid = ""
    for each objQual in objProp.Qualifiers_
      if objQual.Name = "CIMTYPE" then wtype = objQual.Value
      if objQual.Name = "ID" then wid = objQual.Value
      if objQual.Name = "Description" then wdesc = StrArr(objQual.Value,"")
      ' other ??????????????????
    next
    '''wscript.echo "      Param: " & objProp.Name & " " & wtype & ";" & wdesc

    ' name:type
    if wres = "" then
      wres = objProp.Name & ":" & wtype
    else
      wres = wres & "          " & objProp.Name & ":" & wtype
    end if
    if wdesc <> "" then
      wres = wres & ";  /* " & wdesc & "*/"
    else
      wres = wres & ";"
    end if
    wres = wres & vbCRLF
  next
  DumpParams = wres
end function

function SortArray(parr)
  dim aList,wi,wx,wres
  set aList = CreateObject("System.Collections.ArrayList" )
  for wi = 0 to Ubound(parr)
    aList.Add Trim(parr(wi))
  next
  aList.Sort
  redim wres(Ubound(parr))
  wi = 0
  for each wx in aList
    wres(wi) = wx
    wi = wi+1
  next
  SortArray = wres
  set aList = Nothing
end function

' Qualifier "ValueMap" corresponds to qualifier "Values" or "DefineValues" or both
' also "ValueDescriptions" may be present
' cpp_quote - looks like C++ quotation - should go to description
'make constants of then
' ID, WmiDataID? , etc. - make them comments
' DisplayName - same thing

' MappingStrings=
'   follows the list of strings separated with "|"
rchateauneu
Posts: 1
Joined: Wed Jul 05, 2017 12:07 pm

Re: Widows objects discovery from WMI

Post by rchateauneu »

By the way, does WMI works on ReactOS ? If not, it might be possible to use WBEM which is open source (See OpenLMI , OpenPegasus etc ... )
User avatar
dizt3mp3r
Posts: 1874
Joined: Mon Jun 14, 2010 5:54 pm

Re: Widows objects discovery from WMI

Post by dizt3mp3r »

OOOh, someone is going to smack you for raising a thread from the dead.
Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.
Xbase
Posts: 32
Joined: Thu Apr 19, 2018 2:08 pm

Re: Widows objects discovery from WMI

Post by Xbase »

hi,

there is a Tool called "WMI Code Creater" from MS
https://www.microsoft.com/en-us/downloa ... px?id=8572

"The WMI Code Creator tool allows you to generate VBScript, C#, and VB .NET code that uses WMI to complete a management task such as querying for management data, executing a method from a WMI class, or receiving event notifications using WMI."

but i can't get it run on ReactOS (have try Compatible Mode) :(
User avatar
EmuandCo
Developer
Posts: 4723
Joined: Sun Nov 28, 2004 7:52 pm
Location: Germany, Bavaria, Steinfeld
Contact:

Re: Widows objects discovery from WMI

Post by EmuandCo »

You can't be serious to post on a 6 years old thread can you?! LOCKED
ReactOS is still in alpha stage, meaning it is not feature-complete and is recommended only for evaluation and testing purposes.

If my post/reply offends or insults you, be sure that you know what sarcasm is...
Locked

Who is online

Users browsing this forum: No registered users and 12 guests