powershell - 使用 Powershell 对目录进行 GetAccessControl() 不返回任何数据

标签 powershell acl directory powershell-1.0

我有一个目录,其名称包含方括号,这使得在 Powershell 中处理它有点问题。我看到建议,要获取目录的 ACL,您应该使用 DirectoryInfoGetAccessControl() 方法,而不是使用 cmdlet Get-Acl.

不幸的是,使用GetAccessControl()不会返回任何目录的任何结果。请参阅下面我的脚本。

$filename = "C:\somefolder"
$di = get-item $filename
$di | get-acl    # this returns the correct ACL data as expected
$di.GetAccessControl()   # this shows an empty ACL (see below output)

空 ACL 的意思是这样的:

Path      Owner      Access
----      -----      ------

我发现获取目录名包含方括号的 $di 实例的唯一方法是使用双反引号转义括号

$filename = "C:\some``[folder``]withsquarebrackets"

如果我这样做,然后执行 $di = get-item $filename$di 显示它包含指定文件夹的详细信息。但在这种情况下,其余两行返回不同的结果:

$di | get-acl     # does nothing, doesn't even show an empty ACL
$di.GetAccessControl()   # shows an empty ACL as seen above

谁能解释一下如何可靠地获取文件夹枚举的 ACL,其中一些文件夹的名称中包含方括号?

更新2012-02-23 16:31Z 在 Andy Arismendi 报告该方法对他有效后,我确定 GetAccessControl() 似乎在 Powershell 2 上工作正常。受影响的计算机运行 Powershell 1。有什么想法如何使用 Powershell 1 解决此问题吗?

最佳答案

方括号问题是因为方括号是“通配符”通配符。您可以使用 -literalpath 参数来避免所有转义:

$di = get-item -literalpath C:\some[folder]withsquarebrackets
$di.getaccesscontrol()

不幸的是,它们没有包含 get-acl 的 -literpath 参数,因此您只能执行 get-item,然后使用该方法的 getaccesscontrol() 方法来获取访问列表。 Get-Acl 在 V3 中确实有一个 -literalpath 参数。

关于powershell - 使用 Powershell 对目录进行 GetAccessControl() 不返回任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9416680/

相关文章:

powershell - 如何在PowerShell中的注册表项上使用getauditrules()获取ACL对象中的审核规则?

c# - "Move will not work across volumes"- 为什么?以及如何克服?

Powershell:将 XmlDocument 转换为 XmlNode

powershell - 在构建过程中重启 Docker 容器

powershell - 在任务计划程序中传递Powershell参数

Python - 连接多个子文件夹中的文本文件

windows-7 - Windows 7 在 "Program Files"中创建文件夹在 C# 代码中失败,即使我有管理员权限!

powershell - 命令行中的 "@"是什么意思?

ios - 解析具有角色的用户

parameters - 将参数传递给sails.js 策略