我正在尝试使用PowerShell获得某些网络文件夹上所有权限的列表。不幸的是,我遇到了可怕的PathTooLongException,所以我试图使用Robocopy来解决。但是我是PowerShell的新手,因此希望能有所帮助。我想出的最简单的命令是
Get-Childitem "S:\StartingDir" -recurse | Get-Acl | Select-Object path,accestostring | Export-Csv "C:\export.csv"
那行得通,除了我得到的异常(exception)以外,我都可以做。如何将Robocopy插入此语句以绕过异常?有什么想法吗?
最佳答案
首先,使用以下几行创建一个批处理文件,例如getShortFilename.bat:
@ECHO OFF
echo %~s1
这将返回传递给它的长文件名的短文件名。当Get-Acl由于路径较长而失败时,以下脚本将使用该脚本来获取短文件名。然后,它将使用带有cacls的短路径来返回权限。
$files = robocopy c:\temp NULL /L /S /NJH /NJS /NDL /NS /NC
remove-item "c:\temp\acls.txt" -ErrorAction SilentlyContinue
foreach($file in $files){
$filename = $file.Trim()
# Skip any blank lines
if($filename -eq ""){ continue }
Try{
Get-Acl "$filename" -ErrorAction Stop| Select-Object path, accesstostring | Export-Csv "C:\temp\acls.txt" -Append
}
Catch{
$shortName = &C:\temp\getShortFilename.bat "$filename"
$acls = &cacls $shortName
$acls = $acls -split '[\r\n]'
#create an object to hold the filename and ACLs so that export-csv will work with it
$outObject = new-object PSObject
[string]$aclString
$firstPass = $true
# Loop through the lines of the cacls.exe output
foreach($acl in $acls){
$trimmedAcl = $acl.Trim()
# Skip any blank lines
if($trimmedAcl -eq "" ){continue}
#The first entry has the filename and an ACL, so requires extra processing
if($firstPass -eq $true){
$firstPass = $false
# Add the long filename to the $exportArray
$outObject | add-member -MemberType NoteProperty -name "path" -Value "$filename"
#$acl
# Add the first ACL to $aclString
$firstSpace = $trimmedAcl.IndexOf(" ") + 1
$aclString = $trimmedAcl.Substring($firstSpace, $trimmedAcl.Length - $firstSpace)
} else {
$aclString += " :: $trimmedAcl"
}
}
$outObject | add-member -MemberType NoteProperty -name "accesstostring" -Value "$aclString"
$outObject | Export-Csv "C:\temp\acls.txt" -Append
}
}
笔记:
关于windows - 由于PathTooLongException而使用PowerShell和Robocopy获取ACL信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37755822/