Sign in to follow this  
Followers 0
taurus905

Objects and/or Properties in an Array

20 posts in this topic

#1 ·  Posted (edited)

I am looking for a way to put the following in to an array:

$objItem.Availability

$objItem.BytesPerSector

$objItem.Capabilities

$objItem.CapabilityDescriptions

$objItem.Caption

$objItem.CompressionMethod

$objItem.ConfigManagerErrorCode

$objItem.ConfigManagerUserConfig

$objItem.CreationClassName

$objItem.DefaultBlockSize

$objItem.Description

$objItem.DeviceID

$objItem.ErrorCleared

$objItem.ErrorDescription

$objItem.ErrorMethodology

$objItem.Index

$objItem.InstallDate

$objItem.InterfaceType

$objItem.LastErrorCode

$objItem.Manufacturer

$objItem.MaxBlockSize

$objItem.MaxMediaSize

$objItem.MediaLoaded

$objItem.MediaType

$objItem.MinBlockSize

$objItem.Model

$objItem.Name

$objItem.NeedsCleaning

$objItem.NumberOfMediaSupported

$objItem.Partitions

$objItem.PNPDeviceID

$objItem.PowerManagementCapabilities

$objItem.PowerManagementSupported

$objItem.SCSIBus

$objItem.SCSILogicalUnit

$objItem.SCSIPort

$objItem.SCSITargetId

$objItem.SectorsPerTrack

$objItem.Signature

$objItem.Size

$objItem.Status

$objItem.StatusInfo

$objItem.SystemCreationClassName

$objItem.SystemName

$objItem.TotalCylinders

$objItem.TotalHeads

$objItem.TotalSectors

$objItem.TotalTracks

$objItem.TracksPerCylinder

to make this script much shorter and more versatile.

I have tried different methods, but can't seem to find a way that works.

Is this possible with objects and/or properties?

Thank you for suggestions.

taurus905

; Generated by AutoIt Scriptomatic

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      $Output = $Output & "Availability: " & $objItem.Availability & @CRLF
      $Output = $Output & "BytesPerSector: " & $objItem.BytesPerSector & @CRLF
      $strCapabilities = $objItem.Capabilities(0)
      $Output = $Output & "Capabilities: " & $strCapabilities & @CRLF
      $strCapabilityDescriptions = $objItem.CapabilityDescriptions(0)
      $Output = $Output & "CapabilityDescriptions: " & $strCapabilityDescriptions & @CRLF
      $Output = $Output & "Caption: " & $objItem.Caption & @CRLF
      $Output = $Output & "CompressionMethod: " & $objItem.CompressionMethod & @CRLF
      $Output = $Output & "ConfigManagerErrorCode: " & $objItem.ConfigManagerErrorCode & @CRLF
      $Output = $Output & "ConfigManagerUserConfig: " & $objItem.ConfigManagerUserConfig & @CRLF
      $Output = $Output & "CreationClassName: " & $objItem.CreationClassName & @CRLF
      $Output = $Output & "DefaultBlockSize: " & $objItem.DefaultBlockSize & @CRLF
      $Output = $Output & "Description: " & $objItem.Description & @CRLF
      $Output = $Output & "DeviceID: " & $objItem.DeviceID & @CRLF
      $Output = $Output & "ErrorCleared: " & $objItem.ErrorCleared & @CRLF
      $Output = $Output & "ErrorDescription: " & $objItem.ErrorDescription & @CRLF
      $Output = $Output & "ErrorMethodology: " & $objItem.ErrorMethodology & @CRLF
      $Output = $Output & "Index: " & $objItem.Index & @CRLF
      $Output = $Output & "InstallDate: " & WMIDateStringToDate($objItem.InstallDate) & @CRLF
      $Output = $Output & "InterfaceType: " & $objItem.InterfaceType & @CRLF
      $Output = $Output & "LastErrorCode: " & $objItem.LastErrorCode & @CRLF
      $Output = $Output & "Manufacturer: " & $objItem.Manufacturer & @CRLF
      $Output = $Output & "MaxBlockSize: " & $objItem.MaxBlockSize & @CRLF
      $Output = $Output & "MaxMediaSize: " & $objItem.MaxMediaSize & @CRLF
      $Output = $Output & "MediaLoaded: " & $objItem.MediaLoaded & @CRLF
      $Output = $Output & "MediaType: " & $objItem.MediaType & @CRLF
      $Output = $Output & "MinBlockSize: " & $objItem.MinBlockSize & @CRLF
      $Output = $Output & "Model: " & $objItem.Model & @CRLF
      $Output = $Output & "Name: " & $objItem.Name & @CRLF
      $Output = $Output & "NeedsCleaning: " & $objItem.NeedsCleaning & @CRLF
      $Output = $Output & "NumberOfMediaSupported: " & $objItem.NumberOfMediaSupported & @CRLF
      $Output = $Output & "Partitions: " & $objItem.Partitions & @CRLF
      $Output = $Output & "PNPDeviceID: " & $objItem.PNPDeviceID & @CRLF
      $strPowerManagementCapabilities = $objItem.PowerManagementCapabilities(0)
      $Output = $Output & "PowerManagementCapabilities: " & $strPowerManagementCapabilities & @CRLF
      $Output = $Output & "PowerManagementSupported: " & $objItem.PowerManagementSupported & @CRLF
      $Output = $Output & "SCSIBus: " & $objItem.SCSIBus & @CRLF
      $Output = $Output & "SCSILogicalUnit: " & $objItem.SCSILogicalUnit & @CRLF
      $Output = $Output & "SCSIPort: " & $objItem.SCSIPort & @CRLF
      $Output = $Output & "SCSITargetId: " & $objItem.SCSITargetId & @CRLF
      $Output = $Output & "SectorsPerTrack: " & $objItem.SectorsPerTrack & @CRLF
      $Output = $Output & "Signature: " & $objItem.Signature & @CRLF
      $Output = $Output & "Size: " & $objItem.Size & @CRLF
      $Output = $Output & "Status: " & $objItem.Status & @CRLF
      $Output = $Output & "StatusInfo: " & $objItem.StatusInfo & @CRLF
      $Output = $Output & "SystemCreationClassName: " & $objItem.SystemCreationClassName & @CRLF
      $Output = $Output & "SystemName: " & $objItem.SystemName & @CRLF
      $Output = $Output & "TotalCylinders: " & $objItem.TotalCylinders & @CRLF
      $Output = $Output & "TotalHeads: " & $objItem.TotalHeads & @CRLF
      $Output = $Output & "TotalSectors: " & $objItem.TotalSectors & @CRLF
      $Output = $Output & "TotalTracks: " & $objItem.TotalTracks & @CRLF
      $Output = $Output & "TracksPerCylinder: " & $objItem.TracksPerCylinder & @CRLF
      if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_DiskDrive" )
