在长路径名上获取文件夹 NTFS ACL [英] Get Folder NTFS ACL on long path name

查看:36
本文介绍了在长路径名上获取文件夹 NTFS ACL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PS 脚本,如果分配了单个用户,它将返回 NTFS ACL,运行良好,直到我遇到超过 260 个字符的路径.我发现了很多关于路径太长问题和一些变通方法的信息,但我正在努力将解决方案集成到我的脚本中.有什么建议吗?

I have a PS script that will return NTFS ACLs if an individual user is assigned, works well until I hit a path exceeding 260 characters. I've found a lot of information on the path too long problem and some work-arounds but I'm struggling to integrate a solution into my script. Any suggestions?

谢谢!

$DateStart = Get-Date  
$Path = "E:\"           
$PermittedOU1 = "OU=Groups,dc=chiba,dc=localt"
$PermittedOU3 = "OU=System Accounts,OU=Accounts,dc=chiba,dc=local"
$PermittedACL1 = get-adgroup -Filter * -SearchBase $PermittedOU1 
$PermittedACL3 = get-aduser  -Filter * -SearchBase $PermittedOU3
$ObjectPathItem = Get-ChildItem -path $Path -Recurse | where-object {$_.PsIsContainer} | foreach-    object -process { $_.FullName } 
$howmany=0  
$Logfilename = "C:\Users\administrator\Documents\$(get-date -f yyyy-MM-dd-hh-mm).csv"


Add-Content $Logfilename "$DateStart`n"
$totalfolders=0
$i=0

ForEach ($Folder in $ObjectPathItem)
{
$totalfolders++
}



Foreach ($Folder in $ObjectPathItem)                                                
{                                                                            

   $ObjectACL = Get-ACL -Path $Folder                                            
   $i++
   $howmany=0 
   Write-Progress -id 1 -Activity "Folder Recursion" -status "Folders Traversed: "                 -PercentComplete (($i / $totalfolders) * 100)


   Foreach ($ACL in $ObjectACL.access)                                       
    {

        $ACLstring = $ACL.identityreference.Value                           
        $ACLstring = $ACLstring.Replace("CHIBA\","")                        
        if (($ACLstring -notin $PermittedACL1.name)`
        -and ($ACLstring -notin $PermittedACL3.SamAccountName)`
        -and ($ACLstring -notin "NT AUTHORITY\SYSTEM") `
        -and ($ACLstring -notin "BUILTIN\Administrators") `
        -and ($ACLstring -notin "CREATOR OWNER"))   
         {
                 $newline = "`"$Folder`"" + "," + "$ACLString"
                 Add-Content $Logfilename "$newline"   
                 $howmany+=1 
            }

        else {
                $howmany+=1
             }

    }


}
$DateEnd = Get-Date
Add-Content $Logfilename "`n`n$DateEnd"

推荐答案

您通常可以使用的一个选项是使用 New-PSDrive 创建映射驱动器.类似的东西:

One option you can usually use is to create a mapped drive using New-PSDrive. Something like:

Try{
    $ObjectACL = Get-ACL -Path $Folder
}
Catch{
    $SubPathLength = $Folder.FullName.substring(0,200).LastIndexOf('\')
    $NewTempPath = $Folder.FullName.SubString(0,$SubPathLength)
    New-PSDrive -Name Temp4ACL -Provider FileSystem -Root $NewTempPath
    $ObjectACL = Get-ACL "Temp4ACL:$($Folder.FullName.SubSTring($SubPathLength,$Folder.FullName.Length-$SubPathLength))"
}

这将找到路径中第 200 个字符之前的最后一个 \,抓取完整路径的子字符串直到该文件夹​​名称的末尾并创建它的临时驱动器,然后获取基于临时驱动器和剩余路径的 ACL.所以这条路:

That will find the last \ before the 200th character in the path, grab a substring of the full path up to the end of that folder's name and create a temp drive of it, then get the ACL based off the temp drive and the remaining path. So this path:

C:\Temp\Subfolder\Really Long Folder Name\Another Subfolder\ABCDEFGHIJKLMNOPQRSTUVWXYZ\We Are Really Pushing It Now\Im Running Out Of Folder Name Ideas\Hello My Name Is Inigo Montoya\You Killed My Father Prepare To Die\ReadMe.txt

在倒数第二个反斜杠处被剪切.我最终会从以下位置获得 ACL:

Gets cut at the second to last backslash. I would end up getting the ACL from:

Temp4ACL:\You Killed My Father Prepare To Die\ReadMe.txt

这篇关于在长路径名上获取文件夹 NTFS ACL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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