使用前几年对新年文件夹结构脚本

时间:2016-09-27 17:10:35

标签: powershell

是否可以编写一个powershell脚本来创建我在文本文件中的文件夹结构,如下所示:

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

但是要拥有从前一年已经存在的权限和权限吗?文件夹结构哪一年是唯一的区别?

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

我只是想创建新的一年,但保持所有权利和权限与去年相同。上一年还有其他文件所以我试图让txt.file创建文件夹,然后以某种方式抓住前几年的权利和写入新文件夹的权限。这可能吗?

@sapl收到两个错误:

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

1 个答案:

答案 0 :(得分:1)

让我们假设您有一个文件" folders.txt"它包含所需的文件夹,每行一个,你可以这样做:

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读取folders.txt的内容并将其作为字符串数组返回
  • foreach循环遍历数组,每行名为$ currentFolder
  • md创建文件夹(New-Item的别名)
  • Get-ACL获取旧文件夹的ACL /权限。它采用新创建的文件夹的文件夹路径,替换" 16-17"用" 15-16"并获取旧文件夹的ACL。关于Why does Set-Acl on the drive root try to set ownership of the "object"?更改所有者时,Get-ACL无法正常工作。
  • Set-ACL设置从旧文件夹到管道的ACL,并将其设置为新创建的文件夹。

请确保您在高架PowerShell控制台中运行它("以管理员身份运行")。

希望能帮到你

相关问题