Endif


Func WMIDateStringToDate($dtmDate)

    Return (StringMid($dtmDate, 5, 2) & "/" & _
    StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
    & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc
Edited by taurus905

"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
Share on other sites



Weaponx wrote this code the other day.

$oObj = ObjCreate('Scripting.Dictionary') ;Sample object

$oTLA = ObjCreate("TLI.TLIApplication")
$objInterface = $oTLA.InterfaceInfoFromObject($oObj)

$oMembers = $objInterface.Members

For $member In $oMembers
    ConsoleWrite($member.Name & @CRLF)
Next

Maybe you could do the same thing?

Share this post


Link to post
Share on other sites

Weaponx wrote this code the other day.

$oObj = ObjCreate('Scripting.Dictionary') ;Sample object

$oTLA = ObjCreate("TLI.TLIApplication")
$objInterface = $oTLA.InterfaceInfoFromObject($oObj)

$oMembers = $objInterface.Members

For $member In $oMembers
    ConsoleWrite($member.Name & @CRLF)
Next

Maybe you could do the same thing?

Hello Richard Robertson,

Thank you for your response.

I read the thread you referred to:

http://www.autoitscript.com/forum/index.ph...=TLIApplication

But this isn't really what I was looking for.

I am able to read all the properties to an array.

But I am not able to get the following to output properly:

For $objItem In $colItems
            For $i = 1 To UBound($a_objItem) - 1
                $s_wmi_Data = $s_wmi_Data & $a_objItem[$i] & @TAB
            Next
            $s_wmi_Data = $s_wmi_Data & @CRLF
        Next

The $a_objItem[$i] contains:

$objItem.Availability

$objItem.BytesPerSector

$objItem.Capabilities

$objItem.CapabilityDescriptions

$objItem.Caption

etc . . .

I know the syntax is wrong.

And I would like to find a similar way to make this work with an array.

Thank you again for your help.

taurus905


"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
Share on other sites

I was just wondering if anyone had any ideas on this.

Thank you in advance.

taurus905


"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
Share on other sites

I was just wondering if anyone had any ideas on this.

Thank you in advance.

taurus905

I haven't messed with COM that much in AutoIt3 yet but it looks like For In Next is used

to enumerate COM object properties. You may try VarGetType to determine what each

element type is or just use String to get a string representation of each element and

manipulate it.

If not that then I'm not seeing what you're trying to do.

Share this post


Link to post
Share on other sites

I haven't messed with COM that much in AutoIt3 yet but it looks like For In Next is used

to enumerate COM object properties. You may try VarGetType to determine what each

element type is or just use String to get a string representation of each element and

manipulate it.

If not that then I'm not seeing what you're trying to do.

Hello MilesAhead,

Thank you for your interest in my issue.

I should have taken the time to explain earlier exactly what I am trying to do.

This example works:

; Generated by AutoIt Scriptomatic

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) Then
   For $objItem In $colItems
      $Output = $Output & "Index: " & $objItem.Index & @CRLF
      $Output = $Output & "InterfaceType: " & $objItem.InterfaceType & @CRLF
      $Output = $Output & "Model: " & $objItem.Model & @CRLF
      $Output = $Output & "Name: " & $objItem.Name & @CRLF
      $Output = $Output & "Size: " & $objItem.Size & @CRLF
      If Msgbox(1,"WMI Output",$Output) = 2 Then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_DiskDrive" )
