我们有一个服务器,为我们所有的用户提供My Documents文件夹。某些文件夹所有者已更改为管理员。我正在尝试设计一个PowerShell脚本,该脚本将发送到每个用户的根文件夹,并将用户作为所有子文件夹和文件的所有者。这可能吗?
我从以前的脚本中获得以下内容,该脚本试图将用户设置为每个文档根文件夹的完全权限:
$FolderPath = "E:\mydocuredir\"
$MyDocsMain = Get-ChildItem -Path $FolderPath -Directory
Get-ChildItem -Path $FolderPath -Directory | ForEach-Object{
$HomeFolders = Get-ChildItem $FolderPath $_.Name -Directory
$Path = $HomeFolders.FullName
$Acl = (Get-Item $Path).GetAccessControl('Access')
$Username = $_.Name
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule($Username, 'FullControl', 'ObjectInherit', 'InheritOnly', 'Allow')
$Acl.SetAccessRule($Ar)
Set-Acl -path $Path -AclObject $Acl
}
首先确保重定向文件夹follow best practice的共享和根文件夹权限。
我会使用NTFSSecurity PS模块(blog on its use)。此模块具有更容易理解的命令,因为它们遵循通过GUI设置权限的方式。
$FolderPath = "E:\mydocuredir"
Get-ChildItem -Path $FolderPath -Directory | ForEach-Object{
Add-NTFSAccess -Path $_.FullName -Account "domain\$($_.Name)" -AccessRights FullControl -AppliesTo ThisFolderSubfoldersAndFiles
}
要设置所有者,请将Add-NTFSAccess
命令替换为:
Set-NTFSOwner -Path $_.FullName -Account "domain\$($_.Name)"