Solved

Powershell Array to Datatable

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Synchronize a new Active Directory domain with an existing Office 365 tenant
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

867 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

20 Experts available now in Live!

Get 1:1 Help Now