Export-CSV only gets the “Length”

梦想的初衷 提交于 2019-12-04 07:09:39

问题


when I try to export to a CSV list, I only get all number for "Length" .Count property until the split point is reached, then split the CSV array to a new file with a new name that will be used from this point on. What might be the issue?

$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:


Export-CSV is built to take PSCustomObjects as input, not lines of text.

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

The output of this line will be something like:

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

At least three lines, an array of string. What properties does an array of string have?

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


   TypeName: System.String

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

Length. The only property you see in the CSV, because Export-CSV is exporting all the properties, and that's the only property.

Fix: Remove | ConvertTo-CSV from the Get-ACL line, leave your custom objects as custom objects and let the export handle converting them.

(This should also fix the counting, because it's not counting 3+ lines of text while trying to export 1+ line of data every time).



来源:https://stackoverflow.com/questions/39462438/export-csv-only-gets-the-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!