how to loop through datatable in powershell?
<p>I am getting different errors while fetching data from datatable. Here is my code to fill datatable:</p>

<pre><code>$dt = New-Object System.Data.DataTable
$dt.Columns.Add("ServiceName")
$dt.Columns.Add("HostName")

$SQLConnection.Open()
$Reader = $SqlCmd.ExecuteReader()
while ($Reader.Read()) {
    $dt.Rows.Add($Reader.GetValue(0), $Reader.GetValue(1))
}
$SqlConnection.Close()
return $dt
</code></pre>

<p>Above code is in a function which returns datatable and when I access this returned datatable in calling script I don't get the data.</p>

<p>Script to fetch data on each row:</p>

<pre><code>foreach ($Row in $BackupData) {
    Write-Host "$Row[0]"
    Write-Host "$Row[1]"
}
</code></pre>

<p>I am getting output like:</p>

<pre>ServiceName[0]
HostName[0]
System.Data.DataRow[0]
System.Data.DataRow[0]
System.Data.DataRow[0]
System.Data.DataRow[0]</pre>

<p>And when I use <code>"$($Row[0])"</code> I get another error</p>

<blockquote>
  <p>unable to index an object of type system.Data.DataColumn</p>
</blockquote>

<p>How can I get the data of each row and column?</p>