Link to home
Start Free TrialLog in
Avatar of tim_cs
tim_csFlag for United States of America

asked on

Powershell Array to Datatable

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

ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial