Hi,
I am trying to write a powershell script to read some values out of a database and then put them into an two dimension array.
My SQL query will return:
User, Group
User a, Group 1
User a, Group 2
User b, Group 2
User c, Group 3
User d, Group 5
etc
I would like to get the data read into an array so that I can read out the values later in the script. For example:
$table[0][0] should return User a
$table[0][1] should return Group 1
I first setup the connection with this code.
$Connection = New-Object System.Data.SQLClient.SQLConnection $Connection.ConnectionString = "server=xxx;database=yyy;trusted_connection=true;" $Connection.Open() $Command = New-Object System.Data.SQLClient.SQLCommand $Command.Connection = $Connection # Put the SQL query below $Command.CommandText = "select user, group from xxx" $Reader = $Command.ExecuteReader()
Then I loop through the $Reader object and try and read all the values into an array
$table = @()
$intCounter = 0 while($Reader.Read()) { #write-host $Reader $user = $Reader["User_Name"].ToString() $group = $Reader["Group_Name"].ToString() Write-Host "$user $group $intCounter" $table += @($user,$group) $intCounter++ } $Connection.Close()
The line Write-Host "$User $Group $intCounter" displays the users and the groups, so I know that $user and $group have values in them.
I think that the problem is that I have just defined a 1 dimension array $table = @(), as $table[0] returns User a and $table[1] returns Group 1.
So 2 questions:
1. How do I define a multidimension array $table = @()() ?
2. If I am on the wrong track, how should I attack this?