通过Powershell将安全权限附加到文件夹

时间:2015-02-03 21:36:17

标签: powershell iis windows-server-2012

尝试使用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权限的关键字吗?

enter image description here

修改

通过切换Allow的{​​{1}} / Deny值,我发现每个规范只更改了文件夹上的FileSystemRights权限。快速截图:

enter image description here

2 个答案:

答案 0 :(得分:3)

当您知道自己究竟在寻找什么时,这很容易找到。你需要的是[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

由此您应该能够操纵代码来完成您想要的任务。

答案 1 :(得分: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的文件系统权限和继承标志的特定组合的最直接方法是手动创建它并在高级安全性设置中检查结果:

"Security" tab of properties dialog "Advanced Security Settings" dialog "Change Advanced Security Settings" dialog "Permission Entry" dialog