当文件名包含括号时,如何使用 PowerShell 的 get-acl cmdlet?
假设我有一个名为“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
没有这样的开关。这似乎是一个已知问题。
根据该报告中的观察,我能想到的唯一解决方法是以这种有点尴尬的方式使用 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
。因为我不知道通配符会出现在哪里,所以我必须编写代码来转义它们(并将路径转换为 UNC 格式)。这有点笨拙。有没有更简单的方法?顺便说一句,我正在使用 PowerShell v1.0。
意见:也许这是 PowerShell 设计中进行权衡的不可避免的结果,但不幸的是,每个命令都需要一个忽略通配符的选项,而不是在 shell 本身中具有该功能,以便所有命令自动受益。
Suppose I have a file named "test[1].txt". Both of these commands produce no output:
PS C:\Temp> dir test[1].txt
PS C:\Temp> get-acl test[1].txt
PS C:\Temp>
The good news is that the dir
command has a -LiteralPath
switch that tells it not to interpret any characters as wildcards:
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
The bad news is that get-acl
has no such switch. This appears to be a known issue.
The only workaround I could come up with, following an observation in that report, is to use UNC paths in this somewhat awkward way:
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
I'm using get-acl in a pipeline fed by dir -rec -name
. Because I don't know where the wildcards will appear, I'll have to write code to escape them (and convert the paths to UNC format). It's a bit kludgy. Is there a simpler way? I'm using PowerShell v1.0, by the way.
Opinion: Perhaps this is an unavoidable consequence of trade-offs made in the design of PowerShell, but it's unfortunate that every command needs an option to ignore wildcards, rather than having that functionality in the shell itself so that all commands automatically benefit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题时不时地困扰着我,当你点击一个没有 -LiteralPath 支持的 cmdlet 时,它会很糟糕。在这种情况下,另一种解决方法是使用文件的短名称。 PowerShell 社区扩展 通过 Get-ShortPath cmdlet 支持此功能,例如:
This issue has bit me from time to time and it sucks when you hit a cmdlet without -LiteralPath support. In this case, another work-around is to use the short name of the file. The PowerShell Community Extensions supports this with the Get-ShortPath cmdlet e.g.: