Export-CSV仅获得"长度"

时间:2016-09-13 04:57:03

标签: excel powershell split export-to-csv

当我尝试导出到CSV列表时,我只获得"长度"的所有数字。 到达分割点之前的.Count属性,然后将CSV数组拆分为具有将从此时开始使用的新名称的新文件。可能是什么问题?

$RootFolder = Get-Content "c:\DRIVERS\myfile.txt"

foreach ($arrayOfPaths in $RootFolder){

  $csv  = $arrayofPaths -replace '^\\\\[^\\]+\\([^\\]+)\\([^\\]+).*', 'C:\output\Company_name_${1}_${2}.csv'
  $csvIndex = 1
  $maxRows = 1000000
  $rowsLeft = $maxRows

  Get-ChildItem $arrayOfPaths -Recurse | Where-Object {$_.mode -match "d"} | ForEach-Object {
  #$csv  = $_.FullName -replace '^\\\\[^\\]+\\([^\\]+)\\([^\\]+).*', 'C:\output\Company_name_${1}_${2}.csv'# <- construct CSV path here
  $path = $_.FullName
  $thisCSV = Get-Acl $path | Select-Object -Expand Access |
    Select-Object @{n='Path';e={$path}}, IdentityReference, AccessControlType,
                  FileSystemRights |
    ConvertTo-Csv
if ($thisCSV.count -lt $rowsLeft) {
    $thisCSV | Export-Csv $csv -append -noType
    $rowsLeft -= $thisCSV.count
} else {
    $thisCSV[0..($rowsLeft - 1)] | Export-Csv $csv -append -noType
    $csvIndex++
    $csv = $csv -replace '\.csv$', "$csvIndex.csv"
    if ($thisCSV.count -gt $rowsLeft) {
        $thisCSV[$rowsLeft..($thisCSV.count - 1)] | Export-Csv $csv -append -noType
    }
    $rowsLeft = $maxRows - ($thisCSV.count - $rowsLeft)
}


  }

}

1 个答案:

答案 0 :(得分:0)

Export-CSV用于将PSCustomObjects作为输入,而不是文本行。

$thisCSV = Get-Acl $path | Select-Object -Expand Access |
    Select-Object @{n='Path';e={$path}}, IdentityReference, AccessControlType,
                  FileSystemRights |
    ConvertTo-Csv

此行的输出将类似于:

#TYPE Selected.System.Security.AccessControl.FileSystemAccessRule
"Path","IdentityReference","AccessControlType","FileSystemRights"
"c:\test","BUILTIN\Administrators","Allow","FullControl"

至少三行,一个字符串数组。字符串数组有哪些属性?

PS C:\> 'a','b' | Get-Member -MemberType Property


   TypeName: System.String

Name   MemberType Definition       
----   ---------- ----------       
Length Property   int Length {get;}

长度。您在CSV中看到的唯一属性,因为Export-CSV导出所有属性,这是唯一的属性。

修复:从| ConvertTo-CSV行中删除Get-ACL,将自定义对象保留为自定义对象,然后让导出句柄转换它们。

(这也应该修复计数,因为每次尝试导出1+行数据时,它不会计算3行以上的文本。)