获取共享文件夹权限

时间:2018-11-16 12:49:35

标签: powershell

我对powershell相当陌生,我需要做一个共享文件夹权限报告。我有以下代码:

$path = \\server\shared_folder
dir $path | where { $_.PsIsContainer } | % { $path1 = $_.fullname; Get-Acl $_.Fullname | % { $_.access | Add-Member -MemberType NoteProperty '.\Application Data' -Value $path1 -passthru }} | Export-Csv $reportpath

但是输出是:

  

\\ server \ shared_folder \ folder1; FileSystemRights; AccessControlType; IsInherited; InheritanceFlags; PropagationFlags

  \\ server \ shared_folder \ folder2; FileSystemRights; AccessControlType; IsInherited; InheritanceFlags; PropagationFlags

我需要以下输出:

  

\\ server \ shared_folder; FileSystemRights; AccessControlType; IsInherited; InheritanceFlags; PropagationFlags

如果未指定,为什么要执行“ dir” recursiveley?如果我指定要告诉我的位置?

2 个答案:

答案 0 :(得分:1)

要获得确切的答案,您需要:

$path = "\\server\shared_folder"
dir $path | where { $_.PsIsContainer } | % { $path1 = $_.Root; Get-Acl $_.Fullname | % { $_.access | Add-Member -MemberType NoteProperty '.\Application Data' -Value $path1 -passthru }} | Export-Csv $reportpath

在ForEach-Object(%)之后注意“ $ _。Root”。但我认为以下方法会更好,因为这样可以在“。\ Application Data”下看到文件夹名称:

$path = "\\server\shared_folder"
dir $path | where { $_.PsIsContainer } | % { $path1 = $_.Name; Get-Acl $_.Fullname | % { $_.access | Add-Member -MemberType NoteProperty '.\Application Data' -Value $path1 -passthru }} | Export-Csv $reportpath

希望这会有所帮助,因为您的问题不是十分清楚。

答案 1 :(得分:0)

也请您帮个忙,并在PowerShell ISE中编写代码并使用PowerShell StyleGuide指南:

$path = "\\server\shared_folder"
$shares = Get-ChildItem $path | Where-Object { $_.PsIsContainer }
$data = foreach($share in $shares){ 
    $path = $share.Name
    $acls = Get-Acl $share.Fullname 
    foreach($acl in $acls){ 
        $acl.access | Add-Member -MemberType NoteProperty '.\Application Data' -Value $path -passthru
    } 
} 
$data | Export-Csv $reportpath

这使代码更具可读性,并且以后更容易进行故障排除或修改代码。此版本不输出共享文件夹内的文件夹的ChildItems。仅将文件夹放在“ \ server \ shared_folder”中。