Quiron
Quiron

Reputation: 370

Get shared folder permissions

Im quite new with powershell and I need to do a shared folders permission report. I've the following code:

$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

But the output is:

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

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

I need the following output:

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

Why is doing the "dir" recursiveley if I do not specify it? If I am specifying where im telling to do so?

Upvotes: 0

Views: 772

Answers (2)

Bernard Moeskops
Bernard Moeskops

Reputation: 1283

Also please do yourself a favour and write the code in PowerShell ISE and use the PowerShell StyleGuide guidelines:

$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

This makes the code readable and makes it easier to troubleshoot or modify the code later on. This version does not output ChildItems of the folders inside the shared folder. Only the folders placed inside "\server\shared_folder".

Upvotes: 0

Bernard Moeskops
Bernard Moeskops

Reputation: 1283

To get the exact answer you are asking for:

$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

Notice the "$_.Root" after the ForEach-Object (%). But in my opinion the following is better because this way you see the foldername under '.\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

Hope this helps, because your question is not very clear imo.

Upvotes: 1

Related Questions