Solved

Powershell Array to Datatable

Posted on 2013-01-22
1
3,625 Views
Last Modified: 2013-01-28
In Powershell what is the best way to import all array values into a Datatable?  I've created the Datatable with a specific format so I can pass it into a stored procedure.  I'm pulling in some server information from multiple servers and this is getting added to an array object.  The number of columns in both match.  

I thought something along the lines of this might work but it's not.  

$DBStats | ForEach-Object {$dt.ImportRow($_)}

Open in new window


$DBStats is my object array, $dt is my Datatable.

EDIT: I've been able to add to the Datatable now from the array using the below function.  Is there a better way of doing this?  

Function LoadDataTable
{
	PARAM($dt,$DBStats)
	$DBStats | ForEach-Object {
		$row = $dt.NewRow()
		$row.Item('Parent') = $_.Parent.ToString()
		$row.Item('Name') = $_.Name.ToString()
		$row.Item('Size') = $_.Size.ToString()
		$dt.Rows.Add($row)
		}
}

Open in new window

0
Comment
Question by:tim_cs
1 Comment
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 38806576
An improvement is not to use foreach-object but foreach, as this performs better on already existing data (versus data created on the fly):
Function LoadDataTable(dt,$DBStats)
{
  foreach ($data in $DBStats)
  {
     $row = $dt.NewRow()
     $row.Item('Parent') = $data.Parent
     $row.Item('Name') = $data.Name
     $row.Item('Size') = $data.Size
     $dt.Rows.Add($row)
  }
}

Open in new window

This could be made more dynamic:
Function LoadDataTable(dt,$DBStats)
{
  $cols = $DBStats | select -first 1 | get-member -MemberType NoteProperty | select -Expand Name
  foreach ($data in $DBStats)
  {
     $row = $dt.NewRow()
     foreach ($col in $cols)  { $row.Item($col) = $data.$col }
     $dt.Rows.Add($row)
  }
}

Open in new window

0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This article will help you understand what HashTables are and how to use them in PowerShell.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question