首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Powershell Windows ACL

Powershell Windows ACL
EN

Stack Overflow用户
提问于 2021-08-31 08:05:38
回答 1查看 88关注 0票数 0

我们正在运行下面提到的脚本来更改一堆ACL权限,当我们从一个环境迁移到另一个环境时,这些权限需要细化到文件级。

下面的脚本对文件夹/子文件夹有效,但当涉及到实际文件本身时似乎失败了。

代码语言:javascript
运行
复制
$items = get-childitem \\file.location.com.au\project\people\user1 -recurse | select-object -property fullname

Foreach ($item in $items) {
# Get the ACL for an existing folder
$existingAcl = Get-Acl -Path '$item'

# Set the permissions that you want to apply to the folder
$permissions = 'SERVER\USER1', 'Read,Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow'

# Create a new FileSystemAccessRule object
$rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $permissions

# Modify the existing ACL to include the new rule
$existingAcl.SetAccessRule($rule)

# Apply the modified access rule to the folder
$existingAcl | Set-Acl -Path '$ITEM'
}

正如你所看到的,我们得到了下面的错误,我不确定为什么。有没有人能看到我遗漏了什么?

我花了很多时间来纠正这个问题,但没有取得任何进展。

代码语言:javascript
运行
复制
At line:14 char:1
+ $existingAcl.SetAccessRule($rule)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Get-Acl : Cannot find path '$item' because it does not exist.
At line:5 char:16
+ $existingAcl = Get-Acl -Path '$item'
+                ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Get-Acl], ItemNotFoundException
    + FullyQualifiedErrorId : GetAcl_PathNotFound_Exception,Microsoft.PowerShell.Commands.GetAcl
   Command

You cannot call a method on a null-valued expression.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-31 11:16:24

这应该会让你走上正确的道路:

代码语言:javascript
运行
复制
$items = get-childitem \\file.location.com.au\project\people\user1 -recurse | select-object -property fullname
# Set the permissions that you want to apply to the folder
$permissions = 'SERVER\User1', 'Read,Modify', 'Allow'

# Create a new FileSystemAccessRule object
$newaccessrule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $permissions


Foreach ($item in $items) {
# Get the ACL for an existing folder
$existingAcl = Get-Acl -Path $item.FullName

# Modify the existing ACL to include the new rule
$existingAcl.SetAccessRule($newaccessrule)
$existingAcl.SetAccessRuleProtection($false,$true)

# Apply the modified access rule to the folder
Set-Acl -Path $item.FullName -AclObject $existingAcl
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68995030

复制
相关文章

相似问题

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