首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell -设置文件夹权限

Powershell -设置文件夹权限
EN

Stack Overflow用户
提问于 2015-11-17 22:12:48
回答 1查看 145关注 0票数 2

嗨,我想给域名用户与PowerShell文件夹的完全访问权限。我正在使用这组代码,这些代码是通过Technet和博客研究收集的。我还是有一些问题。

当我运行下面的代码时,我得到了这个错误:

代码语言:javascript
复制
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

下面是我的代码:

代码语言:javascript
复制
$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 

我尝试将$user变量替换为"domain\Domain Users“,代码按预期工作。我一直不知道如何正确地传递$user变量,这样才能传递user参数,而不是硬编码。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2015-11-17 22:35:18

检查DomainName变量的值。这意味着$user变量包含类似MyDomain OtherDomain\Domain Users的内容。

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

代码语言:javascript
复制
$domainName = (gwmi Win32_NTDomain).DomainName
if ($domainName -is [array]) { $domainName = $domainName[0] }

注意:我的域名是按字母顺序排在第一位的,所以我不知道是先显示当前域名还是按字母顺序显示它们。您需要测试并找到适合您的解决方案。

编辑:看起来$domainName = $env:USERDOMAIN可能会做到这一点

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33759092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档