Jump to content

[Solved] How to make a two dimensional array from this data? - (Moved)


Recommended Posts

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?

Edited by DannyJ
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Here SRE way :

#include <Array.au3>

Local $sOutput = "Username: User1" & @CRLF & _
@CRLF & _
"Type: Admin" & @CRLF & _
@CRLF & _
"RegDate: 1999" & @CRLF & _
@CRLF & _
@CRLF & _
"Username: User2" & @CRLF & _
@CRLF & _
"Type: User" & @CRLF & _
@CRLF & _
"RegDate: 2000"

; extract informations
Local $aArray = StringRegExp($sOutput, "(?|Username|Type|RegDate)\h*:\h*(.*)", 3)
_ArrayDisplay($aArray)

; convert 1d array into 2d array
Local $aFinal[UBound($aArray)/3][3]
For $i = 0 to UBound($aFinal) - 1
  $aFinal[$i][0] = $aArray[$i*3]
  $aFinal[$i][1] = $aArray[$i*3+1]
  $aFinal[$i][2] = $aArray[$i*3+2]
Next
_ArrayDisplay($aFinal)

 

Link to comment
Share on other sites

9 hours ago, DannyJ said:

  If you run this Powershell this powershell command, you can get this dataset that I am talking about:

Get-LocalUser | Select *

Thats odd, if I run that in powershell i get totally different column names and data

AccountExpires         :
Description            : Built-in account for administering the computer/domain
Enabled                : False
FullName               :
PasswordChangeableDate :
PasswordExpires        :
UserMayChangePassword  : True
PasswordRequired       : True
PasswordLastSet        :
LastLogon              : 1/16/2021 12:30:28 AM
Name                   : Administrator
SID                    : S-1-5-21-4204884644-104046959-XXXXXXXXXX-500
PrincipalSource        : Local
ObjectClass            : User

 

Code hard, but don’t hard code...

Link to comment
Share on other sites

9 hours ago, JockoDundee said:

Thats odd, if I run that in powershell i get totally different column names and data

AccountExpires         :
Description            : Built-in account for administering the computer/domain
Enabled                : False
FullName               :
PasswordChangeableDate :
PasswordExpires        :
UserMayChangePassword  : True
PasswordRequired       : True
PasswordLastSet        :
LastLogon              : 1/16/2021 12:30:28 AM
Name                   : Administrator
SID                    : S-1-5-21-4204884644-104046959-XXXXXXXXXX-500
PrincipalSource        : Local
ObjectClass            : User

 

Yes I wrote above data just for example, the correct data is the command

Link to comment
Share on other sites

Link to comment
Share on other sites

2 hours ago, Nine said:

Still the code provided above will work.  You just need to change the list of values you want to gather and adapt the size of the 2D array...

@Nine Thank you very much, your code above will work!

 

Link to comment
Share on other sites

16 hours ago, DannyJ said:

Yes I wrote above data just for example, the correct data is the command

fwiw, you can modify the command 

Get-LocalUser | Select *

to

Get-LocalUser | Select * | Format-Table -HideTableHeaders | Out-File users.txt

This will put the output in table format, without headers and into a file.

Then you should be able to load the file using _FileReadFromArray() directly into a 2D array. - Untested.

Though you may have to mess with delimiters etc, depending on whether white space is embedded in the data.

Code hard, but don’t hard code...

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...