简体   繁体   English

共享文件区 - 如何改进我的 PowerShell 脚本?

[英]Shared file area - how can I improve my PowerShell script?

I am trying to make a script that creates file areas for different groups in a company.我正在尝试制作一个脚本,为公司中的不同组创建文件区域。 All the members in the group need to have full access to the shared files, but any member from the other groups can not have access - not even see them.组中的所有成员都需要对共享文件具有完全访问权限,但其他组中的任何成员都不能访问 - 甚至看不到它们。 This is what I have.这就是我所拥有的。 For the "Limit access" section you need to change up the name of the group, and repeat this for each group.对于“限制访问”部分,您需要更改组的名称,并对每个组重复此操作。 Can I put this in a foreach loop?我可以把它放在一个foreach循环中吗?

I am new to Powershell and really want to learn how to improve my script.我是 Powershell 的新手,我真的很想学习如何改进我的脚本。 So how can I improve and automate this code?那么如何改进和自动化这段代码呢?

# Creates file areas
$folders = ('C:\shares\it-drift','C:\shares\dev-team','C:\shares\regnskap','C:\shares\renhold','C:\shares\HR')
mkdir -path $folders
$folders | Where-Object {$_ -like "*shares*"} | ForEach-Object {$name = (Get-Item $_).name; $DfsPath = (‘\\sec.core\files\’ + $name); $targetPath = (‘\\dc1\’ + $name);New-DfsnFolderTarget -Path $dfsPath -TargetPath $targetPath}

# Limits access
$folder = ('C:\shares\dev-team') 
$ACL = Get-Acl -path \\sec.core\files\dev-team
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("sec.core\g_dev_team","FullControl","Allow")
$ACL.SetAccessRule($AccessRule)
$ACL | Set-Acl -Path "\\sec.core\files\dev-team"
$ACL.SetAccessRuleProtection($true,$true)
$ACL = Get-Acl "\\sec.core\files\dev-team"
$ACL.Access | where {$_.IdentityReference -eq "BUILTIN\Users" } | foreach { $acl.RemoveAccessRuleSpecific($_) }
Set-Acl "\\sec.core\files\dev-team" $acl
(Get-ACL -Path "\\sec.core\files\dev-team").Access | Format-Table IdentityReference,FileSystemRights,AccessControlType,IsInherited,InheritanceFlags -AutoSize

Appreciate all tips:)感谢所有提示:)

Here is how you can automate set of the same ACL for each shared folder:以下是为每个共享文件夹自动设置相同 ACL 的方法:

# Creates file areas
$folders = @(
    'C:\shares\it-drift'
    'C:\shares\dev-team'
    'C:\shares\regnskap'
    'C:\shares\renhold'
    'C:\shares\HR'
)

mkdir -path $folders

$folders | Where-Object {$_ -like "*shares*"} | 
    ForEach-Object {
        $name = (Get-Item $_).name
        $DfsPath = '\\sec.core\files\' + $name
        $targetPath = '\\dc1\' + $name
        New-DfsnFolderTarget -Path $dfsPath -TargetPath $targetPath
    }

# Limits access
foreach($folder in $folders)
{
    $leaf = Split-Path $folder -Leaf
    $path="\\sec.core\files\$leaf"
    $acl = Get-Acl -Path $path
    $AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("sec.core\g_dev_team","FullControl","Allow")
    $acl.SetAccessRule($AccessRule)
    Set-Acl -Path $path -AclObject $acl
    $acl.SetAccessRuleProtection($true,$true)
    $acl = Get-Acl "\\sec.core\files\dev-team"
    $acl.Access.where({$_.IdentityReference -eq "BUILTIN\Users"}).foreach({
        $acl.RemoveAccessRuleSpecific($_)
    })
    Set-Acl -Path $path -AclObject $acl
    (Get-ACL -Path $path).Access |
        Format-Table IdentityReference,FileSystemRights,AccessControlType,IsInherited,InheritanceFlags -AutoSize
}

Try to use vertical coding instead of horizontal coding.尝试使用垂直编码而不是水平编码。 People reading your code will be thankful.阅读您的代码的人将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM