[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[string]
$Path
,
[Parameter(Mandatory =
$false
)]
[string]
$ComparePath
,
[switch]
$IncludeInherited
,
[switch]
$PassThru
)
function CollectAccessRules([string]
$path
, [switch]
$includeInherited
) {
$root
= (
Get-Item
$path
).FullName
Write
-Verbose
"Collecting folders in $root..."
$folders
=
Get-ChildItem
-Path
$root
-Directory
-Recurse
|
Sort-Object
-Property
FullName
Write
-Verbose
"$root contains $($folders.Count) folders"
foreach
(
$folder
in
$folders
) {
Write
-Verbose
$folder
.FullName
$rules
= (
Get-Acl
-Path
$folder
.FullName).Access
foreach
(
$rule
in
$rules
) {
if (!
$rule
.IsInherited
-or
$includeInherited
) {
[PSCustomObject]@{
Path =
$folder
.FullName.Substring(
$root
.Length + 1)
RuleType =
$rule
.AccessControlType
Identity =
$rule
.IdentityReference
Access =
$rule
.FileSystemRights
IsInherited =
$rule
.IsInherited
Inheritance =
$rule
.InheritanceFlags
Propagation =
$rule
.PropagationFlags
}
}
}
}
}
$rules_1 = CollectAccessRules
$Path
-IncludeInherited
:
$IncludeInherited
if (!
$ComparePath
) {
if (
$PassThru
) {
$rules_1
} else {
$rules_1 | Out
-GridView
-Title
"Security access rules for '$Path'"
}
} else {
$rules_2 = CollectAccessRules
$ComparePath
-IncludeInherited
:
$IncludeInherited
$diff
=
Compare-Object
$rules_1 $rules_2
-Property
Path, Identity, RuleType, Access, Propagation, IsInherited, Inheritance
if (
$PassThru
) {
$diff
} else {
$diff
| Out
-GridView
-Title
"Security access rule differences '$Path' <=> '$ComparePath'"
}
}