On a number of the servers where I work the share folder permissions have become cluttered with direct permissions for some of our techs due to them needing to take ownership. I have figured out how to fix the ownership issue so it won't happen anymore but I am stuck on the cleanup of these permissions. unfortunately when I run this command nothing happens not even an error. I am guessing its a logic error of some kind on my part but I cant spot it. Any help would be appreciated.
# $vData is the root path
Get-Item $vData | foreach { $_ ; $_ | Get-ChildItem -directory -Force -Recurse }| foreach { $currentDir = $_; $acl = ($_ | Get-Acl).Access; $IDs = $acl | select identityreference ; foreach ($ID in $IDs) { if (($ID.ToString()).endswith('-admin')) { $acesToRemove = $acl | where{ $_.IsInherited -eq $false -and $_.IdentityReference -eq $ID }; $acl.RemoveAccessRuleAll($acesToRemove); Set-Acl -AclObject $acl $currentDir.ToString(); } } }
since its a 1 liner I have split it below for ease of reading.
Get-Item $vData |`
foreach {`
$_ ; $_ | Get-ChildItem -directory -Force -Recurse `
}`
| foreach {`
$currentDir = $_;`
$acl = ($_ | Get-Acl).Access; `
$IDs = $acl | select identityreference ;`
foreach ($ID in $IDs) { `
if (($ID.ToString()).endswith('-admin')) {`
$acesToRemove = $acl | where{ $_.IsInherited -eq $false -and $_.IdentityReference -eq $ID };`
$acl.RemoveAccessRuleAll($acesToRemove); `
Set-Acl -AclObject $acl $currentDir.ToString(); `
}`
}`
}
the code to remove the permissions is based off of code I found here
Remove a user from ACL completely using PowerShell