【问题标题】:Powershell taking ownership of folder before set-aclPowershell 在 set-acl 之前取得文件夹的所有权
【发布时间】:2016-09-28 20:42:39
【问题描述】:

我可以接管所有权,然后将 acl 设置为文件夹吗?我有一个folders.txt 文件,其中有文件夹的位置。

例如:

D:\Dept\CC\NorthRiver\16-17\StaffAdministration

然后我在新建上一年文件夹结构,并将往年文件夹的权限和权限复制到新文件夹年份匹配文件夹中。由于文件夹的所有权,我遇到了一个问题。如果我不是所有者,我无法复制某些文件夹的权限,我会收到Set-ACL : The security identifier is not allowed to be the owner of this object. 有什么办法解决这个问题吗?

我尝试添加该行(将所有者更改为我,但也没有用):

get-item $currentFolder.Replace("16-17", "15-16") | set-owner -Account 'VDB-TST1\Administrators'

有没有人知道我可以如何做到这一点? 这是我的完整脚本:

Function Get-FileName{
[CmdletBinding()]
Param(
    [String]$Filter = "|*.*",
    [String]$InitialDirectory = "C:\")

    [void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $InitialDirectory
    $OpenFileDialog.filter = $Filter
    [void]$OpenFileDialog.ShowDialog()
    $OpenFileDialog.filename
}


    #Get and Set the ACL to the new years folder structure
    foreach ($currentFolder in (GC (Get-FileName -InitialDirectory $env:USERPROFILE\Desktop -Filter "Text files (*.txt)|*.txt|All files (*.*)|*.*"))) {
    md $currentFolder # Create Folder
    get-item $currentFolder.Replace("16-17", "15-16") | set-owner -Account 'VDB-TST1\Administrators'
    Get-ACL $currentFolder.Replace("16-17", "15-16") | Set-ACL $currentFolder 
}

【问题讨论】:

    标签: powershell


    【解决方案1】:

    我认为您遇到了 this post. 中描述的 Set-ACL 和 Get-ACL 的相同限制尝试更改

    Get-ACL $currentFolder.Replace("16-17", "15-16") | Set-ACL $currentFolder
    

    (Get-Item $currentFolder.Replace("16-17", "15-16")).GetAccessControl('Access') | Set-ACL $currentFolder
    

    作为替代方案,您可以使用robocopy 从一个目录复制 ntfs 权限,然后将它们应用到另一个目录。

    robocopy $currentFolder.Replace("16-17", "15-16") $currentfolder /copy:S /SECFIX
    

    希望这会有所帮助。

    【讨论】:

    • 这不会将新文件夹保留为同一所有者,因为我正在创建它?只是出于好奇,如果我想保留同一个所有者,这也可能吗?
    • 这只是设置 NTFS 文件的权限。 robocopy 只是设置权限,并不实际复制任何文件或文件夹。如果您想通过 powershell 设置文件夹的所有者,您需要查看 takeown.exe 或更多 elegant solution crafted by Boe Prox
    【解决方案2】:

    Powershell 原生的 Set-ACL cmdlet 非常糟糕。我建议使用可用的NTFS 模块。我曾多次尝试使用 Set-ACL,但它总是浪费我更多的时间,而不是真正有用。

    【讨论】:

      猜你喜欢
      • 2016-08-13
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 2018-12-03
      相关资源
      最近更新 更多