简体   繁体   English

Powershell:将所有者应用于每个用户名的多个文件和文件夹

[英]Powershell: Apply owner to multiple files and folders per username

We have a server that houses the My Documents folder for all our users. 我们有一台服务器为所有用户提供“我的文档”文件夹。 Some of the folder owner changed to administrator. 一些文件夹所有者已更改为管理员。 I am trying to devise a PowerShell script that goes to each user's root my documents folder and applies the user as the owner for all the sub folders and files with in it. 我正在尝试设计一个PowerShell脚本,该脚本将转到每个用户的“我的文档”文件夹的根目录,并将该用户用作其中所有子文件夹和文件的所有者。 Is this Possible? 这可能吗?

I have the following from a previous script that attempted to set the user as full permissions per each my document root folder: 我从以前的脚本中获得了以下内容,该脚本试图将用户设置为每个我的文档根文件夹的完全权限:

$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
}

Firstly ensure that the share and root folder permissions for redirected folders follow best practice . 首先,确保重定向文件夹的共享和根文件夹权限遵循最佳实践


I would use the NTFSSecurity PS Module ( blog on its use ). 我将使用NTFSSecurity PS模块( 有关其用法的博客 )。 This module has commands that are much easier to understand as they follow they way your would set permissions via the GUI. 该模块具有易于理解的命令,因为它们遵循的方式与您通过GUI设置权限的方式相同。

$FolderPath = "E:\mydocuredir"

Get-ChildItem -Path $FolderPath -Directory | ForEach-Object{
    Add-NTFSAccess -Path $_.FullName -Account "domain\$($_.Name)" -AccessRights FullControl -AppliesTo ThisFolderSubfoldersAndFiles
}

To set Owner, replace the Add-NTFSAccess command with: 要设置所有者,请将Add-NTFSAccess命令替换为:

Set-NTFSOwner -Path $_.FullName -Account "domain\$($_.Name)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM