Shared Folder ACL remotely

Hi I am trying to get shared folder reports from multiple servers remotely, I am able to get the reports using below code, but the thing is if a folder is shared with 3 user account then i am getting those detail in 3 separate line , because of get-smbshareaccess , need help in joining those details in single line seperated by any delimiter

$cred = Get-Credential


$result = Invoke-Command -ComputerName 'ESHWAR' -ScriptBlock {
Get-SmbShare -Special $false | Where-Object { (!$_.Name.Endswith('$')) }|
ForEach-Object { Get-SmbShareAccess -Name $_.Name } | Sort-Object Name

} -Credential $cred | Select-Object -Property @{ Label="Server_Name";Expression={($_.PSComputerName).Tostring()}},
@{Label = "Share_Name";Expression={($_.Name).ToString()}},
@{Label = "User_Name";Expression={($_.AccountName).ToString()}},
@{Label = "Rights";Expression={($_.AccessRight).ToString()}} | Export-Csv -Path report.csv -Append -NoTypeInformation

 

Hi Eshwar,

I just tweaked your code a bit, and now give it a try…

$Cred = Get-Credential

$ScriptBlock = [scriptblock]::Create({
    Get-SmbShare -Special $false | Where-Object { (!$_.Name.Endswith('$')) } | ForEach-Object { Get-SmbShareAccess -Name $_.Name } 
})

$Result = Invoke-Command -ComputerName 'ESHWAR' -ScriptBlock $ScriptBlock -Credential $Cred

$UniqueInResult = $Result | Select-Object -Property PSComputerName, Name, AccessRight -Unique

$Output = @()

$Output += ForEach ($Item in $UniqueInResult)
{
    $UserName   = ($Result | Where-Object {$_.Name -eq $Item.Name -and $_.PSComputerName -eq $Item.PSComputerName -and $_.AccessRight -eq $Item.AccessRight}).AccountName -join ';'
    New-Object -TypeName psobject -Property @{
        Server_Name = $Item.PSComputerName
        Share_Name  = $Item.Name
        User_Name   = $UserName
        Rights  = $Item.AccessRight
    }
}

$Output | Select-Object -Property Server_Name, Share_Name, User_Name, Rights | Export-Csv -Path report.csv -Append -NoTypeInformation

Thank you.

Hi Kiran,

Thanks a lot man, really helped me a lot, i got the desired output but i did some little tweak to the code you provided.

I had 3 folders , Initially from your code for 1st folder it was good, but for the 2nd and 3rd, it again went to next line, so removed AcessRights evaluaion from foreach loop, because for some reason O/P for Access rights was coming in numerical format and rights was not mandatory for me now.

After that got some duplicate lines so used unique parameter in last select-object just before passing to the files

$Cred = Get-Credential

$ScriptBlock = [scriptblock]::Create({
Get-SmbShare -Special $false | Where-Object { (!$_.Name.Endswith('$')) } | ForEach-Object { Get-SmbShareAccess -Name $_.Name } 
})

$Result = Invoke-Command -ComputerName 'ESHWAR' -ScriptBlock $ScriptBlock -Credential $Cred

$UniqueInResult = $Result | Select-Object -Property PSComputerName, Name -Unique

$Output = @()

$Output += ForEach ($Item in $UniqueInResult)
{
$UserName = ($Result | Where-Object {$_.Name -eq $Item.Name -and $_.PSComputerName -eq $Item.PSComputerName }).AccountName -join ';'
New-Object -TypeName psobject -Property @{
Server_Name = $Item.PSComputerName
Share_Name = $Item.Name
User_Name = $UserName

}
}

$Output | Select-Object * -Unique | Export-Csv -Path report.csv -Append -NoTypeInformation



Thanks again