Jump to content

Recommended Posts

Posted (edited)

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
  • Moderators
Posted

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.

Expand  

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:

  Reveal hidden contents

 

Posted

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)

 

Posted
  On 4/26/2021 at 1:06 PM, DannyJ said:

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

Get-LocalUser | Select *
Expand  

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

Posted
  On 4/26/2021 at 10:21 PM, 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

 

Expand  

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

Posted
  On 4/27/2021 at 11:50 AM, 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...

Expand  

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

 

Posted
  On 4/27/2021 at 7:57 AM, DannyJ said:

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

Expand  

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

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
×
×
  • Create New...