【问题标题】:Append security rights to a folder via Powershell通过 Powershell 将安全权限附加到文件夹
【发布时间】:2015-02-03 21:36:17
【问题描述】:

尝试使用 Powershell,我正在尝试弄清楚如何向我们的用户帐户添加特定权限。下面的代码会将服务帐户添加到文件夹Security 选项卡,但它不会调整权限。知道为什么吗?

#variables
$okeeffename = "WCFService"
$domain = "InsideServices.dev.com"
$okeeffedirectory = "d:\webcontent\$domain\$okeeffename"

#create webcontent and application folders
Write-Host "Creating directories" -ForegroundColor Yellow
New-Item -Path $okeeffedirectory -type directory -ErrorAction Stop

#adjust security for folders
$okeefferights = Get-Acl $okeeffedirectory
$read = New-Object system.security.accesscontrol.filesystemaccessrule($useraccount, "Read", "Allow")
$list = New-Object system.security.accesscontrol.filesystemaccessrule($useraccount, "ListDirectory", "Allow")
$readexecute = New-Object system.security.accesscontrol.filesystemaccessrule($useraccount, "ReadAndExecute", "Allow")
$okeefferights.SetAccessRule($read)
$okeefferights.SetAccessRule($list)
$okeefferights.SetAccessRule($readexecute)
Set-Acl -Path $okeeffedirectory -AclObject $okeefferights

第二个问题:我正在尝试将服务帐户的以下权限添加到文件夹中。有人可以指出 Powershell 用于List Folder Contents 权限的关键字吗?

编辑

通过切换FileSystemRightsAllow/Deny 值,我发现每个规范都只更改文件夹的Special Permissions 权限。快速屏幕截图:

【问题讨论】:

    标签: powershell iis windows-server-2012


    【解决方案1】:

    当您知道自己在寻找什么时,这很容易找到。您需要的是[System.Security.AccessControl.FileSystemRights]。我们可以通过使用 [enum] 来找到可用的权限列表:

    PS C:\windows\system32> [enum]::GetNames([System.Security.AccessControl.FileSystemRights])
    
    ListDirectory
    ReadData
    WriteData
    CreateFiles
    CreateDirectories
    AppendData
    ReadExtendedAttributes
    WriteExtendedAttributes
    Traverse
    ExecuteFile
    DeleteSubdirectoriesAndFiles
    ReadAttributes
    WriteAttributes
    Write
    Delete
    ReadPermissions
    Read
    ReadAndExecute
    Modify
    ChangePermissions
    TakeOwnership
    Synchronize
    FullControl
    

    您可以在一个对象中创建多个权限,例如(这应该允许用户仅读取/执行对文件夹及其内容的访问权限):

    $Rights = [System.Security.AccessControl.FileSystemRights]"ListDirectory,ReadData,Traverse,ExecuteFile,ReadAttributes,ReadPermissions,Read,ReadAndExecute"
    

    我通常设置 ACL 的模板是这样的:

    $Rights = [System.Security.AccessControl.FileSystemRights]"FullControl" 
    
    $InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]"ObjectInherit,ContainerInherit"
    $PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None 
    
    $objType =[System.Security.AccessControl.AccessControlType]::Allow 
    
    $objUser = New-Object System.Security.Principal.NTAccount("Domain\User") 
    
    $objACE = New-Object System.Security.AccessControl.FileSystemAccessRule($objUser, $Rights, $InheritanceFlag, $PropagationFlag, $objType) 
    
    $objACL = Get-ACL "C:\Temp" 
    $objACL.AddAccessRule($objACE) 
    
    Set-ACL "C:\Temp" $objACL
    

    你应该能够操作代码来完成你想要的。

    【讨论】:

    • 哇,非常感谢。有没有推荐的网站或教程来学习语法?
    • 我希望......不幸的是,我自己学习这一切是相当具有挑战性的。这里有一些关于 SO 的好帖子,您可以通读以提供帮助。
    • 工作就像一个魅力。谢谢!
    【解决方案2】:

    要构建一个在“安全”选项卡中显示为“列出文件夹内容”的 ACE,您需要组合 5 个file system rights

    • ListDirectory
    • ReadAttributes
    • ReadExtendedAttributes
    • ReadPermissions
    • Traverse

    并将继承设置为ContainerInherit

    $list = New-Object Security.AccessControl.FileSystemAccessRule($useraccount, 'Traverse,ListDirectory,ReadAttributes,ReadExtendedAttributes,ReadPermissions', 'ContainerInherit', 'None', 'Allow')
    

    找出特定 ACE 的文件系统权限和继承标志的特定组合的最直接方法是手动创建它并在高级安全设置中检查结果:

    【讨论】:

      猜你喜欢
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      相关资源
      最近更新 更多