PowerShell: Set-Acl Does Not Appear to Work

Filed under: PowerShell, Quick Code, Security — Written by Chrissy on Saturday, July 28th, 2007 @ 7:32 am

If you've ever dealt with NTFS permissions in VBScript, you will no doubt appreciate just how easy PowerShell now makes it to manage access control lists. Basic examples in PowerShell books and around the 'net look something like this:

$directory = "Test"
$acl = Get-Acl $directory
$accessrule = New-Object system.security.AccessControl.FileSystemAccessRule("IUSR_CRACKLIN", "Modify", "Allow")
$acl.AddAccessRule($accessrule)
set-acl -aclobject $acl $directory

In the example above, user "IUSR_CRACKLIN" is given Modify access to the Test directory. Running the code above will not produce any errors but upon checking permission via the GUI, it seems as though the user was added, but no permissions were set.

I thought that perhaps this was an issue with Vista and I tried it on Windows Server 2003. And that's when I noticed that the directory had been given "Special Permissions." When I checked the Advanced permissions, I could see that Modify access had been assigned, but only to "This Folder." Other folders that had the checkboxes checked listed "This Folder, subfolders and files"

Since I wanted the Test directory permissions to match the others, I searched the Google to see which flags would give me "This Folder, subfolders and files." I found Damir Dobric's blog post titled "Directory Security and Access Rules which sported a handy reference table flags that must be set to achieve various scenarios.

Subfolders and Files only InheritanceFlags.ContainerInherit, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly
This Folder, Subfolders and Files    InheritanceFlags.ContainerInherit, InheritanceFlags.ObjectInherit, PropagationFlags.None
This Folder, Subfolders and Files InheritanceFlags.ContainerInherit, InheritanceFlags.ObjectInherit, PropagationFlags.NoPropagateInherit
This folder and subfolders InheritanceFlags.ContainerInherit, PropagationFlags.None
Subfolders only InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly
This folder and files InheritanceFlags.ObjectInherit, PropagationFlags.None
This folder and files InheritanceFlags.ObjectInherit, PropagationFlags.NoPropagateInherit

So it setting the following should give me what I need:
InheritanceFlags.ContainerInherit, InheritanceFlags.ObjectInherit and PropagationFlags.None
.

$directory = "Test"
$inherit = [system.security.accesscontrol.InheritanceFlags]"ContainerInherit, ObjectInherit"
$propagation = [system.security.accesscontrol.PropagationFlags]"None"
$acl = Get-Acl $directory
$accessrule = New-Object system.security.AccessControl.FileSystemAccessRule("IUSR_CRACKLIN", "Modify", $inherit, $propagation, "Allow")
$acl.AddAccessRule($accessrule)
set-acl -aclobject $acl $directory

I then checked the permissions and voila:

Imagine that.. PowerShell can set any number of permissions with about 6 lines of code while VBScript requires over 36 lines JUST to set the constants needed for managing permissions. I'm so excited thinking about the possibilities: PowerShell + Windows Core + SSH is going to be awesome.

6 Comments   -
  • Comment by damir | July 28, 2007 @ 7:45 am

    How to utilize Directory Security and Access Rules in .NET?http://developers.de/blogs/damir_dobric/archive/2007/06/18/directory-security-and-access-rules.aspx#1457

  • Comment by ghjconan | August 27, 2007 @ 8:36 pm

    Thank you :)
    I'm writing a ps script about batch creating floders with certain ACL.

  • [...] 这里插入一段题外话。对很多ITpro而言,.net是开发人员用的,因此也不会把太多时间花在这上。但是我们在实际工作中时不时会遇到需要写一段脚本来解决的问题。很多人的办法就是通过访问脚本中心或者搜索来解决问题。那么对于.net而言,其实我们也可以通过搜索解决问题,在不知道具体.net方法的情况下。昨天我在思考“批量创建带有特定ACL的文件夹”这个问题时,我也是通过搜索知道如何利用.net配合PowerShell来设置ACL。昨天我的搜索关键词是“PowerShell Set-ACL”,搜索结果的第四个正是我需要的内容。在该页面提供的脚本基础之上,我完成了以下代码。 [...]

  • Comment by Colin Bowern | September 25, 2007 @ 3:30 am

    Thanks for the post! That saved a whole bunch of time (and like your experience, cut down my VBScript)

  • Comment by Michael | January 25, 2008 @ 1:24 pm

    Another option use xcacls or cacls... You can do this at the command line of your shell and pass it parameters in 1 line of code. This is what I have done in the past with automating our file server, works, it's slow but it works.

  • [...] PowerShell: Set-Acl Does Not Appear to Work [...]

Leave your comment