powershell - How to pipe foreach output to export-csv -
the code below outputs following.
type system.string length 100
how can output content of strings?
$filein | % { $array = $_.split(" ") if ($array[0] -eq "user") { $name = $array[1]+"."+$array[2] $remaining = "" ($i = 3; $i -ne $array.length; $i++) {$remaining+=$array[$i]+" "} get-aduser $name -properties description, company | % { $name + " - " + $remaining + " - " + $_.description + " - " + $_.company | export-csv $output} } }
export-csv
exporting objects properties csv. you're trying export single string, includes value , length
property.
also, -
not valid delimiter in csv(at least not in .net). type information can removed -notypeinformation
-switch. try this:
$filein | % { $array = $_.split(" ") if ($array[0] -eq "user") { $name = $array[1]+"."+$array[2] $remaining = "" ($i = 3; $i -ne $array.length; $i++) {$remaining+=$array[$i]+" "} get-aduser $name -properties description, company | % { new-object psobject -property @{ "name" = $name "remaining" = $remaining "description" = $_.description "company" = $_.company } } } } | select-object name, remaining, description, company | export-csv $output -delimiter ';' -notypeinformation
i tried understand trying here. give summary of changes:
i'm creating object containing information want export, every row in
$filein
i'm setting order of properties
select-object
after object every line in$filein
has been createdi'm exporting array of objects csv-file delimiter
;
(just show how specify it), , without type-information @ start. if useexport-csv
inside foreach loop, overwrite file every time , you'd have 1 row + header-row in end. in ps3.0 have done inside loop, using-append
switch.
edit if need string format, need use else export-csv
, ex. out-file
-append
switch. ex:
$filein | % { $array = $_.split(" ") if ($array[0] -eq "user") { $name = $array[1]+"."+$array[2] $remaining = "" ($i = 3; $i -ne $array.length; $i++) {$remaining+=$array[$i]+" "} get-aduser $name -properties description, company | % { "$name - $remaining - $($_.description) - $($_.company)" | out-file -append $output } } }
Comments
Post a Comment