?
Solved

Powershell Array to Datatable

Posted on 2013-01-22
1
Medium Priority
?
4,181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 70

Accepted Solution

by:
Qlemo earned 2000 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

741 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