使用Export-Csv对数据进行分组

时间:2013-10-23 23:52:21

标签: powershell csv

我创建了一个动态对象,希望输出到CSV文件。到目前为止,我只在一个单元格中,在一个单元格中输入值,或者输出对象的描述(例如字符串长度)。

我的HashTable对象是按以下方式创建的:

 $item = @{"Total"=0; "Complete"= 0}
 $obj=@{"AllCounted" = 0 }

 # gets outside data
 foreach($field in $data){

     $obj+=@{$field = $item}

     $obj[$field]+= @{ $subField = $item }

导致以下对象

 $obj{
      "AllCounted" = [int]
      "dynamicField1"= {
                             "Total"    =[int];
                             "Complete" =[int]
                             "SubField1" ={
                                             "Total"    =[int];
                                             "Complete" =[int]
                             }
                         }
      "dynamicField _ N"= {
                            "Total"    =[int];
                            "Complete" =[int]
                            "SubField _ N" ={
                                             "Total"    =[int];
                                             "Complete" =[int]
                             }
                        }
   }

我的目标是

 $obj.Keys|%{ 
         $_ +","+$obj[$_]["Total"]+","+ $obj[$_]["Complete"]  | Out-File "something.csv" -append"

         foreach($innerLoop in $obj[$_].Keys){
                 ","+ $_ +","+$obj[$_]["Total"]+","+ $obj[$_]["Complete"]  | Out-File "something.csv" -append"
        }

}

CSV显示

 Field1  |  Total      | Complete
         |  SubField1  |  Total   | Complete 
         |  SubField2  |  Total   | Complete 
         |  SubField N |  Total   | Complete 


 Field2  |  Total      | Complete
         |  SubField1  |  Total   | Complete 
         |  SubField2  |  Total   | Complete 
         |  SubField N |  Total   | Complete 


 Field N |  Total      | Complete
         |  SubField1  |  Total   | Complete 
         |  SubField2  |  Total   | Complete 
         |  SubField N |  Total   | Complete 

所有想法但仍在努力......

0 个答案:

没有答案