PoshCode Archive  Artifact [9e04e41e83]

Artifact 9e04e41e832047c6d95bb1885449403d99c54f9ae703402afaf3af6e4897fd68:

  • File Out-DataTable.ps1 — part of check-in [fad282b8a4] at 2018-06-10 13:17:37 on branch trunk — Creates a DataTable for an object, based on script by Marc van Orsouw (user: Chad Miller size: 2579)

# encoding: ascii
# api: powershell
# title: Out-DataTable
# description: Creates a DataTable for an object, based on script by Marc van Orsouw
# version: 1.0
# type: script
# author: Chad Miller
# license: CC0
# function: Out-DataTable
# x-poshcode-id: 2954
# x-derived-from-id: 3433
# x-archived: 2016-07-29T09:18:47
# x-published: 2012-09-13T18:13:00
#
#
#######################
<#
.SYNOPSIS
Creates a DataTable for an object
.DESCRIPTION
Creates a DataTable based on an objects properties.
.INPUTS
Object
    Any object can be piped to Out-DataTable
.OUTPUTS
   System.Data.DataTable
.EXAMPLE
$dt = Get-Alias | Out-DataTable
This example creates a DataTable from the properties of Get-Alias and assigns output to $dt variable
.NOTES
Adapted from script by Marc van Orsouw see link
Version History
v1.0  - Chad Miller - Initial Release
v1.1  - Chad Miller - Fixed Issue with Properties
v1.2  - Chad Miller - Added setting column datatype by property as suggested by emp0
v1.3  - Chad Miller - Corrected issue with setting datatype on empty properties
v1.4  - Chad Miller - Corrected issue with DBNull
.LINK
http://thepowershellguy.com/blogs/posh/archive/2007/01/21/powershell-gui-scripblock-monitor-script.aspx
#>
function Out-DataTable
{
    [CmdletBinding()]
    param([Parameter(Position=0, Mandatory=$true, ValueFromPipeline = $true)] [PSObject[]]$InputObject)

    Begin
    {
        $dt = new-object Data.datatable  
        $First = $true 
    }
    Process
    {
        foreach ($object in $InputObject)
        {
            $DR = $DT.NewRow()  
            foreach($property in $object.PsObject.get_properties())
            {  
                if ($first)
                {  
                    $Col =  new-object Data.DataColumn  
                    $Col.ColumnName = $property.Name.ToString()  
                    if ($property.value)
                    {
                        if ($property.value -isnot [System.DBNull])
                        { $Col.DataType = $property.value.gettype() }
                    }
                    $DT.Columns.Add($Col)
                }  
                if ($property.IsArray)
                { $DR.Item($property.Name) =$property.value | ConvertTo-XML -AS String -NoTypeInformation -Depth 1 }  
                else { $DR.Item($property.Name) = $property.value }  
            }  
            $DT.Rows.Add($DR)  
            $First = $false
        }
    } 
     
    End
    {
        Write-Output @(,($dt))
    }

} #Out-DataTable