Hi, I am working on a script to generate CSV after filtering out columns of certain Data Types.
In particular, I am wondering how I would specify an Identity type column comprised of:
column (Data Type: INT, Property: (Identity Increment=1 and Identity Seed=1))
My code is below at the bottom (and the actual filter items are in the next to last line - just below).
Exclude-ColumnsByType -ExcludeType System.String, System.DateTime
Thanks for any help.
# !! Define the function to process each pipeline input object and,
# !! for efficiency, use a steppable pipeline to invoke Select-Object
function Exclude-ColumnsByType {
param(
[Parameter(Mandatory, ValueFromPipeline)]
$InputObject,
[Parameter(Mandatory)]
[string[]] $ExcludeType
)
begin {
$propertiesToInclude = $null
}
process {
if (-not $propertiesToInclude) {
$propertiesToInclude = $InputObject.psobject.Properties.Where({ $ExcludeType -notcontains $_.TypeNameOfValue }).Name
$steppablePipeline = { Select-Object -Property $propertiesToInclude }.GetSteppablePipeline()
$steppablePipeline.Begin($true)
}
$steppablePipeline.Process($InputObject)
}
end {
$steppablePipeline.End()
}
}
#Initiate a connection
$MyConnection = Connect-DbaInstance -SqlInstance LOCALHOST\MSSQLSERVER -TrustServerCertificate
#Define a list of tables
$Tables = @('aaaa1_table4', 'aaaa1_table5', 'newspic', 'news')
#Iterate through the list
foreach ($Table in $Tables) {
#Build the query
$Query = "SELECT * FROM $Table;"
#Build the output file path and remove the schema, dot, and v from the file name
#and append the .csv extension
$CsvPath = "C:\CSVOut\b\$Table.csv"
# !! Exclude-ColumnsByType is now called in the pipeline to operate on the
# !! Invoke-DbaQuery output objects.
#Run the query and export results to CSV
Invoke-DbaQuery -SqlInstance $MyConnection -Database cop -Query $Query |
Exclude-ColumnsByType -ExcludeType System.String, System.DateTime |
Export-Csv $CsvPath -Delimiter '|' -NoTypeInformation -Encoding unicode
}