2016-09-28 100 views
0

我可以接管所有權,然後將set-acl設置爲一個文件夾嗎?我有一個folders.txt文件,我有文件夾的位置。Powershell在set-acl前取得文件夾的所有權-ACL

例如:

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 
} 

回答

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 

希望這會有所幫助。

+0

這不會保留新文件夾作爲相同的所有者,因爲我正在創建它?只是出於好奇,如果我想保持同一個所有者也是可能的嗎? –

+0

這只是設置NTFS文件的權限。 robocopy只設置權限,並不實際複製任何文件或文件夾。如果你想通過powershell設置文件夾的所有者,你需要看看takeown.exe或更多[由Boe Prox製作的優雅解決方案](https://learn-powershell.net/2014/06/24/changing -ownership-的-文件或文件夾,使用-的powershell /)。 – StephenP

0

使用Set-ACL cmdlet的原產於PowerShell是非常可怕的。我建議使用可用的NTFS模塊。我已經嘗試過多次使用Set-ACL,它總是浪費我更多的時間,而不是實際上有用。

相關問題