使用csv导出/导入ACL

时间:2014-12-16 16:11:31

标签: powershell acl

我想在csv文件中导出从根文件夹开始的每个子文件夹的ACL,然后在另一台计算机上导入镜像文件夹。

我使用此代码从C:\ Users \ user \ Desktop \ a导出:

Get-ChildItem "C:\Users\user\Desktop\a" -Recurse | ?{ $_.PsIsContainer } | %{
$Path = $_.FullName
# Exclude inherited rights from the report
(Get-Acl $Path).Access | ?{ !$_.IsInherited } | Select-Object `
    @{n='Path';e={ $Path }}, IdentityReference, AccessControlType, `
    InheritanceFlags, PropagationFlags, FileSystemRights
} | Export-CSV "C:\Users\user\Desktop\Permissions.csv"

以及导入ACL的代码:

$par = Import-Csv -Path "C:\Users\user\Desktop\Permissions.csv"
foreach ( $i in $par ) { 
    $path= $i.Path
    $IdentityReference= $i.IdentityReference
    $AccessControlType=$i.AccessControlType
    $InheritanceFlags= $i.InheritanceFlags
    $PropagationFlags=$i.PropagationFlags
    $FileSystemRights=$i.FileSystemRights
    echo $path $IdentityReference
    $acl = Get-Acl C:\Users\user\Desktop
    $permission = $i.IdentityReference,$i.FileSystemRights,$i.AccessControlType
    $accessRule = new-object System.Security.AccessControl.FileSystemAccessRule $permission
    $acl.SetAccessRule($accessRule)
    $acl | Set-Acl $path
}

Permissions.csv是这样的:

#TYPE Selected.System.Security.AccessControl.FileSystemAccessRule
"Path","IdentityReference","AccessControlType","InheritanceFlags","PropagationFlags","FileSystemRights"
"C:\Users\user\Desktop\a\b","DITOADMIN\pluto","Allow","None","None","ReadAndExecute, Synchronize"
"C:\Users\user\Desktop\a\b\c","DITOADMIN\pluto","Allow","ContainerInherit, ObjectInherit","None","ReadAndExecute, Synchronize"
"C:\Users\user\Desktop\a\b\c","DITOADMIN\admin","Allow","None","None","FullControl"
"C:\Users\user\Desktop\a\b\c","DITOADMIN\pippo","Allow","ContainerInherit, ObjectInherit","None","ReadAndExecute, Synchronize"

不幸的是,导入操作不起作用,因为只导入了最后一个权限(因此仅针对pippo用户而不是针对pluto)。

有谁知道为什么?

2 个答案:

答案 0 :(得分:1)

我已经修好了。 问题是线,我在这里使用的路径是错误的(应该是CSV导入的路径)

$acl = Get-Acl C:\Users\user\Desktop
$permission = $i.IdentityReference,$i.FileSystemRights,$i.AccessControlType

我已经改变了

$acl = Get-Acl $path
$permission = $IdentityReference, $FileSystemRights, $InheritanceFlags, $PropagationFlags, $AccessControlType

再见

答案 1 :(得分:0)

这几乎是完美的。在这里,您可以将CSV的权限添加到文件夹中,但是您可以保留旧权限。您不要将ACL从一个文件夹“重置/复制”到另一个文件夹。

所以不要获取接收文件夹的ACL: $ acl = Get-Acl $ path

我创建了一个新的: $ acl = New-Object System.Security.AccessControl.DirectorySecurity

但是,我失去了所有者属性。所以我需要一种方法来导出CSV中的所有者权利..