Created
January 10, 2015 23:55
-
-
Save lazywinadmin/1e094b3f0f229e9b011d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Credit: SAPIEN | |
function ConvertTo-DataTable | |
{ | |
<# | |
.SYNOPSIS | |
Converts objects into a DataTable. | |
.DESCRIPTION | |
Converts objects into a DataTable, which are used for DataBinding. | |
.PARAMETER InputObject | |
The input to convert into a DataTable. | |
.PARAMETER Table | |
The DataTable you wish to load the input into. | |
.PARAMETER RetainColumns | |
This switch tells the function to keep the DataTable's existing columns. | |
.PARAMETER FilterWMIProperties | |
This switch removes WMI properties that start with an underline. | |
.EXAMPLE | |
$DataTable = ConvertTo-DataTable -InputObject (Get-Process) | |
#> | |
[OutputType([System.Data.DataTable])] | |
param( | |
[ValidateNotNull()] | |
$InputObject, | |
[ValidateNotNull()] | |
[System.Data.DataTable]$Table, | |
[switch]$RetainColumns, | |
[switch]$FilterWMIProperties) | |
if($Table -eq $null) | |
{ | |
$Table = New-Object System.Data.DataTable | |
} | |
if($InputObject-is [System.Data.DataTable]) | |
{ | |
$Table = $InputObject | |
} | |
else | |
{ | |
if(-not $RetainColumns -or $Table.Columns.Count -eq 0) | |
{ | |
#Clear out the Table Contents | |
$Table.Clear() | |
if($InputObject -eq $null){ return } #Empty Data | |
$object = $null | |
#find the first non null value | |
foreach($item in $InputObject) | |
{ | |
if($item -ne $null) | |
{ | |
$object = $item | |
break | |
} | |
} | |
if($object -eq $null) { return } #All null then empty | |
#Get all the properties in order to create the columns | |
foreach ($prop in $object.PSObject.Get_Properties()) | |
{ | |
if(-not $FilterWMIProperties -or -not $prop.Name.StartsWith('__'))#filter out WMI properties | |
{ | |
#Get the type from the Definition string | |
$type = $null | |
if($prop.Value -ne $null) | |
{ | |
try{ $type = $prop.Value.GetType() } catch {} | |
} | |
if($type -ne $null) # -and [System.Type]::GetTypeCode($type) -ne 'Object') | |
{ | |
[void]$table.Columns.Add($prop.Name, $type) | |
} | |
else #Type info not found | |
{ | |
[void]$table.Columns.Add($prop.Name) | |
} | |
} | |
} | |
if($object -is [System.Data.DataRow]) | |
{ | |
foreach($item in $InputObject) | |
{ | |
$Table.Rows.Add($item) | |
} | |
return @(,$Table) | |
} | |
} | |
else | |
{ | |
$Table.Rows.Clear() | |
} | |
foreach($item in $InputObject) | |
{ | |
$row = $table.NewRow() | |
if($item) | |
{ | |
foreach ($prop in $item.PSObject.Get_Properties()) | |
{ | |
if($table.Columns.Contains($prop.Name)) | |
{ | |
$row.Item($prop.Name) = $prop.Value | |
} | |
} | |
} | |
[void]$table.Rows.Add($row) | |
} | |
} | |
return @(,$Table) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment