异常调用“ .ctor”并带有5个参数以及其他错误

时间:2019-07-23 19:33:34

标签: powershell

我正在创建用于自动化过程的脚本,并且遇到设置网络共享权限的问题。请查看下面的代码。

$Employee = Get-ADUser -Identity test_Person  | Select-Object -ExpandProperty SamAccountName

$Manager = Get-ADUser -Identity test_Person  | Select-Object -ExpandProperty Manager

$Drive = "\\Sharename\directory\"

$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”的异常:“值不能为null。   参数名称:identity   在线:5字符:7   + $ Ar =新对象System.Security.AccessControl.FileSystemAccessRule($ ...   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~       + CategoryInfo:InvalidOperation:(:) [New-Object],MethodInvocationException       + FullyQualifiedErrorId:ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

     

使用“ 1”作为参数调用“ SetAccessRule”的异常:“值不能为null。   参数名称:“规则”   在线:6字符:1   + $ ACL.SetAccessRule($ Ar)   + ~~~~~~~~~~~~~~~~~~~~~~~       + CategoryInfo:未指定:(:) [],MethodInvocationException       + FullyQualifiedErrorId:ArgumentNullException

     

Set-Acl:该进程不具有此操作所需的'SeSecurityPrivilege'特权。   在第7行:1个字符   + Set-Acl“ $ LDrive \ $($ Employee)” $ ACL   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~       + CategoryInfo:PermissionDenied:(\ Drive \ Directory \ Test_Person:String)[Set-Acl],PrivilegeNotHeldException       + FullyQualifiedErrorId:System.Security.AccessControl.PrivilegeNotHeldException,Microsoft.PowerShell.Commands.SetAclCommand

1 个答案:

答案 0 :(得分:0)

您必须更改处理返回的经理数据的方式。您必须先将Manager对象的SamAccountNameSecurityIdentitifer值传递给FileSystemAccessRule()

$Employee = Get-ADUser -Identity test_Person -Properties Manager
$Manager = Get-ADUser -Identity $Employee.Manager | Select-Object -ExpandProperty SamAccountName
$Drive = "\\Sharename\directory"
$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在默认情况下不会返回Manager属性。您必须将其包括在-Property Manager中。其次,manager属性返回manager对象的DN。 FileSystemAccessRule()接受从IdentityReferenceSamAccountName派生的SID对象,这意味着您必须为正确的数据格式执行转换或其他查找。