Jump to content

How to read PowerShell command into a multiple dimensional dimensional array? - (Moved)


Recommended Posts

$sCommands1 = 'powershell.exe Get-ChildItem'
$iPid = run($sCommands1   , @WorkingDir , @SW_SHOW , 0x2)

$sOutput = ""



 While 1
    $sOutput &= StdoutRead($iPID)
        If @error Then
            ExitLoop
        EndIf
 WEnd

;~ msgbox(0, '' , $sOutput)
ConsoleWrite("$sOutput")
ConsoleWrite($sOutput)
ConsoleWrite(@CRLF)
$aOutput = stringsplit($sOutput ,@LF , 2)


For $i=0 To  UBound($aOutput) - 1 Step 1
    ConsoleWrite($aOutput[$i])

Next

The script above reads the whole directory into a one dimensional array, but I need to work with the array, so I need to split the array into multiple dimensions.

I have already read some forum answers here, and I have already tried these commands:

Quote

 

ConsoleWrite(_ArrayToString($aOutput))

_ArrayAdd($aOut , _ArrayToString($aOutput) , 0 , " " @CRLF"|" , 1)

_ArraySpilt

 

Are there any way to use the $aOutput variable like in PowerShell:

PowerShell:

$a = Get-ChildItem

$a.Mode

I imagine this in AutoIt  $aOutput

ConsoleWrite($aOutput[i].Mode)

Or if I split this command into 2 dimension like:

For $i To UBound($aOutput)-1 Step 1

ConsoleWrite($aOutput[$i][1])

ConsoleWrite($aOutput[$i][2])

Next

 

Edited by DannyJ
Link to post
Share on other sites

in my much acclaimed* post titled: 

there is an example of chopping into 2 dimensional array. Maybe that can be of help ?.

Developer General Discussion, is for AutoIt development as a product, not general help scripting.

*much acclaimed may be an exaggeration from the over imaginative author 

Link to post
Share on other sites
#include <Array.au3>
_ArrayDisplay(_HowToChopSuey())
Func _HowToChopSuey()
    Local $sCommands1 = 'powershell.exe Get-ChildItem'
    Local $ArrayB, $sOutput = "", $iPid = run($sCommands1   , @WorkingDir , @SW_SHOW , 0x2)

    While 1
        $sOutput &= StdoutRead($iPID)
        If @error Then ExitLoop
    WEnd

    $aOutput = stringsplit(StringReplace($sOutput, @CR, "") ,@LF , 2)
    Local $aReturn[UBound($aOutput)][6]

    For $i=0 To UBound($aOutput) - 1 ; Step 1
        ConsoleWrite($i & @TAB & $aOutput[$i] & @CRLF)
        $aReturn[$i][0] = $i ; or not, no clue
        $aReturn[$i][1] = StringMid($aOutput[$i], 1, 7)
        $aReturn[$i][2] = StringMid($aOutput[$i], 14, 11)
        $aReturn[$i][3] = StringMid($aOutput[$i], 26, 11)
        $aReturn[$i][4] = StringMid($aOutput[$i], 36, 14)
        $aReturn[$i][5] = StringMid($aOutput[$i], 51)
    Next
Return $aReturn
EndFunc

 

Link to post
Share on other sites
  • Developers

