Jump to content

Merge 2 x 1d temp arrays into one 2d array


Recommended Posts

Hello all,

Summary: I have a basic piece of code that is to be a part of a much larger project; I just can't seem to get the right output. I'm retrieving two lots of powershell data into 2 x 1d arrays and trying to add them into a single 2d array. Retrieving the data together into the 2d array seemed harder, due to the application names varying too much to string split. Data being pulled is application name and GUID. From here I will use this info in a drop down box and an uninstall button to run the required command to remove the selected software (have this sorted already).

Problem: When I merge the data it doesn't put the application name and GUID on the same row in differing columns eg. my test box has 24 applications plus some superfluous data from Powershell to be cleaned up by the _arraydeletes. Instead I end up with an array with 58 rows and 2 columns; whereas my temp 1d arrays have 28 rows. As you can see I've tried both _ArrayInsert and _ArrayAdd but I still get the same result.

Question: Is there something that I'm doing wrong in putting the data into the 2d array or do I just need to do some more post processing to tidy it up and align the names and GUIDs?

Code:

#include <Array.au3>

$Cmd1 = (" /c Powershell.exe " & Chr(34) & "Get-WmiObject -Class win32reg_addremoveprograms | where {$_.ProdID -like " & Chr(34) & Chr(123) & Chr(42) & Chr(125) & Chr(34) & "} | select DisplayName" & Chr(34))
$Cmd2 = (" /c Powershell.exe " & Chr(34) & "Get-WmiObject -Class win32reg_addremoveprograms | where {$_.ProdID -like " & Chr(34) & Chr(123) & Chr(42) & Chr(125) & Chr(34) & "} | select ProdID" & Chr(34))

Global $aNameGUID[1][2]
;_ArrayDisplay($aNameGUID)
ReadApps($Cmd1,0)
;_ArrayDisplay($aNameGUID)
ReadApps($Cmd2,1)
_ArrayDisplay($aNameGUID)
Terminate()

Func ReadApps($Command,$col)
    $DOS = Run(@ComSpec & $Command, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    ProcessWaitClose($DOS)
    $DOSOut = StdoutRead($DOS)
    ;MsgBox(0,"Data",$DOSOut) ;Show the line items that we want in the array
    Local $tmpArray = StringSplit(StringTrimRight(StringStripCR($DOSOut), StringLen(@CRLF)), @CRLF)
    If @error Then
        MsgBox(0,"FAIL","I failed to find objects")
        Exit
    Else
        _ArrayDisplay($tmpArray)
    EndIf
        ;_ArrayDelete($tmpArray, 3)
        ;_ArrayDelete($tmpArray, 2)
        ;_ArrayDelete($tmpArray, 1)
        ;$tmpArray[0] = $tmpArray[0] - 3
        For $i = 0 To UBound($tmpArray) - 1
            ;_ArrayAdd($aNameGUID, $tmpArray[$i], $col)
            _ArrayInsert($aNameGUID, 0, $tmpArray[$i], $col)
        Next
    $tmpArray = 0
EndFunc ;==>ReadApps

While 1
    Sleep(1500)
WEnd

Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

Thanks in advance,

Luxyboy

Edited by StillLearningThisStuff
update
Link to comment
Share on other sites

Could you not retrieve both the DisplayName and ProdID in a single query?

Get-WmiObject -Class win32reg_addremoveprograms | where {$_.ProdID -like "{*}"} | select DisplayName, ProdID | Format-Table

You could also bypass using PowerShell and just query WMI directly from AutoIt (via COM object) and then format the data any way you want/need.

edit: WMI example

#include <Array.au3>  ;only needed for demo

$objWMIService = WMIService(@ComputerName)  ;WMIService Object - Establish Connection
If $objWMIService = 0 Then Exit

Local $strData[2]= ["DisplayName","ProdID"]  ;Data to Return from WMI Query

$arrResults = WMIQuery($objWMIService,"SELECT * FROM Win32Reg_AddRemovePrograms",$strData)  ;Run WMI Query against WMIService Object
$objWMIService = 0  ;Termninate WMIService Object

_ArrayDisplay($arrResults)

Func WMIService($host) ;Connects to WMI Service
    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $host & "\root\cimv2")
    If not IsObj($objWMIService) Then return 0
    return $objWMIService
EndFunc

Func WMIQuery($objWMIService,$strWMIQuery,$arrData) ;Perform WMI Query with Query String and Data Return Parameters
    If not IsArray($arrData) then return 0
    $colItems = $objWMIService.ExecQuery ($strWMIQuery)  ;Execute query against WMI Service Object
    $iCount = $colItems.count
    If $iCOunt <= 0 Then return 0
    $iBound = UBound($arrData)
    Local $arrResults[$colItems.count+1][$iBound] ;Two-Dimension Array to store query Results

    For $i = 0 to $iBound-1
        $arrResults[0][$i] = $arrData[$i]
    Next

    Local $iIdx = 1
    For $objItem in $colItems
        For $i = 0 to $iBound - 1  ;Loop through WMI Query Results
            ;$arrResults[$iIdx][$i] = $arrData[$i]  ;Property/Data Name
            $arrResults[$iIdx][$i] = Execute("$objitem." & $arrData[$i])  ;Result
        Next
        $iIdx += 1
    Next

    return $arrResults  ;Return Result Array
EndFunc

 

Edited by spudw2k
Link to comment
Share on other sites

This should do it:

Func ReadApps($Command,$col)
    $DOS = Run(@ComSpec & $Command, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    ProcessWaitClose($DOS)
    $DOSOut = StdoutRead($DOS)
    ;MsgBox(0,"Data",$DOSOut) ;Show the line items that we want in the array
    Local $tmpArray = StringSplit(StringTrimRight(StringStripCR($DOSOut), StringLen(@CRLF)), @CRLF)
    If @error Then
        MsgBox(0,"FAIL","I failed to find objects")
        Exit
    Else
        _ArrayDisplay($tmpArray)
    EndIf
        ;_ArrayDelete($tmpArray, 3)
        ;_ArrayDelete($tmpArray, 2)
        ;_ArrayDelete($tmpArray, 1)
        ;$tmpArray[0] = $tmpArray[0] - 3
        If UBound($aNameGUID) < UBound($tmpArray) Then ReDim $aNameGUID[UBound($tmpArray)][2]
        For $i = 0 To UBound($tmpArray) - 1
            ;_ArrayAdd($aNameGUID, $tmpArray[$i], $col)
            ;_ArrayInsert($aNameGUID, 0, $tmpArray[$i], $col)
            $aNameGUID[$i][$col] = $tmpArray[$i]
        Next
    $tmpArray = 0
EndFunc ;==>ReadApps

 

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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

×
×
  • Create New...