Quick way to convert CSV types to original types with PowerShell's Import-Csv cmdlet? -
i'm using powershell v2 , have custom type i've defined called material using code this:
add-type @" public struct material { public string businessunit; public string source; public string primarysource; public string legacymaterialnumber; } "@
...and i've got collection of objects of type i'm writing csv file using export-csv cmdlet. works fine , produces data original type specified this:
#type material businessunit,source,primarysource,legacymaterialnumber bu1,nafo,nafo-fg,17502 bu1,nafo,nafo-fg,17504 bu1,nafo,nafo-fg,17739 bu1,nafo,nafo-fg,17837 bu1,nafo,nafo-fg,17841
the issue i'm running when import data using import-csv each line created type:
csv:material
...instead of original type started with:
material
in specific case issue because want pass each material function parameter typed material. parse each row , rebuild material objects data of course, there quick way import csv data original type?
import-csv
wants create psobject
based type. can create conversion function (or filter) convert material type. pipe output of import-csv
function , target array:
filter convert-csvtomaterial([parameter(mandatory=$true,valuefrompipeline=$true)]$csvmaterial) { $material = new-object -typename material $material.businessunit = $csvmaterial.businessunit $material.legacymaterialnumber = $csvmaterial.legacymaterialnumber $material.primarysource = $csvmaterial.primarysource $material.source = $csvmaterial.source $material } $importedmaterial = import-csv .\materiallist.csv | convert-csvtomaterial
Comments
Post a Comment