Moved to the appropriate forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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.

  • Similar Content

    • By mLipok
      Usually when I collect data from DataBase I need to give EndUser a possibility to select rows which should be taken in the processing loop.
      I was searching on the forum and I'm not able to find any UDF or even example of how to select data from array.
      I have my own solutions but I think they are not worth posting on the forum as it is very old code and I am looking for a better solution.

      Could anybody point me to some examples/solutions ?

      Thank you in advance.
      @mLipok
    • By EmilyLove
      I have a string containing the full path of an executable and an array of executables without their paths. I am trying to compare the string to the list in the array and if a match is found, remove it from the array. The entry get removed from the array successfully, and after checking its return result, uses it to update the ubound if it succeeded, but it doesn't want to update to the new value. Any ideas what I am doing wrong? It acts like it is read-only.
      #include <Array.au3> #include <File.au3> Local $sApp_Exe = "F:\App\Nextcloud\nextcloud.exe" Local $aWaitForEXEX = [3, "Nextcloud.exe", "nextcloudcmd.exe", "QtWebEngineProcess.exe"] For $h = 1 To $aWaitForEXEX[0] If StringInStr($sApp_Exe, $aWaitForEXEX[$h]) <> 0 Then $iRet = _ArrayDelete($aWaitForEXEX, $h) If $iRet <> -1 Then $aWaitForEXEX[0] = $iRet ;this line doesn't work. $aWaitForEXEX[0] doesn't update and shortly gives Error: Array variable has incorrect number of subscripts or subscript dimension range exceeded.: _ArrayDisplay($aWaitForEXEX) EndIf Next  
    • By WhaleJesus
      #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <file.au3> ; Create Data Folder if it doesn't exist yet If FileExists(@ScriptDir & "\Data") Then Else ShellExecute(@ScriptDir) DirCreate(@ScriptDir & "\Data") EndIf ; Playlist Name & location input Global $playlistnameinput = InputBox("Playlist", "Enter The playlist name", _ "Name") Global $playlistlocationinput = InputBox("Location", "Specify where you would like the playlist folder to be stored", @ScriptDir & "\Playlists\" & $playlistnameinput) ; Create file in Data folder and other vars Global $sDataFile = @ScriptDir & "\Data\Data.txt" Global $DataHandle = FileOpen($sDataFile, 1) Global $DataFileLine = FileReadLine($sDataFile, 1) FileClose($DataFileLine) MsgBox(0, "", $DataFileLine, 10) ; Prove it exists If FileExists($sDataFile) Then _FileWriteToLine($DataHandle, $DataFileLine, $playlistnameinput, True, True) $DataFileLine += 1 _FileWriteToLine($DataHandle, 1, $DataFileLine, True) Else MsgBox($MB_SYSTEMMODAL, "Error", "File " & $sDataFile & "Does not exist") EndIf Global $sPDataFile = @ScriptDir & "\Data\" & $playlistnameinput & "_Data.txt" Global $PDataHandle = FileOpen($sPDataFile, 1) If FileExists($sPDataFile) Then _FileWriteToLine($PDataHandle, 1, $playlistnameinput, True, True) _FileWriteToLine($PDataHandle, 2, $playlistlocationinput, True, True) Else MsgBox($MB_SYSTEMMODAL, "Error", "File " & $sPDataFile & "Does not exist") EndIf _FileWriteToLine stopped working and i don't know what it is in my code that's causing this, please help
    • By vinnyMS
      #Include <Array.au3> #include <Constants.au3> $s = FileRead("2.txt") Local $w = StringRegExp($s, '(?is)(\b\w+\b)(?!.*\b\1\b)', 3) _ArrayColInsert($w, 1) For $i = 0 to UBound($w)-1 StringRegExpReplace($s, '(?i)\b' & $w[$i][0] & '\b', $w[$i][0]) $w[$i][1] = @extended Next _ArraySort($w, 1, 0, 0, 1) _ArrayDisplay($w) i have this script that returns 3 columns  
       
      i need to copy the  Col 0 and Col 1 as text to paste on notepad or excel
      you will have to create a "copy" button if possible
      array.au3 2.txt
    • By DannyJ
      I have a dataset like this, (a strubg)
      Username: User1 Type: Admin RegDate: 1999 Username: User2 Type: User RegDate: 2000 How to make a 2 dimensional array that I can display with _ArrayDisplay?
      This would be a perfect 2D array to represent my data:
      Username           Tpye RegDate User1              Admin 1999 User2              User 2000   If you run this Powershell this powershell command, you can get this dataset that I am talking about:
      Get-LocalUser | Select * With this code you can try it to read into a string:
      #include <GuiConstantsEx.au3> #include <WindowsConstants.au3> #include "GUIListViewEx.au3" #include <Array.au3> ; Just for display in example #RequireAdmin #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_UseX64=y #EndRegion $sCommand = "powershell.exe Get-LocalUser | Select *" Local $iPid = Run($sCommand, @WorkingDir , @SW_SHOW , $STDOUT_CHILD) ProcessWaitClose($iPid) Local $sOutput = StdoutRead($iPID) ConsoleWrite($sOutput) How can I correctly split $sOutput into a 2D array (with the above mentioned layout) that I can display and I work with?
×
×
  • Create New...