PoshCode Logo PowerShell Code Repository

Out-DataTable by Chad Miller 24 months ago (modification of post by Chad Miller view diff)
View followups from Chad Miller | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/3534"></script>download | new post

Creates a DataTable for an object, based on script by Marc van Orsouw

  1. #######################
  2. function Get-Type
  3. {
  4.     param($type)
  5.  
  6. $types = @(
  7. 'System.Boolean',
  8. 'System.Byte[]',
  9. 'System.Byte',
  10. 'System.Char',
  11. 'System.Datetime',
  12. 'System.Decimal',
  13. 'System.Double',
  14. 'System.Guid',
  15. 'System.Int16',
  16. 'System.Int32',
  17. 'System.Int64',
  18. 'System.Single',
  19. 'System.UInt16',
  20. 'System.UInt32',
  21. 'System.UInt64')
  22.  
  23.     if ( $types -contains $type ) {
  24.         Write-Output "$type"
  25.     }
  26.     else {
  27.         Write-Output 'System.String'
  28.        
  29.     }
  30. } #Get-Type
  31.  
  32. #######################
  33. <#
  34. .SYNOPSIS
  35. Creates a DataTable for an object
  36. .DESCRIPTION
  37. Creates a DataTable based on an objects properties.
  38. .INPUTS
  39. Object
  40.     Any object can be piped to Out-DataTable
  41. .OUTPUTS
  42.    System.Data.DataTable
  43. .EXAMPLE
  44. $dt = Get-psdrive| Out-DataTable
  45. This example creates a DataTable from the properties of Get-psdrive and assigns output to $dt variable
  46. .NOTES
  47. Adapted from script by Marc van Orsouw see link
  48. Version History
  49. v1.0  - Chad Miller - Initial Release
  50. v1.1  - Chad Miller - Fixed Issue with Properties
  51. v1.2  - Chad Miller - Added setting column datatype by property as suggested by emp0
  52. v1.3  - Chad Miller - Corrected issue with setting datatype on empty properties
  53. v1.4  - Chad Miller - Corrected issue with DBNull
  54. v1.5  - Chad Miller - Updated example
  55. v1.6  - Chad Miller - Added column datatype logic with default to string
  56. .LINK
  57. http://thepowershellguy.com/blogs/posh/archive/2007/01/21/powershell-gui-scripblock-monitor-script.aspx
  58. #>
  59. function Out-DataTable
  60. {
  61.     [CmdletBinding()]
  62.     param([Parameter(Position=0, Mandatory=$true, ValueFromPipeline = $true)] [PSObject[]]$InputObject)
  63.  
  64.     Begin
  65.     {
  66.         $dt = new-object Data.datatable  
  67.         $First = $true
  68.     }
  69.     Process
  70.     {
  71.         foreach ($object in $InputObject)
  72.         {
  73.             $DR = $DT.NewRow()  
  74.             foreach($property in $object.PsObject.get_properties())
  75.             {  
  76.                 if ($first)
  77.                 {  
  78.                     $Col =  new-object Data.DataColumn  
  79.                     $Col.ColumnName = $property.Name.ToString()  
  80.                     if ($property.value)
  81.                     {
  82.                         if ($property.value -isnot [System.DBNull]) {
  83.                             $Col.DataType = [System.Type]::GetType("$(Get-Type $property.TypeNameOfValue)")
  84.                          }
  85.                     }
  86.                     $DT.Columns.Add($Col)
  87.                 }  
  88.                 if ($property.IsArray) {
  89.                     $DR.Item($property.Name) =$property.value | ConvertTo-XML -AS String -NoTypeInformation -Depth 1
  90.                 }  
  91.                else {
  92.                     $DR.Item($property.Name) = $property.value
  93.                 }
  94.             }  
  95.             $DT.Rows.Add($DR)  
  96.             $First = $false
  97.         }
  98.     }
  99.      
  100.     End
  101.     {
  102.         Write-Output @(,($dt))
  103.     }
  104.  
  105. } #Out-DataTable

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me