Advertisement
opexxx

AD_Permissions_ReportGEN.ps1

Sep 6th, 2021 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # AD_Permissions_ReportGEN by Dan Murray and Google.
  2.  
  3. Import-Module ActiveDirectory
  4.  
  5. # Array for report.
  6. $report = @()
  7. $schemaIDGUID = @{}
  8.  
  9. # ignore dupe errors if any #
  10. $ErrorActionPreference = 'SilentlyContinue'
  11. Get-ADObject -SearchBase (Get-ADRootDSE).schemaNamingContext -LDAPFilter '(schemaIDGUID=*)' -Properties name, schemaIDGUID |
  12.  ForEach-Object {$schemaIDGUID.add([System.GUID]$_.schemaIDGUID,$_.name)}
  13. Get-ADObject -SearchBase "CN=Extended-Rights,$((Get-ADRootDSE).configurationNamingContext)" -LDAPFilter '(objectClass=controlAccessRight)' -Properties name, rightsGUID |
  14.  ForEach-Object {$schemaIDGUID.add([System.GUID]$_.rightsGUID,$_.name)}
  15. $ErrorActionPreference = 'Continue'
  16.  
  17. # Get a list of AD objects.
  18. $AOs  = @(Get-ADDomain | Select-Object -ExpandProperty DistinguishedName)
  19. $AOs += Get-ADOrganizationalUnit -Filter * | Select-Object -ExpandProperty DistinguishedName
  20. $AOs += Get-ADObject -SearchBase (Get-ADDomain).DistinguishedName -SearchScope Subtree -LDAPFilter '(objectClass=*)' | Select-Object -ExpandProperty DistinguishedName
  21.  
  22. # Loop through each of the AD objects and retrieve their permissions.
  23. # Add report columns to contain the path.
  24. ForEach ($AO in $AOs) {
  25.     $report += Get-Acl -Path "AD:\$AO" |
  26.      Select-Object -ExpandProperty Access |
  27.      Select-Object @{name='organizationalunit';expression={$AO}}, `
  28.                    @{name='objectTypeName';expression={if ($_.objectType.ToString() -eq '00000000-0000-0000-0000-000000000000') {'All'} Else {$schemaIDGUID.Item($_.objectType)}}}, `
  29.                    @{name='inheritedObjectTypeName';expression={$schemaIDGUID.Item($_.inheritedObjectType)}}, `
  30.                    *
  31. }
  32.  
  33. # Filter by single user and export to a CSV file.
  34. $User ='Username'
  35. $report | Where-Object {$_.IdentityReference -like "*$User*"} | Select-Object IdentityReference, ActiveDirectoryRights, OrganizationalUnit, IsInherited -Unique |
  36.  
  37. # Change the path where appropriate.
  38. Export-Csv -Path "C:\AD_Permissions\explicit_permissions.csv" -NoTypeInformation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement