Deye Posted April 12, 2020 Share Posted April 12, 2020 (edited) expandcollapse popup; 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 April 16, 2020 by Deye Link to comment Share on other sites More sharing options...
Nine Posted April 12, 2020 Share Posted April 12, 2020 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 Deye 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Deye Posted April 12, 2020 Author Share Posted April 12, 2020 (edited) Nine why?, you are now in a powershell forum We need more tables like these Edit: added parameters examples: Edited April 16, 2020 by Deye Link to comment Share on other sites More sharing options...
Nine Posted April 12, 2020 Share Posted April 12, 2020 (edited) It was just for fun Deye, because of the other thread. But alright, I'll go away. Sorry for that... Edited April 12, 2020 by Nine Deye 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted April 13, 2020 Moderators Share Posted April 13, 2020 Nice start @Deye. Make it work with import-csv, and add dot notation, and you're gold "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 comment Share on other sites More sharing options...
Deye Posted April 13, 2020 Author Share Posted April 13, 2020 (edited) @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 April 13, 2020 by Deye Link to comment Share on other sites More sharing options...
Deye Posted April 14, 2020 Author Share Posted April 14, 2020 (edited) Edit: the OP Edited April 16, 2020 by Deye Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted April 14, 2020 Moderators Share Posted April 14, 2020 (edited) @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 April 14, 2020 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 comment Share on other sites More sharing options...
Deye Posted April 16, 2020 Author Share Posted April 16, 2020 Cleaned and polished I think its now pretty much "It" the rest are just tweaks for personal needs .. @JLogan3o13 I'm not so oriented with powershell script writing, just added one more example to do col imports of by header names Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now