commands

^

ConvertTo-DbaDataTable

Author Chrissy LeMaire (@cl), netnerds.net
Availability Windows, Linux, macOS

 

Want to see the source code for this command? Check out ConvertTo-DbaDataTable on GitHub.
Want to see the Bill Of Health for this command? Check out ConvertTo-DbaDataTable.

Synopsis

Creates a DataTable for an object.

Description

Creates a DataTable based on an object's properties. This allows you to easily write to SQL Server tables.

Thanks to Chad Miller, this is based on his script. https://gallery.technet.microsoft.com/scriptcenter/4208a159-a52e-4b99-83d4-8048468d29dd

If the attempt to convert to data table fails, try the -Raw parameter for less accurate datatype detection.

Syntax

ConvertTo-DbaDataTable
    [-InputObject] <PSObject[]>
    [-TimeSpanType <String>]
    [-SizeType <String>]
    [-IgnoreNull]
    [-Raw]
    [-EnableException]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Get-Service | ConvertTo-DbaDataTable

Creates a DataTable from the output of Get-Service.

Example: 2
PS C:\> ConvertTo-DbaDataTable -InputObject $csv.cheesetypes

Creates a DataTable from the CSV object $csv.cheesetypes.

Example: 3
PS C:\> $dblist | ConvertTo-DbaDataTable

Creates a DataTable from the $dblist object passed in via pipeline.

Example: 4
PS C:\> Get-Process | ConvertTo-DbaDataTable -TimeSpanType TotalSeconds

Creates a DataTable with the running processes and converts any TimeSpan property to TotalSeconds.

Required Parameters

-InputObject

The object to transform into a DataTable.

Alias
Required True
Pipeline true (ByValue)
Default Value

Optional Parameters

-TimeSpanType

Specifies the type to convert TimeSpan objects into. Default is 'TotalMilliseconds'. Valid options are: 'Ticks', 'TotalDays', 'TotalHours', 'TotalMinutes', 'TotalSeconds', 'TotalMilliseconds', and 'String'.

Alias
Required False
Pipeline false
Default Value TotalMilliseconds
Accepted Values Ticks,TotalDays,TotalHours,TotalMinutes,TotalSeconds,TotalMilliseconds,String
-SizeType

Specifies the type to convert DbaSize objects to. Default is 'Int64'. Valid options are 'Int32', 'Int64', and 'String'.

Alias
Required False
Pipeline false
Default Value Int64
Accepted Values Int64,Int32,String
-IgnoreNull

If this switch is enabled, objects with null values will be ignored (empty rows will be added by default).

Alias
Required False
Pipeline false
Default Value False
-Raw

If this switch is enabled, the DataTable will be created with strings. No attempt will be made to parse/determine data types.

Alias
Required False
Pipeline false
Default Value False
-EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

Alias
Required False
Pipeline false
Default Value False