1

I am trying to get the permissions with groups and members to list all the detailed share. I found this script. I am trying to execute it but something wrong. I want it return a result that will filter in CSV file as follow:

$queryPath = "s:\it\install"
$targetFile = "C:\Test.csv"

$Table = @()

$Record = [ordered]@{
    "Path" = ""
    "IdentityReference" = ""
    "Class" = ""
    "GrpMember" = ""
}

$foldersToQuery = Get-ChildItem $queryPath | Where {$_.PSIsContainer} | select -expandproperty FullName

foreach ($folder in $foldersToQuery) {
    $Record.Path = $folder
    $permissions = Get-Acl $folder | select -expandproperty Access

    foreach ($permission in $permissions) { {
        [string]$id = $permission.IdentityReference
        $SamAccountName = $id.Split('\')[1]
        $ADObject = Get-ADObject -Filter ('SamAccountName -eq "{0}"' -f $SamAccountName) }
        $Record.IdentityReference = $permission.IdentityReference.ToString()

        switch ($ADObject.ObjectClass) {
            'user' {
                $Record.Class = $ADObject.ObjectClass
                $Record.GrpMember = ""
                $objRecord = New-Object PSObject -property $Record
                $Table += $objrecord
            }
            'group' { {
                $Record.Class = $ADObject.ObjectClass
                $members = Get-ADGroupMember $SamAccountName }

                foreach ($member in $members) {{
                    $Record.GrpMember = $member.name
                    $objRecord = New-Object PSObject -property $Record
                    $Table += $objrecord
                }
            }
        }
    }
}
}
$Table | Export-Csv $targetFile -NoTypeInformation -Encoding UTF8
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 2
    When you say something is wrong....whats wrong? – ArcSet Mar 12 '19 at 19:05
  • What does this produce and what do you wish for it to produce? – lit Mar 12 '19 at 22:01
  • it is not giving the results. did some one try it? – Rabah Ferhi Mar 12 '19 at 23:41
  • getting this message in a loop: PS H:\scripts> .\permisions.ps1 [string]$id = $permission.IdentityReference $SamAccountName = $id.Split('\')[1] $ADObject = Get-ADObject -Filter ('SamAccountName -eq "{0}"' -f $SamAccountName) – Rabah Ferhi Mar 12 '19 at 23:53
  • 1
    Have you tries removing the ''`enter code here`''. Also, it is customary to add a link to the page where you found this original code. – Theo Mar 13 '19 at 10:07
  • @Theo The "enter code here" was most likely added when posting the question, so I removed it. – Ansgar Wiechers Mar 13 '19 at 11:54
  • If you already have the SamAccountName of the user or group, change the strangly formatted line to get the ADObject into `$ADObject = Get-ADObject -Filter "SamAccountName -eq '$SamAccountName'"` – Theo Mar 13 '19 at 13:53
  • original link: https://stackoverflow.com/questions/52150963/powershell-get-acl-get-members-instead-of-group – Rabah Ferhi Mar 13 '19 at 14:26

0 Answers0