异常调用".ctor",其中包含 5 个参数以及其他错误



我正在创建一个脚本来自动化流程,但在网络共享上设置权限时遇到了问题。请查看下面的代码。

$Employee = Get-ADUser -Identity test_Person  | Select-Object -ExpandProperty SamAccountName
$Manager = Get-ADUser -Identity test_Person  | Select-Object -ExpandProperty Manager
$Drive = "\Sharenamedirectory"
$ACL = Get-Acl "$Drive$($Employee)"
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule($Manager, "FullControl", "containerInherit,ObjectInherit", "None", "Allow")
$ACL.SetAccessRule($Ar)
Set-Acl "$Drive$($Employee)" $ACL

以下是错误。任何帮助都非常感谢

新对象:使用"5"参数调用".ctor"的异常:"值不能为空。 参数名称:标识" 行:5 字符:7 + $Ar = new-Object System.Security.AccessControl.FileSystemAccessRule($ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 类别信息 : 无效操作: (:) [新对象], 方法调用异常 + FullQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

使用"1"参数调用"SetAccessRule"的异常:"值不能为空。 参数名称:规则" 行:6 字符:1 + $ACL。设置访问规则($Ar) + ~~~~~~~~~~~~~~~~~~~~~~~ + 类别信息:未指定:(:) [],方法调用异常 + 完全限定错误 ID : 参数空异常

Set-ACL :进程不具有此操作所需的"SeSecurityPrivilege"权限。 行:7 字符:1 + 设置-ACL "$LDrive\$($Employee)" $ACL + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (\Drive\Directory\Test_Person:String) [Set-ACL], PrivilegeNotHeldException + FullQualifiedErrorId : System.Security.AccessControl.PrivilegeNotHeldException,Microsoft.PowerShell.Command.SetAclCommand

您必须更改处理返回的管理器数据的方式。在将管理器对象传递到FileSystemAccessRule()之前,必须查找管理器对象的SamAccountNameSecurityIdentitifer值。

$Employee = Get-ADUser -Identity test_Person -Properties Manager
$Manager = Get-ADUser -Identity $Employee.Manager | Select-Object -ExpandProperty SamAccountName
$Drive = "\Sharenamedirectory"
$ACL = Get-Acl "$Drive$($Employee.SamAccountName)"
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule($Manager, "FullControl", "containerInherit,ObjectInherit", "None", "Allow")
$ACL.SetAccessRule($Ar)
Set-Acl "$Drive$($Employee.SamAccountName)" $ACL

在您尝试时,$Manager为空,因为默认情况下Get-ADUser不返回管理器属性。您必须将其包含在-Property Manager.其次,管理器属性返回管理器对象的 DN。FileSystemAccessRule()接受从SamAccountNameSID派生的IdentityReference对象,这意味着您必须执行转换或其他查找以查找正确的数据格式。

最新更新