嗨,我想授予域用户使用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变量,以便可以传递用户参数,而不是对其进行硬编码。
谢谢
最佳答案
检查您的DomainName变量的值。如果在此处运行相同的代码,则不仅会得到我正在运行的域,而且还会得到所有受信任的域作为数组。这意味着$ user变量包含类似MyDomain OtherDomain\Domain Users
的内容。
如果可能,我建议将域声明为$domainName = "MyDomain"
。如果确实需要动态获取域,则需要测试数组;
$domainName = (gwmi Win32_NTDomain).DomainName
if ($domainName -is [array]) { $domainName = $domainName[0] }
注意:我的域是按字母顺序优先显示的,因此我不知道当前域是先显示还是按字母顺序显示。您需要测试并找到适合您的解决方案。
编辑:看起来
$domainName = $env:USERDOMAIN
可能会解决问题
关于powershell - Powershell-设置文件夹权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33759092/