0

I am trying to get the following powershell script to output the results into a .csv. The .csv file is created but there is no data, what am I doing wrong?

$rootfolder = Get-ChildItem -recurse -Path [PATH] | where {$_.Attributes -eq 'Directory'} 
foreach ($userdir in $rootfolder) { 
$userdir.FullName 
get-acl $userdir.FullName | foreach {write-host "The owner is : " $_.Owner -Foregroundcolor Green } | export-csv c:\powershell\test.csv
Write-Host "`n" 
} 
4

1 回答 1

0

您无法foreach以您尝试的方式在管道中发送进一步的输出;您需要单独导出每个项目。修改后的脚本(请注意,我还修改了您where-object以测试PSIsContainer返回的每个项目的属性gci):

$rootfolder = Get-ChildItem -recurse -Path [path] | where {$_.PSIsContainer} 
foreach ($userdir in $rootfolder) { 
get-acl -path $userdir.FullName | export-csv c:\powershell\test.csv -NoTypeInformation -Append
}

但是,我认为您会对结果感到失望。get-acl生成对象的集合,而不是基本字符串的集合。例如,如果我get-acl在我的配置文件目录上运行,我会得到:

PS C:\Users\ME> get-acl .|select-object access|fl *


Access : {System.Security.AccessControl.FileSystemAccessRule, System.Security.AccessControl.FileSystemAccessRule,
         System.Security.AccessControl.FileSystemAccessRule}

运行上面的脚本,我在AccessCSV 列中看到的只是System.Security.AccessControl.AuthorizationRuleCollection- 因为这是生成的,一个集合。要获得完整的ACL,您需要进行额外的处理以提取该集合的每个元素。对也返回集合的其他字段重复此操作。

于 2013-05-20T13:21:14.307 回答