tim_cs
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 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?
I thought something along the lines of this might work but it's not.
$DBStats | ForEach-Object {$dt.ImportRow($_)}
$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)
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.