首页 > 解决方案 > 脚本以集体更改文件夹权限

问题描述

我最近继承了一些混乱,我正在尝试编写解决方案的脚本……不幸的是,我不擅长编写脚本。有没有办法创建一个脚本,它将:

  1. 查找目录中的所有文件夹

  2. 按名称识别它们

  3. 将它们绑定到正确的用户(文件夹名称与 AD 用户名相同)

  4. 设置用户权限(匹配用户的完全控制 - 其他域用户没有权限)

我一直在尝试使用这样的东西......但我对变量如何工作的理解在这里低于标准。

$Acl = Get-Acl "C:\MyFolder"
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule("$username","FullControl","Allow")
$Acl.SetAccessRule($Ar)
Set-Acl "C:\MyFolder" $Acl

如果有人愿意帮助我,将不胜感激。

标签: windowspowershellscriptingpermissionsactive-directory

解决方案


尝试利用 NTFS 模块。

文件系统安全 PowerShell 模块 4.2.3

允许使用 PowerShell 更轻松地管理文件和文件夹的权限

https://gallery.technet.microsoft.com/scriptcenter/1abd77a5-9c0b-4a2b-acef-90dbb2b84e85

#Get permissions from all files or folders in the current folder 
dir | Get-NTFSAccess 

#to read and also remove only the explicitly assigned ones 
dir | Get-NTFSAccess -ExcludeInherited | Remove-NTFSAccess 


#to backup permissions just pipe what Get-NTFSAccess returns to Export-Csv 
dir | Get-NTFSAccess -ExcludeInherited | Export-Csv permissions.csv 

#to retore the permissions pipe the imported data to Get-NTFSAccess 
#As the imported data also contains the path you do not need to specify the item 
Import-Csv .\permissions.csv | Get-NTFSAccess 

https://www.powershellgallery.com/packages/NTFSSecurity/4.2.3


推荐阅读