Skip to content
Advertisement

Return the Data from Column Using PowerShell

When I export the $dt to a CSV – it shows in 1.A of Excel = 1 and in 2.A = 2. I need to extract the two value of the second row in column A. First why is the count working? And why do I not get a return for the values in the last two echo commands when I know there is a value = 2 in Row#2 – Column A?

  $factory = [System.Data.Common.DbProviderFactories]::GetFactory('IBM.Data.DB2')
  $cstrbld = $factory.CreateConnectionStringBuilder()
  $cstrbld.Database = 'mydb'
  $cstrbld.UserID = 'user'
  $cstrbld.Password = 'userpass'
  $cstrbld.Server = 'server:port#'
  $dbconn = $factory.CreateConnection()
  $dbconn.ConnectionString = $cstrbld.ConnectionString
  $dbconn.Open()
  $dbcmd = $dbconn.CreateCommand()
  $dbcmd.CommandText = 'SELECT COUNT(*) FROM dbname.UNIT WHERE STATUS = 3'
  $rdr = $dbcmd.ExecuteReader()
  $dt = New-Object System.Data.DataTable
  $dt.Load($rdr)
  $dbconn.Close()
  echo $dt.rows.count
  ## Returns 1
  echo $dt.rows[1]
  ## Returns nothing
  echo $dt.rows[1].column0
  ## Returns nothing







code ```

Advertisement

Answer

because index start to 0, try this:

$dt.rows[0]
$dt.Rows[0].ItemArray[0].ToString()
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement