gpt4 book ai didi

powershell - 获取长路径名上的文件夹 NTFS ACL

转载 作者:行者123 更新时间:2023-12-02 23:13:27 24 4
gpt4 key购买 nike

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

谢谢!

$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 创建映射驱动器。像这样的东西:

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 基于临时驱动器和剩余路径。所以这个路径:

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:

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

关于powershell - 获取长路径名上的文件夹 NTFS ACL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27805419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com