Skip to content

Instantly share code, notes, and snippets.

@PrzemyslawKlys
Created May 17, 2020 16:11
Show Gist options
  • Save PrzemyslawKlys/bfd29beec0e3e5bf454b2b47c40ed432 to your computer and use it in GitHub Desktop.
Save PrzemyslawKlys/bfd29beec0e3e5bf454b2b47c40ed432 to your computer and use it in GitHub Desktop.
function Remove-WinADSharePermission {
[cmdletBinding(DefaultParameterSetName = 'Path', SupportsShouldProcess)]
param(
[Parameter(ParameterSetName = 'Path', Mandatory)][string] $Path,
[ValidateSet('Unknown')][string] $Type = 'Unknown',
[int] $LimitProcessing = [int32]::MaxValue
)
Begin {
[int] $Count = 0
}
Process {
if ($Path -and (Test-Path -Path $Path)) {
@(Get-Item -Path $Path) + @(Get-ChildItem -Path $Path -Recurse:$true) | Where-Object -FilterScript {
$PathToProcess = $_.FullName
$Permissions = Get-FilePermission -Path $PathToProcess -Extended -IncludeACLObject -ResolveTypes
$OutputRequiresCommit = foreach ($Permission in $Permissions) {
#Write-Verbose "Remove-WinADSharePermission - Removing permissions from $PathToProcess for $($Permission.Principal) / $($Permission.PrincipalType)"
if ($Type -eq 'Unknown' -and $Permission.PrincipalType -eq 'Unknown' -and $Permission.IsInherited -eq $false) {
try {
Write-Verbose "Remove-WinADSharePermission - Removing permissions from $PathToProcess for $($Permission.Principal) / $($Permission.PrincipalType)"
$Permission.AllACL.RemoveAccessRule($Permission.ACL)
$true
} catch {
Write-Warning "Remove-WinADSharePermission - Removing permissions from $PathToProcess for $($Permission.Principal) / $($Permission.PrincipalType) failed: $($_.Exception.Message)"
$false
}
}
}
if ($OutputRequiresCommit -notcontains $false -and $OutputRequiresCommit -contains $true) {
$Permissions
}
} | Select-Object -First $LimitProcessing | ForEach-Object -Process {
try {
Set-Acl -Path $PathToProcess -AclObject $_[0].ALLACL -ErrorAction Stop
} catch {
Write-Warning "Remove-WinADSharePermission - Commit for $($PathToProcess) failed: $($_.Exception.Message)"
}
}
}
}
End {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment