Powershell-设置文件夹权限

Powershell-设置文件夹权限,powershell,Powershell,您好,我想使用PowerShell为域用户提供对文件夹的完全访问权限。我正在使用从使用Technet和博客的研究中收集的这组代码。我仍然有问题 当我运行下面的代码时,出现此错误: Exception calling "SetAccessRule" with "1" argument(s): "Some or all identity references could not be translated." At line:13 char:1 + $acl.SetAccessRule($a

您好,我想使用PowerShell为域用户提供对文件夹的完全访问权限。我正在使用从使用Technet和博客的研究中收集的这组代码。我仍然有问题

当我运行下面的代码时,出现此错误:

Exception calling "SetAccessRule" with "1" argument(s): "Some or all     identity references could not be translated."
At line:13 char:1
+ $acl.SetAccessRule($accessRule)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IdentityNotMappedException
这是我的密码:

$directory = "C:\inetpub\wwwroot\app.webservice"
$domainName = (gwmi Win32_NTDomain).DomainName
$group = 'Domain Users'
$inherit = [system.security.accesscontrol.InheritanceFlags]"ContainerInherit, ObjectInherit"
$propagation = [system.security.accesscontrol.PropagationFlags]"None"
$acl = (Get-Item $directory).GetAccessControl("Access")
$user = "{0}\{1}" -f "$domainName", $group
$user.trim()
$access = "FullControl"
$accessType = "Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule -ArgumentList @("$user","$access", "$inherit", "$propagation", "$accessType")
$acl.SetAccessRule($accessRule)
set-acl $directory $acl 
我尝试用“domain\domain Users”替换$user变量,代码按预期工作。我一直无法弄清楚如何正确地传递$user变量,以便传递用户参数而不是硬编码


谢谢

检查域名变量的值。如果我在这里运行相同的代码,我不仅会得到我正在运行的域,而且会得到作为一个数组的所有受信任域。这意味着$user变量包含类似于
MyDomain OtherDomain\Domain Users
的内容

如果可能,我建议将域声明为
$domainName=“MyDomain”
。如果您确实需要动态获取域,则需要测试数组

$domainName = (gwmi Win32_NTDomain).DomainName
if ($domainName -is [array]) { $domainName = $domainName[0] }
注意:我的域名是按字母顺序排列的,所以我不知道当前域名是先显示的还是按字母顺序显示的。您需要测试并找到适合您的解决方案


编辑:看起来
$domainName=$env:USERDOMAIN
可能会起作用

我一定会选择
$env:USERDOMAIN
(用户)或
(gwmi Win32_ComputerSystem)。Domain
(机器)而不是
Win32_NTDomain
谢谢你。这段代码也适用于我需要的东西。