Jump to content

Tables to Arrays


Recommended Posts


 

; https://www.autoitscript.com/forum/topic/202362-tables-to-arrays/?tab=comments#comment-1452276

#include <Array.au3>

$sTables = "Counter,Date,Disk,process,Service,HotFix,printer,AppxPackage,NetAdapter" & _
        ",NetIPAddress,Acl"

$aTables = StringSplit($sTables, ",")
For $i = 1 To $aTables[0]
    _ArrayDisplay(GetTable($aTables[$i]), $aTables[$i] & " - info")
Next

$aArray = GetTable("process -id " & WinGetProcess("", "") & " | fl * -Force")
_ArrayDisplay($aArray, "Get Current process Detail")

$aArray = GetTable("CimInstance Win32_Process | where commandline -match 'applog'")
_ArrayDisplay($aArray)

$aArray = GetTable("(Process)[3]")
_ArrayDisplay($aArray, "Get Process 3")

$aArray = GetTable("process -name '" & GetTable("(Process)[10]")[2][4] & "'")
_ArrayDisplay($aArray, "Get Process name from Process 10")

$aArray = GetTable("Service| where status -eq 'Running'")
_ArrayDisplay($aArray, "Get up and Running Services")

$aArray = GetTable("(Process -id " & @AutoItPID & ").StartInfo | select -ExpandProperty environmentvariables")
_ArrayDisplay($aArray, ".StartInfo ExpandProperty environmentvariables From Process -id")

$aArray = GetTable("Content .\Example.csv | select -First 5", ",")
_ArrayDisplay($aArray, "List First 5 lines")

$aArray = GetTable("Content .\Example.csv -Tail 5", ",")
_ArrayDisplay($aArray, "List -Tail 5 Lines")

$aArray = GetTable("import-csv '.\Example.csv'|Select-Object 'System Resolver', 'Server IP', 'Owner'")
_ArrayDisplay($aArray, "Display Columns By Header Names")

$aArray = GetTable("NetAdapter -physical | where status -eq 'up'")
_ArrayDisplay($aArray, "Up and Running physical net NetAdapter Detail")

$aArray = GetTable("NetIPAddress -AddressFamily IPv4 -AddressState Preferred")
_ArrayDisplay($aArray, "IPv4 NetIP Detail")

Func GetTable($sTable, $sDelim = False, $bFormTabList = True)
    Local $sSfstring = $bFormTabList ? '|Format-List|Out-String -Width 4096"' : ""
    $sCommands = 'powershell -Command "Get-' & $sTable & $sSfstring
    $sCommands = StringReplace($sCommands, "Get-(", "(Get-")
    If StringInStr($sCommands, "import") Then
        $sCommands = StringReplace($sCommands, "Get-", "")
    EndIf

    $iPID = Run(@ComSpec & " /c " & $sCommands, "", @SW_HIDE, $stdout_child)
    $sOutput = ""
    While 1
        $sOutput &= StdoutRead($iPID)
        If @error Then
            ExitLoop
        EndIf
    WEnd

    If $sDelim Then
        Local $ArrMain[0][_ColCount($sOutput, $sDelim)]
        _ArrayAdd($ArrMain, $sOutput, Default, ",")
        Return $ArrMain
    EndIf

    Local $Array[0][3]
    If StringInStr($sCommands, "import") Then
        $s = StringRegExpReplace($sOutput, "\. *[ ]*[ ]", ".")
        $s = StringRegExpReplace($sOutput, "\:[ ]*[ ]", "|:")
    Else
        $s = StringRegExpReplace($sOutput, "\s[: ]*[: ]", "|:")
    EndIf
    _ArrayAdd($Array, StringRegExpReplace($s, "\R$", "|:"), "|:")

    $iIndex = _ArraySearch($Array, "", 2)
    $iIndex = (@error < 0 ? UBound($Array) : $iIndex - 1)

    $s = StringReplace(_ArrayToString(_ArrayExtract($Array, 0, Default, 1, 1), "|"), "||:", @CRLF)
    $s = _ArrayToString(_ArrayExtract($Array, 2, $iIndex, 0, 0), "|") & @CRLF & $s

    $s = StringRegExpReplace($s, "[|]*[|]\R", "")
    $s = StringReplace($s, "|:", "|")

    Local $ArrMain[0][_ColCount($s)]
    _ArrayAdd($ArrMain, $s)
    Return $ArrMain

EndFunc   ;==>GetTable

Func _ColCount($sTmp, $sDelim = "|", $iTimes = 5)
    Local $1 = 0, $2 = 0, $x = 0
    Do
        Local $a = StringRegExp($sTmp, "^(.*)", 3)
        $sTmp = StringRegExpReplace($sTmp, "^(.*)\R", "")
        StringReplace($a[0], $sDelim, $sDelim)
        $2 = @extended
        If $2 > $1 Then
            $1 = $2
        EndIf
        $x += 1
    Until $x >= $iTimes
    Return $1 + 1
EndFunc   ;==>_ColCount

 

Edited by Deye
Link to post
Share on other sites

Or if someone prefers using WMI ;)

#AutoIt3Wrapper_UseX64=y
#include <Constants.au3>
#include <Array.au3>

Opt("MustDeclareVars", 1)

Local $aClass = ["Win32_OperatingSystem", "Win32_PhysicalMemory", "Win32_Processor", "Win32_OperatingSystem", "Win32_Process", "Win32_SystemUsers"]
Local $aWMI
For $sClass In $aClass
  $aWMI = _FillArray($sClass)
  _ArrayDisplay($aWMI)
Next

Func _FillArray($Class)
  Local $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
  Local $colItems = $objWMIService.ExecQuery('SELECT * FROM ' & $Class)
  If Not IsObj($colItems) Then Return SetError (1) ; "Not an object"
  If Not $colItems.count Then Return SetError (2) ; "Not found"
  Local $aWMI[$colItems.count + 1][$colItems.ItemIndex(0).Properties_.count], $iCount = 0, $nCount = 1
  For $oProperty In $colItems.ItemIndex(0).Properties_
    $aWMI[0][$iCount] = $oProperty.name
    $iCount += 1
  Next
  For $oItem In $colItems
    $iCount = 0
    For $oProperty In $oItem.Properties_
      $aWMI[$nCount][$iCount] = $oProperty.Value
      $iCount += 1
    Next
    $nCount += 1
  Next
  Return $aWMI
EndFunc   ;==>_FillArray

 

Link to post
Share on other sites

@JLogan3o13

Added now to the first commented Example :)

if all headers exist  then  $iTimes can be set to  1 for the ColCount Func

otherwise it needs to fish out the raw that will contains the max count of Col-delimiters (default is $iTimes= 5)

as to "dot notation" I will need an example ..

Edited by Deye
Link to post
Share on other sites
  • Moderators

@Deye that part was a poor attempt at humor only :) 

As you likely well know, when dealing with arrays in PowerShell they can be iterated over in a pseudo-associative manner. For example, if I import a csv with the columns "Name|IPAddress|OS|Manufacturer", I can then run through them with something like:

foreach ($server in $aServers) {
	Write-Host $server.IPAddress
}

Whereas AutoIt would have to use the column number.

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...