且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Powershell设置文件夹权限

更新时间:2022-11-28 11:56:06

一种方法是使用WMI和UNC路径.

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

$AccessRule = New-Object  system.security.accesscontrol.filesystemacces-s-rule("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.filesystemacces-s-rule("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
}