PowerShell获取共享驱动器NTFS权限报告报错排查求助
共享驱动器权限报告脚本执行报错排查
执行的PowerShell脚本
$FolderPath = Get-ChildItem -Directory -Path "\\cst-fileserver\Company" -Recurse -Force $Output = @() ForEach ($Folder in $FolderPath) { $Acl = Get-Acl -Path $Folder.FullName ForEach ($Access in $Acl.Access) { $Properties = [ordered]@{'Folder Name'=$Folder.FullName;'Group/User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited} $Output += New-Object -TypeName PSObject -Property $Properties } } $Output | Out-GridView $Report | Export-Csv -path "C:\new\FolderPermissions.csv"
(注:原脚本末尾存在语法错误,多了一个冗余闭合大括号},已修正)
报错信息
Get-ChildItem : Could not find a part of the path 'D:\\shares\\Company\\2MPTRegulatory\\REGULATORY\\PLPI\\PLPI GRANTED\\MPT\\0883 COO(0168) Pulmicort Turbohaler 200 PL\\0883 Granted docs - take effect from 19-06-18\\CST0168 archive\\0168 Pulmicort Turbohaler 200 PL\\sample scans-checks\\CST0168 Polish Pulmicort Turbuhaler 200ug 100doses (90x38x38) 23.06.2014'. At line:1 char:15 + ... olderPath = Get-ChildItem -Directory -Path "D:\\shares" -Recurse -Forc ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ReadError: (D:\\shares\\Compa...x38) 23.06.2014:String) \[Get-ChildItem\], DirectoryNotFoundException + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand Get-ChildItem : Could not find a part of the path 'D:\\shares\\Company\\2MPTRegulatory\\REGULATORY\\PLPI\\PLPI GRANTED\\MPT\\0883 COO(0168) Pulmicort Turbohaler 200 PL\\0883 Granted docs - take effect from 19-06-18\\CST0168 archive\\0168 Pulmicort Turbohaler 200 PL\\sample scans-checks\\CST0168 Polish Pulmicort Turbuhaler 200ug 100doses (BUYER) 23.09.2015'. At line:1 char:15 + ... olderPath = Get-ChildItem -Directory -Path "D:\\shares" -Recurse -Forc ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ReadError: (D:\\shares\\Compa...YER) 23.09.2015:String) \[Get-ChildItem\], DirectoryNotFoundException + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand Get-ChildItem : Could not find a part of the path 'D:\\shares\\Company\\2MPTRegulatory\\REGULATORY\\PLPI\\PLPI GRANTED\\MPT\\0883 COO(0168) Pulmicort Turbohaler 200 PL\\0883 Granted docs - take effect from 19-06-18\\CST0168 archive\\0168 Pulmicort Turbohaler 200 PL\\sample scans-checks\\CST0168 Pulmicort Turbohaler 200mcg 100 doses PL(88x38x38) 26.10.16'. At line:1 char:15 + ... olderPath = Get-ChildItem -Directory -Path "D:\\shares" -Recurse -Forc ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ReadError: (D:\\shares\\Compa...38x38) 26.10.16:String) \[Get-ChildItem\], DirectoryNotFoundException + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand
排查与解决步骤
- 路径一致性校验:脚本中指定的扫描路径是
\\cst-fileserver\Company,但报错显示实际访问的是D:\\shares\\Company,先确认目标路径是否正确,统一使用UNC路径(\\服务器名\共享名)或本地盘符路径格式。 - 路径存在性验证:手动访问报错中的具体路径,确认文件夹是否被删除、移动,或是网络共享映射失效(若
D:\shares是映射盘符)。 - 权限检查:确保运行脚本的账号对目标文件夹及所有嵌套子文件夹拥有读取权限。
- 处理长路径限制:报错中的路径长度已接近Windows传统260字符路径限制,可启用长路径支持:
- 组策略中启用
计算机配置>管理模板>系统>文件系统>启用Win32长路径 - 注册表中添加
HKLM\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled(DWORD值设为1)
- 组策略中启用
- 脚本容错优化:给
Get-ChildItem和Get-Acl添加错误处理,避免个别文件夹报错中断整个脚本,优化后脚本示例:
$FolderPath = Get-ChildItem -Directory -Path "\\cst-fileserver\Company" -Recurse -Force -ErrorAction SilentlyContinue -ErrorVariable PathErrors $Output = @() ForEach ($Folder in $FolderPath) { try { $Acl = Get-Acl -Path $Folder.FullName -ErrorAction Stop ForEach ($Access in $Acl.Access) { $Properties = [ordered]@{ 'Folder Name' = $Folder.FullName 'Group/User' = $Access.IdentityReference 'Permissions' = $Access.FileSystemRights 'Inherited' = $Access.IsInherited } $Output += New-Object -TypeName PSObject -Property $Properties } } catch { Write-Warning "无法获取文件夹权限: $($Folder.FullName),错误信息: $_" } } $Output | Out-GridView $Output | Export-Csv -path "C:\new\FolderPermissions.csv" -NoTypeInformation # 输出无法访问的路径列表 if ($PathErrors) { Write-Host "以下路径无法访问:" $PathErrors | ForEach-Object { Write-Host "- $($_.TargetObject)" } }
(注:原脚本中$Report变量未定义,已替换为实际数据变量$Output;添加-NoTypeInformation避免CSV文件头部生成冗余类型信息)
内容的提问来源于stack exchange,提问作者Kyle P
相关产品推荐
相关产品推荐

