Get-ChildItem:指定的路径,文件名或两者都太长。完全限定的文件名必须小于 260个字符,目录名称必须少于248个字符。
我仍然需要使用这些对象,并且重命名以缩短名称不是一个选项,所以无论如何实际上处理路径太长而Get-ChildItem无法处理的对象?
然后我发现了一个名为AlphaFS的开源.NET库,它克服了这些限制。如何用AlphaFS替换我的脚本?
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.OOXML.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.OpenXml4Net.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.OpenXml4Net.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.OpenXmlFormats.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\ICSharpCode.SharpZipLib.dll")
$wb = New-Object NPOI.XSSF.UserModel.XSSFWorkbook;
$ws = $wb.CreateSheet("Company_NTFS_Permissions");
$wr = $ws.CreateRow(0);
$wr.createCell(0).setCellValue("Folder Path");
$wr.createCell(1).setCellValue("Users/Groups");
$wr.createCell(2).setCellValue("Permissions");
$wr.createCell(3).setCellValue("AccessControlType");
$wr.createCell(4).setCellValue("Permissions Inherited")
$dirToAudit = Get-ChildItem -Path "C:\Inetpub" -recurse | Where { $_.psIsContainer -eq $true }
$maxRow = 1048576
$intRow = 1
$intNextSheet = 2
foreach ($dir in $dirToAudit)
{
$colACL = Get-Acl -Path $dir.FullName
foreach ($acl in $colACL)
{
$fileNameRow = $ws.CreateRow($intRow)
$fileNameRow.CreateCell(0).SetCellValue($dir.FullName)
$intRow++
if ($intRow -eq $maxRow)
{
$ws = $wb.CreateSheet("Company_NTFS_Permissions" + $intNextSheet);
$intNextSheet++
$intRow = 0
}
foreach ($accessRight in $acl.Access)
{
$values = $ws.CreateRow($intRow)
$values.CreateCell(1).SetCellValue($($AccessRight.IdentityReference).ToString())
$values.CreateCell(2).SetCellValue($($AccessRight.FileSystemRights).ToString())
$values.CreateCell(3).SetCellValue($($AccessRight.AccessControlType).ToString())
$values.CreateCell(4).SetCellValue($($acl.AreAccessRulesProtected).ToString())
$intRow++
if ($intRow -eq $maxRow)
{
$ws = $wb.CreateSheet("Company_NTFS_Permissions" + $intNextSheet);
$intNextSheet++
$intRow = 0
}
}
}
}
$fs = new-object System.IO.FileStream("C:\export\test.xlsx",[System.IO.FileMode]'Create',[System.IO.FileAccess]'Write')
$wb.Write($fs);
$fs.Close()