How to set permission on folder using powershell

假装没事ソ 提交于 2019-11-30 20:48:16

问题


i am trying to write a script which adds"CreatorOwner" permission to profile$ folders on all file servers;

i.e , add "CreatorOwner" permissions to "\FileServer\Profile$"

can anybody tell me whats the command and syntax for it?

Please do ask if any questions.


回答1:


One way of doing this is using WMI and UNC paths.

$AccessRule = New-Object  system.security.accesscontrol.filesystemaccessrule("CREATOR OWNER","FullControl","ContainerInherit, ObjectInherit","InheritOnly","Allow")
$profileshare = Get-WmiObject Win32_Share -ComputerName fileserver -Filter "name = 'profile$'"
$driveletter, $path = $profileshare.path
$path = $path.TrimStart("\")
$ACL = Get-Acl "\\fileserver\$driveletter`$\$path"
$ACL.SetAccessRule($AccessRule)
Set-Acl \\fileserver\$driveletter`$\$path -AclObject $ACL

Now if you have a list of server names you could do the following:

$servers = @("fileserver1","fileserver2","fileserver3")
$AccessRule = New-Object  system.security.accesscontrol.filesystemaccessrule("CREATOR OWNER","FullControl","ContainerInherit, ObjectInherit","InheritOnly","Allow")
$servers | % {
$profileshare = Get-WmiObject Win32_Share -ComputerName $_ -Filter "name = 'profile$'"
$driveletter, $path = $profileshare.path
$path = $path.TrimStart("\")
$ACL = Get-Acl "\\$_\$driveletter`$\$path"
$ACL.SetAccessRule($AccessRule)
Set-Acl \\$_\$driveletter`$\$path -AclObject $ACL
}



回答2:


This will do it:

#Change the CSV file path

$Permissions = Import-Csv C:\Test.CSV -delimiter '|'
ForEach ($line in $Permissions)
$inherit = [system.security.accesscontrol.InheritanceFlags]"ContainerInherit, 
ObjectInherit"

$propagation = [system.security.accesscontrol.PropagationFlags]"None" 

$acl = Get-Acl $line.Path
$acl.SetAccessRuleProtection($True, $False)
$accessrule = New-Object system.security.AccessControl.FileSystemAccessRule("domain users", "Read", $inherit, $propagation, "Allow")

#Adding the Rule
$acl.AddAccessRule($accessrule)

#Setting the Change
Set-Acl $line.Path $acl 

What the CSV file looks Like

\SRV01\Folder1\Folder2 etc.



来源:https://stackoverflow.com/questions/14286948/how-to-set-permission-on-folder-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!