Jump to content

Get data from an object without knowing it's structure?


Recommended Posts

Hello.

Is there a way see what data is an object? What I mean by that is if have an object and we know a specific "key", we can access value via dot-annotation: $myObject.myKey

But what if we don't know the key? For ... In seems the way to go but when I tried $myObject[$myKey] it would fail with: 

Quote

Subscript used on non-accessible variable

Basically, I want to show structure of an object as json:

{ myKey: myValue }

Here is my code so far, it shows correct number of objects, but it doesn't show data in them:

Local $oWMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
Local $sQuery = "SELECT CommandLine, ProcessId FROM Win32_Process"
Local $oProcesses = $oWMI.ExecQuery($sQuery, "WQL", 0x30)

ConsoleWrite(object2string($oProcesses) & @CRLF)


func object2string($obj)
    local $ret = "", $val, $sKey
    for $val in $obj
        $sKey = ObjName($val)
        If IsObj($val) Then
            $val = object2string($val)
        EndIf
        $ret &= ($ret ? ", " : "") & $sKey & ": " & $val
    next
    return "{" & $ret & "}"
EndFunc

In this example we know that the data in $val variable can be accessed as $val.CommandLine and $val.ProcessId but what if we don't know that, how can we make it dynamic?

Link to comment
Share on other sites

; https://www.autoitscript.com/forum/topic/212366-get-data-from-an-object-without-knowing-its-structure/?do=findComment&comment=1537603

#include <String.au3>
#include <Array.au3>

$aArray = QueryWin32("SELECT ProcessId, CommandLine FROM Win32_Process")
_ArrayDisplay($aArray)

$aArray = QueryWin32("SELECT ProcessId, CommandLine, Caption, ThreadCount, ExecutablePath FROM Win32_Process")
_ArrayDisplay($aArray)

$aArray = QueryWin32('SELECT ProcessId, CommandLine, Caption, ThreadCount, ExecutablePath FROM Win32_Process WHERE Caption = "msedge.exe"')
_ArrayDisplay($aArray)

;--------------------------------------------------------------------------------------------------------------------------------
Func QueryWin32($sQueryString)
    Local $oWMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    ; $sQuery = "SELECT CommandLine, ProcessId FROM Win32_Process"
    Local $oProcesses = $oWMI.ExecQuery($sQueryString, "WQL", 0x30)

    Local $aPart = StringSplit(_StringBetween($sQueryString, "SELECT ", " FROM Win32_")[0], ", ", 1)
    Local $aResult[1][$aPart[0]]
    Local $idx = 0

    ; make header of the collection
    For $i = 1 To $aPart[0]
        $aResult[0][$i - 1] = $aPart[$i]
        ;ConsoleWrite($i & ") " & $aPart[$i] & @CRLF)
    Next

    ; make the value of the collection
    For $oProcess In $oProcesses
        $idx += 1
        ReDim $aResult[$idx + 1][$aPart[0]]

        For $i = 1 To $aPart[0]
            Local $sExeString = Execute('$oProcess.' & $aPart[$i])
            ;ConsoleWrite($aPart[$i] & "=" & $sExeString & @CRLF)
            $aResult[$idx][$i - 1] = $sExeString
        Next
    Next

    Return $aResult

EndFunc   ;==>QueryWin32process


#cs
      string   CreationClassName;
      string   Caption;
      string   CommandLine;
      datetime CreationDate;
      string   CSCreationClassName;
      string   CSName;
      string   Description;
      string   ExecutablePath;
      uint16   ExecutionState;
      string   Handle;
      uint32   HandleCount;
      datetime InstallDate;
      uint64   KernelModeTime;
      uint32   MaximumWorkingSetSize;
      uint32   MinimumWorkingSetSize;
      string   Name;
      string   OSCreationClassName;
      string   OSName;
      uint64   OtherOperationCount;
      uint64   OtherTransferCount;
      uint32   PageFaults;
      uint32   PageFileUsage;
      uint32   ParentProcessId;
      uint32   PeakPageFileUsage;
      uint64   PeakVirtualSize;
      uint32   PeakWorkingSetSize;
      uint32   Priority;
      uint64   PrivatePageCount;
      uint32   ProcessId;
      uint32   QuotaNonPagedPoolUsage;
      uint32   QuotaPagedPoolUsage;
      uint32   QuotaPeakNonPagedPoolUsage;
      uint32   QuotaPeakPagedPoolUsage;
      uint64   ReadOperationCount;
      uint64   ReadTransferCount;
      uint32   SessionId;
      string   Status;
      datetime TerminationDate;
      uint32   ThreadCount;
      uint64   UserModeTime;
      uint64   VirtualSize;
      string   WindowsVersion;
      uint64   WorkingSetSize;
      uint64   WriteOperationCount;
      uint64   WriteTransferCount;
#CE

 

Edited by ioa747
Update

I know that I know nothing

Link to comment
Share on other sites

Thank you, but this is "cheating", you are getting "keys" from query string. I'm trying to come up with a universal method to dissect already existing object with unknown structure.

Link to comment
Share on other sites

<snip>

Edited by TheXman
Semper volens adiuvare qui merentur
Link to comment
Share on other sites

  • 4 weeks later...
2 minutes ago, junkew said:

Would eval function help?

How would eval reveal what's inside an object?

2 minutes ago, junkew said:

In which circumstance you wouldnt know the objectstructure or subfields.

A "simple" debug function that would output in console whatever was provided to it. So, I could simply add debug($myvar) anywhere in the code and don't worry about providing any additional information about what that $myvar is.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...