Solved

Powershell Array to Datatable

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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…

739 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