< Back

ConvertTo-DbaDataTable

Mon Jan 13, 2020 9:21 am

NAME ConvertTo-DbaDataTable



SYNOPSIS

Creates a DataTable for an object.





SYNTAX

ConvertTo-DbaDataTable [-InputObject] <System.Management.Automation.PSObject[]> [-TimeSpanType <String>]

[-SizeType <String>] [-IgnoreNull <Switch>] [-Raw <Switch>] [-EnableException <Switch>] [<CommonParameters>]





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/s ... 48468d29dd



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





PARAMETERS

-EnableException [<Switch>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-IgnoreNull [<Switch>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-InputObject [<System.Management.Automation.PSObject[]>]

The object to transform into a DataTable.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Raw [<Switch>]

If this switch is enabled, the DataTable will be created with strings. No attempt will be made to

parse/determine data types.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SizeType [<String>]

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

'String'.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-TimeSpanType [<String>]

Specifies the type to convert TimeSpan objects into. Default is 'TotalMilliseconds'. Valid options are:

'Ticks', 'TotalDays', 'TotalHours', 'TotalMinutes', 'TotalSeconds', 'TotalMilliseconds', and 'String'.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS

System.Object[]





NOTES





Tags: DataTable, Table, Data

Author: Chrissy LeMaire (@cl), netnerds.net



Website: https://dbatools.io/

Copyright: (c) 2018 by dbatools, licensed under MIT

License: MIT https://opensource.org/licenses/MIT



-------------------------- 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.



RELATED LINKS

https://dbatools.io/ConvertTo-DbaDataTable