Powershell - 如何完全访问文件夹的本地用户列表

时间:2015-10-20 10:20:24

标签: powershell permissions directory acl

我有一个CSV文件,其中包含文件夹名称(前6个字符)和用户名(其余字符)。 我必须为他的文件夹中的每个用户提供完全控制访问权限。所以我写了:

$Doc = import-csv "C:\Temp\ListOfUsers.csv"

foreach ($x in $Doc)
  {

     $x = ""+ $x

     $CPayID = $x.SubString(10,6)

     $UserName = $x.SubString(17, $x.Length-18)

     $UserPath = "C:\XPAY_FTP_CUST\"+$CPayID

     $Acl = Get-Acl $UserPath

     $Rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$Username","FullControl","ContainerInherit, ObjectInherit","None","Allow")

     $Acl.SetAccessRule($Rule)

     Set-Acl $UserPath $Acl
  }

但是我收到了每个用户的以下错误:

  

使用“1”参数调用“SetAccessRule”的异常:“无法翻译部分或全部身份引用。”

用户存在并且是本地人,我是本地管理员,当我要求回复$Username$UserPath$Acl我收到正确的数据。 拜托,我需要任何帮助。

2 个答案:

答案 0 :(得分:0)

您可能必须将用户名指定为DOMAIN\user。如果是本地用户,DOMAIN将是计算机名称:

$UserReference = New-Object System.Security.Principal.NTAccount $env:ComputerName,$Username
$Rule = New-Object System.Security.AccessControl.FileSystemAccessRule($UserReference,"FullControl","ContainerInherit, ObjectInherit","None","Allow")

答案 1 :(得分:-1)

Mathias比我更聪明,但我在你的代码中注意到了这一点: ($UserReference,"FullControl","ContainerInherit, ObjectInherit","None","Allow")

"ContainerInherit, ObjectInherit"应该是"ContainerInherit", "ObjectInherit"吗?

相关问题