【问题标题】:Take ownership of a folder and set inheritance with PowerShell获取文件夹的所有权并使用 PowerShell 设置继承
【发布时间】:2021-11-01 06:58:35
【问题描述】:

尝试将文件夹的所有者设置为域管理员并强制继承所有子文件夹/文件。使用我发现的脚本组合:

$Account = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList $DomainAdmins;

#Get a list of folders and files
$ItemList = Get-ChildItem -Path $Dir -Recurse;

#Iterate over files/folders
foreach ($Item in $ItemList) {
    $Acl = $null; # Reset the $Acl variable to $null
    $Acl = Get-Acl -Path $Item.FullName; # Get the ACL from the item
    $Acl.SetOwner($Account); # Update the in-memory ACL
    $isProtected = $false 
    $preserveInheritance = $false
    $Acl.SetAccessRuleProtection($isProtected, $preserveInheritance)
    Set-Acl -Path $Item.FullName -AclObject $Acl;  # Set the updated ACL on the target item
}

Error: Set-Acl : Cannot bind argument to parameter 'AclObject' because it is null.

有些文件夹分配正确,但不是全部。我怀疑如果没有所有者(可能是从 AD 中删除的帐户)它会中断。

关于如何解决这个问题的任何想法?

【问题讨论】:

  • 我建议查看NTFSSecurity 模块。这比使用内置 ACL 命令要容易得多。有一些文档herehere
  • 谢谢。此时最好的猜测是目录中有长文件路径名。这可能会有所帮助。
  • 是的,该模块使用了 Alphaleonis 库,该库可以绕过 Windows 对路径名限制的限制。
  • 听起来不错。谢谢,我会告诉你情况如何。

标签: powershell active-directory


【解决方案1】:

我们最终会使用它,即使它不能正确处理长文件路径。

Import-Module -Name NTFSSecurity
#Remove Inheritance on user's root folder
    Get-Item $UserRoot | Disable-NTFSAccessInheritance

#Add Domain Admin to user's root folder
    Add-NTFSAccess -Path $UserRoot -Account 'BUILTIN\Administrators', 'yourDomain\Domain Admins' -AccessRights FullControl

#Set Inheritance on all sub-folders on user's directory
    Get-ChildItem -Path $UserRoot -Recurse | Enable-NTFSAccessInheritance -PassThru

【讨论】:

    【解决方案2】:

    检查SetOwner()设置文件夹所有者的方法

    # Define the owner account/group
    $Account = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList 'BUILTIN\Administrators';
    
    # Get a list of folders and files
    $ItemList = Get-ChildItem -Path c:\test -Recurse;
    
    # Iterate over files/folders
    foreach ($Item in $ItemList) {
        $Acl = $null; # Reset the $Acl variable to $null
        $Acl = Get-Acl -Path $Item.FullName; # Get the ACL from the item
        $Acl.SetOwner($Account); # Update the in-memory ACL
        Set-Acl -Path $Item.FullName -AclObject $Acl;  # Set the updated ACL on the target item
    }
    

    FileSystemAccessRule()中指定继承

    $Acl = Get-Acl "\\R9N2WRN\Share"
    
    $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule("user", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
    
    $Acl.SetAccessRule($Ar)
    Set-Acl "\\R9N2WRN\Share" $Acl
    

    查看SO1SO2 了解更多相关信息。

    【讨论】:

      猜你喜欢
      • 2019-11-14
      • 2017-05-31
      • 2019-09-19
      • 2023-03-28
      • 2013-06-06
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      • 2014-02-15
      相关资源
      最近更新 更多