从文本文件中重新acl



这个让我很困惑。我正试图用PowerShell来做这件事,但它不一定是…

基本上,我有一个文本文件,其中包含已迁移的大型目录结构的ACL信息(我意识到内容中有很多需要,但我们无法再访问原始文件系统,所以这是我们必须处理的(。

我需要生成一个批处理文件来重新访问新的(复制的(文件系统。

因此,简而言之,我需要转换这样的文本(很抱歉滚动,但我正在努力保持行距(:

Path: \Share.Domain.comDirectory01$Subdirectory01

AccessToString : DOMAINGroup01-Knuckleheads Allow  ReadAndExecute, Synchronize
     BUILTINAdministrators 

Path: \Share.Domain.comDirectory02$Subdirectory01

AccessToString : DOMAINDifferent-Group02 Allow  FullControl
                 BUILTINAdministrators Allow  FullControl

进入新文件(或相同的文件,其实并不重要(,内容如下:

ICacls "\Share.Domain.comDirectory01$Subdirectory01" /Grant "DOMAINGroup01-Knuckleheads":(OI)(CI)R,X,S /t /c /l /q /inheritance:r
ICacls "\Share.Domain.comDirectory01$Subdirectory01" /Grant "BUILTINAdministrators":(OI)(CI)F /t /c /l /q /inheritance:r
ICacls "\Share.Domain.comDirectory02$Subdirectory01" /Grant "DOMAINDifferent-Group02":(OI)(CI)F /c /l /q /inheritance:r
ICacls "\Share.Domain.comDirectory02$Subdirectory01" /Grant "BUILTINAdministrators":(OI)(CI)F /c /l /q /inheritance:r

我半可悲的尝试仍在进行中,但我意识到这个敌人比我的魔力更强大:

    #$ACLs = Get-Content C:ScriptsTestAndTest.txt
    #ForEach-Object ($ACL in $ACLs)
    #{
    #Figure out how to break strings into variables....
    #}
    #Declare Var - Need to populate from the imported text file
    $FilePath = "\Share.Domain.comDirectory01$Subdirectory01"
    $GroupName = "DOMAINDomain Admins"
    $TestPerm = "ReadAndExecute"
    If ($TestPerm = "FullControl"){$Perms = "F"}
    Elseif ($TestPerm = "ReadAndExecute"){$Perms = "RX"}
    Elseif ($TestPerm = "Modify"){$Perms = "M"}
    Elseif ($TestPerm = "Deny"){$Perms = "D"}
    Elseif ($TestPerm = "Read"){$Perms = "R"}
    Elseif ($TestPerm = "Write"){$Perms = "W"}
    cls
    #Build icacls string -Test Output
    Write-Host "ICacls ""$FilePath"" /Grant ""$GroupName"":(OI)(CI)$Perms /t /c /l /q /inheritance:r"
    #Write icacls batch file
    #Out-File "C:ScriptsTestre-acl.cmd" "ICacls ""$FilePath"" /Grant ""$GroupName"":(OI)(CI)$Perms /t /c /l /q /inheritance:r"

我意识到这里有很多工作要做;我才刚刚开始想清楚。例如,我认为我需要将权限列表到一个数组中,并以此方式构建字符串。也必然有特殊权限等。

但现在,我正试图弄清楚如何导入文本文件,然后将其分解为变量。。。就像我说的,我在Powershell工作,但它真的可以是任何东西。。。也许是VB还是Python?

非常感谢!

好吧,我认为你的大部分问题都是解析文本,所以这主要是我在这里帮助的。我确实构建了字符串,如果您有更多想要包含的权限,您应该能够找到如何将它们添加到switch中。为此,我们希望将整个文本文件读取为一个大的多行字符串。然后,我们根据关键字"路径:"将其分成块。然后,我将每条记录的路径作为字符串输出,获取权限,解析出帐户、允许/拒绝和单独访问。然后,我将访问转换为icacls接受的短版本,并将参数构建为格式化字符串。

我向主机输出,但一旦您确信它看起来很合适,就可以删除Write-Host "和后面的",它只会执行它(假设icacls位于您所在的同一文件夹中,或者位于PATH环境变量中(。

$Text = Get-Content C:ScriptsTestAndTest.txt -Raw
$Records = $Text -split "(?s)(Path:.*?)(?=Path:|$)"|?{$_}
#Loop through each file/ACL
ForEach($Record in $Records){
    $FilePath = ($Record -split "[rn]+")[0].Substring(6)
    $PermRecords = ($Record -split "(?s)AccessToString : "|Select -skip 1) -split "[rn]+"|?{$_}
    $Perms = $PermRecords|?{$_ -match "(.*\.*?)s+(Allow|Deny)(.*)$"}|%{[pscustomobject]@{'Account'=$Matches[1].Trim();'Type'=$Matches[2];'Perms'=$Matches[3].Trim()}}
    #Loop through each perm for the current file
    $Perms | %{
        #Convert friendly names to abbreviations
        $ShortPerms = ''
        Switch -regex ($_.Perms){
            "FullControl" {$ShortPerms = "F";Continue}
            "ReadAndExecute" {$ShortPerms += "RX,"}
            "Synchronize" {$ShortPerms += "S,"}
            "Modify" {$ShortPerms += "M,"}
            "Read(?=,|$)" {$ShortPerms += "R,"}
            "Write" {$ShortPerms += "W,"}
        }
        $ShortPerms = $ShortPerms.TrimEnd(',')
        $Arguments = '"{0}" /{4} "{1}":(OI)(CI)({2}) /t /c /l /q /inheritance:r' -f $FilePath, $_.Account, $ShortPerms,$(If($_.Type -eq 'Allow'){'Grant'}else{'Deny'})
        write-host "& ICAcls $Arguments"
    }
}

如果-Raw参数对你不起作用,你可以用来解决这个问题

(Get-Content C:ScriptsTestAndTest.txt) -join "`r`n"

如果您有任何问题或问题,请告诉我。

相关内容

  • 没有找到相关文章

最新更新