EndIf

Func WMIDateStringToDate($dtmDate)
    Return (StringMid($dtmDate, 5, 2) & "/" & _
    StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
    & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc

And this one does not give the desired output:

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
Dim $a_Property[6]; Array
$a_Property[1] = "Index"
$a_Property[2] = "InterfaceType"
$a_Property[3] = "Model"
$a_Property[4] = "Name"
$a_Property[5] = "Size"

If IsObj($colItems) Then
    For $objItem In $colItems
        For $i = 1 To UBound($a_Property) - 1
            $Output = $Output & $a_Property[$i] & ": " & $objItem & "." & $a_Property[$i] & @CRLF
        Next
        If Msgbox(1,"WMI Output",$Output) = 2 Then ExitLoop
        $Output=""
    Next
Else
    Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_DiskDrive" )
EndIf

So I am looking for an array solution for these properties.

I am not sure if it is possible, but I thought I would ask if anyone knows of anything I am overlooking.

Thank you for taking the time to help.

taurus905


"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Why does it have to be an array?

And why wouldn't this work?

#include <array.au3>
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
Dim $i_add, $a_Property[6][2]; Array
$a_Property[1][0] = "Index"
$a_Property[2][0] = "InterfaceType"
$a_Property[3][0] = "Model"
$a_Property[4][0] = "Name"
$a_Property[5][0] = "Size"

If IsObj($colItems) Then
    For $objItem In $colItems
        $i_add += 5
        If $i_add > 5 Then
            ReDim $a_Property[$i_add + 1][2]
        EndIf
        $a_Property[$i_add - 4][1] = $objItem.Index
        $a_Property[$i_add - 3][1] = $objItem.InterfaceType
        $a_Property[$i_add - 2][1] = $objItem.Model
        $a_Property[$i_add - 1][1] = $objItem.Name
        $a_Property[$i_add][1] = $objItem.Size
    Next
Else
    Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_DiskDrive" )
EndIf
_ArrayDisplay($a_Property)
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Why does it have to be an array?

And why wouldn't this work?

#include <array.au3>
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
Dim $i_add, $a_Property[6][2]; Array
$a_Property[1][0] = "Index"
$a_Property[2][0] = "InterfaceType"
$a_Property[3][0] = "Model"
$a_Property[4][0] = "Name"
$a_Property[5][0] = "Size"

If IsObj($colItems) Then
    For $objItem In $colItems
        $i_add += 5
        If $i_add > 5 Then
            ReDim $a_Property[$i_add + 1][2]
        EndIf
        $a_Property[$i_add - 4][1] = $objItem.Index
        $a_Property[$i_add - 3][1] = $objItem.InterfaceType
        $a_Property[$i_add - 2][1] = $objItem.Model
        $a_Property[$i_add - 1][1] = $objItem.Name
        $a_Property[$i_add][1] = $objItem.Size
    Next
Else
    Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_DiskDrive" )
EndIf
_ArrayDisplay($a_Property)
Hello SmOke_N,

I appreciate your question on why your example won't work for what I am trying to build. For simplicity, my example showed the properties in an array. But I am trying to create a generic script which will get the desired properties from an external file. So they will need to be passed as a variable, as opposed to being hard-coded in the script.

If this is not possible, I will have to find an alternative to an array.

Thank you for your help.

taurus905


"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
Share on other sites

I thought Weaponx had done something like this before (with Scripting Dictionary???)... but for the life of me, can't remember what post it was in.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

I might be reading this wrong, but are you saying you want to read properties by string name instead of hard coded?

Some objects in some languages support property look up like obj["prop"]. I have never worked with COM in AutoIt, so someone might be able to tell me if that works or not.

Share this post


Link to post
Share on other sites

I might be reading this wrong, but are you saying you want to read properties by string name instead of hard coded?

Some objects in some languages support property look up like obj["prop"]. I have never worked with COM in AutoIt, so someone might be able to tell me if that works or not.

Hello Richard Robertson,

I want to read the value of $objItem.Model to a variable. Model is the property, but I do not want Model to be hard-coded. So Model has to be in a variable also.

Instead of:

$objItem.Model

I would like to concatenate the object and the property, where $a_Property[1] = "Model"

$objItem & "." & $a_Property[1]

I hope this helps clear up what I am attempting.

Thank you.

taurus905


"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
Share on other sites

Have you tried Eval("obj." & $prop[1])? I dunno if it works for objects though.

Share this post


Link to post
Share on other sites

Have you tried Eval("obj." & $prop[1])? I dunno if it works for objects though.

That won't work.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

I went through the entire help file and tried every command which might produce the proper result. None of them worked, so I thought I would start this thread to get other ideas.

taurus905


"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
Share on other sites

I dunno then. Sorry.

Share this post


Link to post
Share on other sites

I went through the entire help file and tried every command which might produce the proper result. None of them worked, so I thought I would start this thread to get other ideas.

taurus905

It sounds like you are trying to serialize a hash table, or associative array. Since I don't know of any built in support or UDF for it in AutoIt it may be easier to use some kind of simple database or a 3rd party library that can store and retrieve hash tables, dictionaries, associative arrays or whatever is the current fashionable term. Seems like to roll your own you would need to use Automation Compatible Types like in COM. If the values can be represented as strings then perhaps you could cheat and just use alternating lines in a .txt file. The odd lines being the var type string name or description and the even lines being the corresponding stringized variable values... then just type cast them when you read them.

As for the WMI stuff I'm on Vista and all that is hosed so I haven't even looked into that stuff. :P

Share this post


Link to post
Share on other sites

It sounds like you are trying to serialize a hash table, or associative array. Since I don't know of any built in support or UDF for it in AutoIt it may be easier to use some kind of simple database or a 3rd party library that can store and retrieve hash tables, dictionaries, associative arrays or whatever is the current fashionable term. Seems like to roll your own you would need to use Automation Compatible Types like in COM. If the values can be represented as strings then perhaps you could cheat and just use alternating lines in a .txt file. The odd lines being the var type string name or description and the even lines being the corresponding stringized variable values... then just type cast them when you read them.

As for the WMI stuff I'm on Vista and all that is hosed so I haven't even looked into that stuff. :P

Hello MilesAhead,

This morning I was leaning towards an associative array. I felt that if I could enumerate the properties of an object, then I should be able to do the same to the values.

Thank you for your suggestions.

taurus905


"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
Share on other sites

I had this issue quite some time back. Check this out.

http://www.autoitscript.com/forum/index.ph...st&p=389577

Hello spudw2k,

This is EXACTLY what I was looking for. Thank you very much for posting the link to the thread with the following solution provided by KentonBomb:

$method = "Name"
$host = "localhost"
$strWMIQuery = "SELECT * FROM Win32_ComputerSystem"

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $host & "\root\cimv2")
$colItems = $objWMIService.ExecQuery ($strWMIQuery)

For $objItem in $colItems
msgbox(0,"Hostname",Execute("$objItem." & $method))
Next

Many thanks to KentonBomb and also to DaleHohm for pointing out the solution.

I love AutoIt. I have always been able to find what I needed with the help of the good people of this forum. And I have yet to find a scripting problem which AutoIt could not handle. This is why I donate to Jon to help him pay his hosting bills.

Thank you.

taurus905


"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
Share on other sites

Huh, Execute. Similar to Eval but slightly different... Glad you solved it.

Share this post


Link to post
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
Sign in to follow this  
Followers 0