Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract data from System.Data.DataRow in powershell

Tags:

sql

powershell

I have a powershell script that executes an sql command and returns a list of ID numbers.

When I iterate through the list, this is what it returns.

System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow

I tried adding Out-String to my list,

$q_result = $db.ExecuteWithResults($int_cmd2)
$table = $q_result.Tables[0] | Out-String

foreach ($user_info in $table)
{
    write-host $user_info 
}

but that returns a poorly formatted list of numbers, everything is tabbed to the very right. see below.

                                                                                                      GroupID
                                                                                                      -------------
                                                                                                                381
                                                                                                                382
                                                                                                                383
                                                                                                                384
                                                                                                                385
                                                                                                                386

I tried using, $user_info.Item[0] in the loop, but that returns nothing.

How can I extract just the numbers from the list?.

like image 776
cyberbemon Avatar asked Aug 30 '16 10:08

cyberbemon


1 Answers

You can also use ItemArray. Prints the entire row, while Item(index) prints the values from the indexed cell.

enter image description here

like image 69
nyagolova Avatar answered Oct 15 '22 05:10

nyagolova