-1

Is it possible to write a powershell script to create a folder structure I have in a text file like so:

D:\Dept\AD\16-17\Audit\FIR
D:\Dept\AD\16-17\Budget\Working
D:\Dept\AD\16-17\Budget\Final
D:\Dept\AD\16-17\Correspondence\MediaReleases
D:\Dept\AD\16-17\Correspondence\DL
D:\Dept\AD\16-17\Correspondence\HF
D:\Dept\AD\16-17\Correspondence\Tax
D:\Dept\CC\QualityAssurance\16-17\Audit\FIR
D:\Dept\CC\QualityAssurance\16-17\Budget\Working
D:\Dept\CC\QualityAssurance\16-17\Budget\Final

but to have the permissions and rights written from the already existing previous year? Folder structure where the year is the only difference?

D:\Dept\AD\15-16\Audit\FIR
D:\Dept\AD\15-16\Budget\Working
D:\Dept\AD\15-16\Budget\Final
D:\Dept\AD\15-16\Correspondence\MediaReleases
D:\Dept\AD\15-16\Correspondence\DL
D:\Dept\AD\15-16\Correspondence\HF
D:\Dept\AD\15-16\Correspondence\Tax
D:\Dept\CC\QualityAssurance\15-16\Audit\FIR
D:\Dept\CC\QualityAssurance\15-16\Budget\Working
D:\Dept\CC\QualityAssurance\15-16\Budget\Final

I am just wanting to create the new year but keep all rights and permissions the same from the previous year. There is other files in the previous year so I am trying to just have txt.file create the folders then somehow grab the previous years right and permissions to write to the new folder. Is this possible?

@sapl Receiving two errors:

Directory: D:\Dept\DC\16-17\OSA


Mode                LastWriteTime     Length Name                                                                                                                                                                        
----                -------------     ------ ----                                                                                                                                                                        
d----         9/27/2016   4:45 PM            OperationalReports                                                                                                                                                          
Set-ACL : The security identifier is not allowed to be the owner of this object.
At line:19 char:56
+     Get-ACL $currentFolder.Replace("16-17", "15-16") | Set-ACL $currentFolder
+                                                        ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (D:\Dept\DC\16-17\OSA\OperationalReports:String) [Set-Acl], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.SetAclCommand


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


Mode                LastWriteTime     Length Name                                                                                                                                                                        
----                -------------     ------ ----                                                                                                                                                                        
d----         9/27/2016   4:45 PM            CFCA                                                                                                                                                                        
Set-ACL : The security identifier is not allowed to be the owner of this object.
At line:19 char:56
+     Get-ACL $currentFolder.Replace("16-17", "15-16") | Set-ACL $currentFolder
+                                                        ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (D:\Dept\CC\Nort...nistration\CFCA:String) [Set-Acl], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.SetAclCommand
David Brierton
  • 6,977
  • 12
  • 47
  • 104

1 Answers1

1

let's assume you have a file "folders.txt" which contains the required folders, one per line, you can do something like this:

foreach ($currentFolder in (Get-Content .\folders.txt)) {
    md $currentFolder  # Create Folder
    (Get-Item $currentFolder.Replace("16-17", "15-16")).GetAccessControl("Access") | Set-ACL $currentFolder
}
  • Get-Content reads the content of folders.txt and returns it as an array of strings
  • foreach loops trough the array, with each line named $currentFolder
  • md creates the folder (alias for New-Item)
  • Get-ACL gets the ACL/Permissions of the old folder. It takes the folder path of the newly created folder, replaces "16-17" with "15-16" and gets the ACL of the old folder. Regarding to Why does Set-Acl on the drive root try to set ownership of the "object"? Get-ACL does not work properly when changing the owner.
  • Set-ACL set the ACL which come trough the pipe from the old folder and sets it to the newly created folder.

Please also ensure, that you run it in an elevated PowerShell Console ("Run as Adminstrator").

Hope that helps you

Community
  • 1
  • 1
sapl
  • 343
  • 3
  • 7
  • http://stackoverflow.com/questions/39748961/set-acl-the-security-identifier-is-not-allowed-to-be-the-owner-of-this-object Is it possible to set-acl without changing ownership? – David Brierton Sep 28 '16 at 15:07