windows 如何使用powershell设置文件夹权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14286948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set permission on folder using powershell
提问by Darktux
i am trying to write a script which adds"CreatorOwner" permission to profile$ folders on all file servers;
我正在尝试编写一个脚本,为所有文件服务器上的 profile$ 文件夹添加“CreatorOwner”权限;
i.e , add "CreatorOwner" permissions to "\FileServer\Profile$"
即,将“CreatorOwner”权限添加到“\FileServer\Profile$”
can anybody tell me whats the command and syntax for it?
谁能告诉我它的命令和语法是什么?
Please do ask if any questions.
如果有任何问题,请提问。
回答by jbockle
One way of doing this is using WMI and UNC paths.
一种方法是使用 WMI 和 UNC 路径。
$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
}
回答by Romero Silva
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
CSV 文件是什么样的
\SRV01\Folder1\Folder2 etc.
\SRV01\Folder1\Folder2 等