Out-DataTable by Chad Miller 17 months ago
View followups from Chad Miller | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2116"></script>download | new post
Creates a DataTable for an object, based on script by Marc van Orsouw
- #######################
- <#
- .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
- .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()
- $object.PsObject.get_properties() | foreach
- {
- if ($first)
- {
- $Col = new-object Data.DataColumn
- $Col.ColumnName = $_.Name.ToString()
- $DT.Columns.Add($Col)
- }
- if ($_.IsArray)
- { $DR.Item($_.Name) =$_.value | ConvertTo-XML -AS String -NoTypeInformation -Depth 1 }
- else { $DR.Item($_.Name) = $_.value }
- }
- $DT.Rows.Add($DR)
- $First = $false
- }
- }
- End
- {
- Write-Output @(,($dt))
- }
- } #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.
PowerShell Code Repository