我正在使用以下数组将权限应用于目录,但出现错误:
新对象:找不到“ FileSystemAccessRule”的重载,并且参数计数为“ 1”。
$psarray = @(
('xppusers', 'D:\XPP\xz\bin', 'Read'),
('xppusers', 'D:\XPP\xz\sys\config', 'Read'),
('xppusers', 'D:\XPP\sd_liz', 'Read'),
('xppusers', 'D:\XPP\xz\help', 'Read')
)
foreach($item in $psarray)
{
$permission = "'" + $item[0] + "', '" + $item[2] + "', 'ContainerInherit, ObjectInherit', 'None', 'Allow'"
$rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $permission
$acl.SetAccessRule($rule)
$acl | Set-Acl -Path $item[1]
}
完整的错误是:
New-Object : Cannot find an overload for "FileSystemAccessRule" and the argument count: "1".
At C:\GPO-Installers\permissions-array.ps1:20 char:13
+ $rule = New-Object -TypeName System.Security.AccessControl.FileSy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
如果我手动创建$ permission,它会正常工作。
例如:
$permission = 'xppusers', 'read', 'ContainerInherit, ObjectInherit', 'None', 'Allow'
不确定我哪里出错了,将非常感谢您的帮助。
谢谢。
您误将单个字符串传递给System.Security.AccessControl.FileSystemAccessRule
构造函数(通过New-Object -ArgumentList
);该错误表明没有构造函数(重载)采用类型为[string]
的single参数。[1]
相反,您必须传递构造函数参数individuallyly,在New-Object
的上下文中,它表示为array(-Args
是-ArgumentList
的别名,您可以甚至省略-Args
):
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule `
-Args ($item + 'ContainerInherit, ObjectInherit', 'None', 'Allow')
由于$item
是包含前两个构造函数参数的数组,因此您可以简单地使用数组串联(+
)附加[2]不变参数。
[1]错误消息在某种程度上可能会引起误解,因为它没有明确指出它不是重要的参数的only,也不是参数的data]类型。例如,如果有一个单参数构造函数,您会看到相同的消息,但是它的参数类型是[[other而不是[string]
。[2]从技术上讲,将创建一个new数组,该数组是来自输入数组的元素的串联。