Powershell中的icacls权限脚本 [英] icacls permissions script in powershell

查看:256
本文介绍了Powershell中的icacls权限脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先让我说我是脚本新手!基本上,我想做的是编写一个脚本来创建所有这些文件:对于上一年和当年的文件中的13-14,因此我需要一个变量,以便在明年运行脚本时它将是14-15.顶部的文件夹是AD,CC,DC,FS,IT,OP:

Let me start off by saying I am very new to scripting! Basically what I am trying to do is to write a script that will create all of these files: For the 13-14 in the files that is the previous and current year so I need a variable that when I run the script next year it will be 14-15. The top folders are AD,CC,DC,FS,IT,OP:

例如

  • C:\ Test \ AD \ 13-14 \ Audit \ Fir
  • C:\ Test \ CC \ 13-14 \ OSA
  • C:\ Test \ DC \ 13-14 \ Vendor
  • C:\ Test \ FS \ 13-14 \ Maintenance
  • C:\ Test \ IT \ 13-14 \ Detail
  • C:\ Test \ OP \ 13-14 \ Training

(还有更多文件夹,这只是一个例子)

(There are many more folders this is just an example)

到目前为止,我编写的创建所有这些文件夹的脚本是:

The Script I have wrote that creates all of these folders so far is:

$Users = Get-Content "C:\Users\david\Desktop\PowershellFoldersDB.txt" 
ForEach ($user in $users)
{
    $newPath = Join-path "C:\Test" -childpath $user
    New-Item $newPath -type directory
}

好了,现在我要添加的脚本是icacls,它使分类的顶层文件夹被锁定,这样就不能创建新文件夹,不能删除任何文件夹,也不能重命名.但是对于顶层之下的文件夹,我也希望能够根据需要添加新文件夹.任何帮助将不胜感激.谢谢

Alright and now what I am trying to add to this is script is icacls making the Folders at the top level of categorization to be locked such that no new folders could be created, none could be deleted, and none renamed. But for folders underneath the top level I want to be able to add new folders if I need too. Any help would be greatly appreciated. Thanks

推荐答案

您要查找的是Get-ACLSet-ACL.您可以根据需要手动设置一个文件夹,然后对该文件夹运行Get-ACL以从中提取所有安全信息.然后,您可以将其应用于使用Set-ACL创建的新文件夹.像这样:

What you are looking for is Get-ACL and Set-ACL. You can manually setup one folder as you want it to be setup, then run Get-ACL against that to pull all the security info from it. Then you can apply that to the new folders as they are created with Set-ACL. So something like:

[int]$YY = Get-Date -f "yy"
[String]$YY = "$($YY-1)-$YY"
$ACL = Get-ACL C:\Test\Template
$ACLSub = Get-ACL "C:\Test\Template\Sub"
"AD","CC","DC","FS","IT","OP" | %{
    $CurrentRoot = New-Item "C:\Test\$_" -ItemType Directory
    $CurrentSub = New-Item "$CurrentRoot\$YY" -ItemType Directory
    $CurrentRoot | Set-ACL -AclObject $ACL
    $CurrentSub | Set-ACL -AclObject $ACLSub
}

然后,您只需要预先设置Template文件夹和其中的Sub文件夹,以拥有正确的安全权限,便一切就绪.这比在PowerShell IMHO中手动设置所有权限要简单得多.

Then you just need to setup the Template folder and the Sub folder within it to have the correct security permissions ahead of time and you are all set. It's much simpler than setting all the permissions by hand in PowerShell IMHO.

好吧,考虑到文本文件中有一个文件夹列表,这实际上会变得更简单.

Well, considering that you have a folder list in a text file this actually becomes simpler.

$Folders  = Get-Content "C:\Users\david\Desktop\PowershellFoldersDB.txt" 
[int]$YY = Get-Date -f "yy"
[String]$YY = "$($YY-1)-$YY"
$ACL = Get-ACL C:\Test\Template
$ACLSub = Get-ACL "C:\Test\Template\Sub"
$folders |?{$_ -match "^(C:\\Test\\..)(\\\d{2}-\d{2})(\\.*)$"}|%{New-Item "$($Matches[1])\$YY$($Matches[3])" -ItemType directory -force | Out-Null}
GCI "$($matches[1])\.." -Directory | %{
    $_ | Set-ACL -AclObject $ACL
    GCI $_.FullName | Set-ACL -AclObject $ACLSub
}

这篇关于Powershell中的icacls权限脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