1
0

Out-DataTable.ps1 2.9 KB

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