powershell extract a value from a table

16,548

Solution 1

Select the appropriate row from the Rows property and then grab the value by column name:

$row1column1value = $dataset.Tables[0].Rows[0].Column1

Solution 2

A DataSet in ADO.NET contains multiple tables. What you have is just giving you the first (only) table in your dataset. You need to specify which row and column to return to get a single value. You would have to do something like this if you are looking for a single value:

$value = $DataSet.Tables[0].Rows[0]["Column1"]

Solution 3

$DataSet.Tables[0].Rows[0].ItemArray[0]

Solution 4

It´s working too, my Column call Teste

$res = $Ds.Tables[0].rows[0]["teste"] 

and now it's possible do IF in variable $res

Share:
16,548
John
Author by

John

Updated on July 19, 2022

Comments

  • John
    John almost 2 years

    I'm still pretty new into Powershell and this has taken way too much time, so HELP! (please)

    I have a script that starts out by extracting a value from a SQL Server table. The extract works by populating a "table" via:

    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)
    

    Where I run into a problem is getting the single value out of $DataSet. The value is a date time stamp that I need. Referencing $DataSet.Tables[0] shows me the date time stamp with a heading of Column1. I've tried all kinds of casting, ToString(), etc. The results are either a display of data types (e.g. System.Data.DataRow) or any number of errors. How do I get the value into a variable that is recognized as a date time?

    TIA,

    John