当文件名包含括号时,如何使用PowerShell的get-acl cmdlet?

时间:2010-02-25 15:34:08

标签: powershell acl

假设我有一个名为“test [1] .txt”的文件。这两个命令都不产生输出:

PS C:\Temp> dir test[1].txt
PS C:\Temp> get-acl test[1].txt
PS C:\Temp>


好消息是dir命令有一个-LiteralPath开关,告诉它不要将任何字符解释为通配符:

PS C:\Temp> dir -LiteralPath test[1].txt


    Directory: Microsoft.PowerShell.Core\FileSystem::C:\Temp


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         2/25/2010   9:57 AM          5 test[1].txt


坏消息是get-acl没有这样的转换。这似乎是一个已知的issue

根据该报告中的观察,我能想出的唯一解决方法是以这种有点尴尬的方式使用UNC路径:

PS C:\Temp> get-acl \\mymachine\C$\temp\test[[]1[]].txt


    Directory: Microsoft.PowerShell.Core\FileSystem::\\mymachine\C$\temp


Path                                    Owner
----                                    -----
test[1].txt                             MYDOMAIN\myname

我在由dir -rec -name提供的管道中使用get-acl。因为我不知道通配符出现在哪里,所以我必须编写代码来逃避它们(并将路径转换为UNC格式)。这有点笨拙。有更简单的方法吗?顺便说一下,我正在使用PowerShell v1.0。

意见:也许这是在PowerShell设计中做出权衡的不可避免的后果,但不幸的是每个命令都需要一个选项来忽略通配符,而不是在shell本身具有该功能,以便所有命令自动受益

1 个答案:

答案 0 :(得分:0)

这个问题不时让我感到困惑,当你点击没有-LiteralPath支持的cmdlet时,它很糟糕。在这种情况下,另一种解决方法是使用文件的短名称。 PowerShell Community Extensions使用Get-ShortPath cmdlet支持此功能,例如:

PS> Get-ShortPath '.\foo`[bar`].txt' | Get-Acl -Path {$_}


    Directory: Microsoft.PowerShell.Core\FileSystem::C:\User
    s\Keith


Path                 Owner               Access
----                 -----               ------
FOO_BA~1.TXT         Keith-Laptop-PC\... NT AUTHORITY\SYS...