Skip to content

Instantly share code, notes, and snippets.

@nvnivs
Last active August 29, 2015 14:02
Show Gist options
  • Save nvnivs/49219084ea7dd83dc7af to your computer and use it in GitHub Desktop.
Save nvnivs/49219084ea7dd83dc7af to your computer and use it in GitHub Desktop.
Disables "Include inheritable permissions from this object's parent" to a given folder
<#
.Synopsis
Recursively updates the version of AssemblyInfo.cs files
.Parameter baseDir
The root directory from which to recursively search for AssemblyInfo files
Defaults to the directory of the script.
.Link
https://gist.github.com/z0c
#>
param(
[Parameter(Position=0)] [string] $path = $(throw "missing -path param"),
[Parameter(Position=2)] [string] $allowed = ''
)
$ErrorActionPreference = "stop"
$allowedUsers = @()
foreach ($u in $allowed.Split(',')) {
if ($u.Contains('\')) {
$allowedUsers += $u.ToLowerInvariant()
}
else {
$allowedUsers += ($env:computername + "\" + $u).ToLowerInvariant()
}
}
$acl = Get-Item $path | Get-Acl
$modified = $false
$acl.Access | ? {
$allowedUsers -notcontains $_.IdentityReference.ToString().ToLowerInvariant()
} | % {
Write-Output "Removing access right of user " + $_.IdentityReference
$acl.RemoveAccessRule($_) | out-null
$modified = $true
}
if ($modified) {
$acl | Set-Acl
}
# Disable permission inheritance from parent object
$acl = Get-Item $path | Get-Acl
if (!$acl.AreAccessRulesProtected) {
$acl.SetAccessRuleProtection($true,$false)
$acl | Set-Acl
Write-Output "Disabled inheritable permissions"
}
else {
Write-Output "Inheritable permissions already disabled"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment