David Brierton
David Brierton

Reputation: 7397

Powershell taking ownership of folder before set-acl

Can I take over ownership and then set-acl to a folder? I have a folders.txt file where I have the location of the folder.

For Example:

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

Then I am creating a new year of the previous year folder structure and copying the rights and permissions of the previous years folders to the new folder years matching folder. I ran into an issue though because of ownership of the folder. If I am not the owner I can not duplicate the permissions of certain folders and I receive Set-ACL : The security identifier is not allowed to be the owner of this object. Is there any way around this?

I tried adding the line (to change the owner to me but that did not work either):

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

Does anyone have any ideas of how I may accomplish this? This is the full script I have:

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 
}

Upvotes: 0

Views: 2354

Answers (2)

StephenP
StephenP

Reputation: 4081

I think you are running into the same limitations of Set-ACL and Get-ACL described in this post. try changing

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

to

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

As an alternative you can use robocopy to copy the ntfs permissions from one directory and then apply them to another.

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

Hope this helps.

Upvotes: 1

creamers
creamers

Reputation: 36

The Set-ACL cmdlet native to powershell is pretty terrible. I would suggest using the NTFS module that is available. I have tried playing with Set-ACL several times and it always wastes more of my time rather than actually being useful.

Upvotes: 0

Related Questions