【问题标题】:Get-ACL -Literalpath throws: illegal character in pathGet-ACL -Literalpath 抛出:路径中的非法字符
【发布时间】:2026-01-15 07:35:01
【问题描述】:

我有一个包含所有要关闭 ACL 的文件夹和文件的数组列表。 一切都在我的 Foreach 循环中运行良好。

但出于一个奇怪的原因,它会抛出:

Get-Acl : 非法字符 + CategoryInfo : NotSpecified: (:) [Get-Acl], ArgumentException + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.GetAclCommand

当我使用“错误”路径运行 get-acl 手册时,我得到了 ACL。 我的代码是:

               $ACL = get-acl -LiteralPath "$Path" | select -ExpandProperty access | select IdentityReference
               $Result = Compare-Object -ReferenceObject $ACLListAccess -DifferenceObject $ACL -Property Access -PassThru
                   if ($Result.count -ne 0)
                   {
                   $ExplicitTest = get-acl -LiteralPath "$Path" | select -ExpandProperty access 
                   if ($ExplicitTest.IsInherited.count -ne $ACLListAccessFull.count -and $ExplicitTest.IsInherited -Like "*False*")
                   {
                   $WrongFolders.Add($Path) | Out-Null
                   }
                   }

带有“非法字符”的路径是 \mycompany.com\folders\Algemeen\Reme§ysen

【问题讨论】:

  • 检查 $Path 在循环中实际扩展为什么。
  • 当我写主机时它是正确的。它看起来像在变量本身中以其他方式处理?

标签: powershell acl


【解决方案1】:

抱歉,无法重现您的问题,并且评论不允许我粘贴此内容

D:\temp\a> get-acl .\aaa§bbb
    Directory: D:\temp\a
Path                                                         Owner                                                        Access                                                      
----                                                         -----                                                        ------                                                      
aaa§bbb                                                      adil                                               BUILTIN\Administrators Allow  FullControl...

如果你复制粘贴那个字符,你也会得到 167 吗?

C:\> [int][char]'§'
167

【讨论】:

  • 是的,它是一回事。当我在另一台计算机上运行它时,它就像一个魅力。当我做 $Path |输出文件我在计算机上出现错误,然后使用不同的字符 §。 powershell 中的 $Outputencoding 是一样的。当我在这台机器上运行 chcp 时,它与在我的另一台机器上不同。当我运行 chcp 850 (这是它工作的机器上的值)时,问题仍然存在。