# convertto-bcpcsv # # Simple modul to convert a Table-Input to a XML-File # 20150403 frank.carius@netatwork.de Initial Version with parameter for files # 20171108 frank.carius@netatwork.de Change to Pipeline Processing # 20171109 frank.carius@netatwork.de Add Escaping for five special characters [CmdletBinding()] param ( [parameter(ValueFromPipeline=$True)] [string[]]$line, [string]$formatxmlpath, # Specif XML ob BCP Import File to geht column ordering [string]$delimiter= "`t", # this is the default delimiter of BCP Input files [switch]$includeheader = $false # you can add the header and bcp can skip the first line with -F 2 but not with "-w Switch ) begin{ write-verbose "convertto-bcpcsv:Start" [long]$count=0 [array]$header=@() try { write-verbose "convertto-bcpcsv:Loading Colums from $formatxmlpath" [xml]$importxml= get-content $formatxmlpath } catch { write-error "Unable to open/parse $formatxmlpath" exit } foreach ($column in $importxml.BCPFORMAT.ROW.COLUMN){ write-verbose "convertto-bcpcsv: AddHeader $($column.name)" $header += $column.name } } process{ $count++ if ($count%100 -eq 0 ) { write-verbose "convertto-bcpcsv:Line $count" } if ($includeheader -and ($count -eq 1)){ write-verbose "convertto-bcpcsv: Add Header to Output" $header -join $delimiter } [array]$outline = @() foreach ($property in $header) { [string]$value = $_.($property) $value = $value.replace("`t","") $outline+= ($value) } $outline -join $delimiter } end { write-verbose "convertto-bcpcsv::End" }