【问题标题】:Powershell: Apply owner to multiple files and folders per usernamePowershell:将所有者应用于每个用户名的多个文件和文件夹
【发布时间】:2018-04-03 14:54:16
【问题描述】:

我们有一个服务器,其中包含所有用户的“我的文档”文件夹。一些文件夹所有者更改为管理员。我正在尝试设计一个 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
}

【问题讨论】:

  • my docs 文件夹的结构是什么,类似于E:\mydocuredir\username\?folder?
  • 是的。 mydocuredir 后面的用户名文件夹,那么就是他们的文件和文件夹。

标签: powershell windows-server windows-networking


【解决方案1】:

首先确保重定向文件夹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)"

【讨论】:

  • 我明白这段代码是分配完全权限的,这似乎比我原来的更好,但我正在寻找分配用户的所有者。
  • 更新答案:)
  • 这条指令会递归地应用于所有文件和子文件夹及其文件吗?
  • 我尝试了该指令,但它只更改了父文件夹中的所有者。
  • 查看Enable-NTFSInheritance 命令。
猜你喜欢
  • 2021-03-21
  • 2015-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-25
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多