Solved

Powershell Array to Datatable

Posted on 2013-01-22
1
3,428 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 68

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now