You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

含重音字符目录中Get-ChildItem无法获取NTFS权限问题求助

解决带重音字符目录的NTFS权限读取及重命名问题

一、脚本生成空白CSV的修复方案

空白CSV的核心原因是路径编码解析错误,导致Get-ChildItem未找到目标目录或Get-Acl读取失败,无数据输出。按以下方式调整脚本:

1. 修复第一段脚本(修正路径匹配与属性格式)

替换-Path为-LiteralPath(避免特殊字符被误解析),同时修复属性名的换行问题:

$FolderPath = Get-ChildItem -LiteralPath "\\server\folder1\óóó\company1" -Recurse -Depth 3 -Directory -Force
$Report = @()
Foreach ($Folder in $FolderPath) {
    $Acl = Get-Acl -LiteralPath $Folder.FullName
    foreach ($Access in $acl.Access)
        {
            $Properties = [ordered]@{
                'FolderName' = $Folder.FullName
                'AD Group or User' = $Access.IdentityReference
                'Permissions' = $Access.FileSystemRights
                'Inherited' = $Access.IsInherited
            }
            $Report += New-Object -TypeName PSObject -Property $Properties
        }
}
$Report | Export-Csv "$env:USERPROFILE\Desktop\FolderPermissions.csv" -Encoding UTF8 -Delimiter ";" -NoTypeInformation

2. 修复第二段脚本(移除冗余判断+优化编码)

-Directory已限定只读取目录,无需额外判断$_.PsIsContainer,同时改用UTF8编码输出:

$path = "\\server\folder1\óóó\company1"
$reportpath = "$env:USERPROFILE\Desktop\FolderPermissions.csv"
Get-ChildItem -LiteralPath $path -Recurse -Depth 3 -Directory -Force | 
    ForEach-Object { 
        $currentPath = $_.FullName
        Get-Acl -LiteralPath $currentPath | 
            ForEach-Object { 
                $_.Access | Add-Member -MemberType NoteProperty -Name 'FolderName' -Value $currentPath -PassThru 
            }
    } | Export-Csv -LiteralPath $reportpath -Encoding UTF8 -Delimiter ";" -NoTypeInformation

3. 修复第三段脚本(解决变量覆盖问题)

原脚本重复使用$path变量导致逻辑错误,修改变量名并优化路径匹配:

$rootPath = "\\server\folder1\óóó\company1"
$shares = Get-ChildItem -LiteralPath $rootPath -Recurse -Depth 3 -Directory -Force
$data = foreach($share in $shares){
    $currentFolderName = $share.Name
    $acls = Get-Acl -LiteralPath $share.FullName
    foreach($accessRule in $acls.Access){
        $accessRule | Add-Member -MemberType NoteProperty -Name 'FolderName' -Value $share.FullName -PassThru
    }
}
$data | Export-Csv "$env:USERPROFILE\Desktop\FolderPermissions.csv" -Encoding UTF8 -Delimiter ";" -NoTypeInformation

二、无法重命名目录的解决方法

同样因编码解析问题,使用-LiteralPath参数严格匹配路径执行重命名:

# 将带重音的目录名óóó修改为noaccents
Rename-Item -LiteralPath "\\server\folder1\óóó" -NewName "noaccents"

关键注意事项

  • 处理带特殊字符(重音、非ASCII)的路径时,始终使用-LiteralPath参数,避免PowerShell误解析路径。
  • 导出CSV时指定-Encoding UTF8,防止特殊字符乱码,同时添加-NoTypeInformation移除CSV头部的类型注释。
  • 若控制台编码异常,可手动设置编码:
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8

内容的提问来源于stack exchange,提问作者jonas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 15:40:05