为什么驱动器根目录上的 Set-Acl 尝试设置“对象”的所有权?

发布于 2024-11-18 23:07:54 字数 1232 浏览 3 评论 0原文

我想更改 C: 驱动器的 ACL。我想做的是删除用户可以直接在驱动器上创建文件夹的权限。我在编写脚本时在另一个文件夹上测试了该脚本。它工作没有问题。完成后,我在实际驱动器的测试环境中尝试了该脚本。我收到一个我无法弄清楚的错误。如果我手动删除权限,它就可以正常工作。有人有主意吗?

$path = "C:\"

$colRights = [System.Security.AccessControl.FileSystemRights]"CreateDirectories"

$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::None 
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None 

$objType =[System.Security.AccessControl.AccessControlType]::Allow 
$objUser = New-Object System.Security.Principal.NTAccount("Authenticated Users") 
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule ($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType) 

$objACL = Get-ACL $path 
$objACL.RemoveAccessRule($objACE) 

Set-ACL $path $objACL

错误是:

Set-Acl : The security identifier is not allowed to be the owner of this object.
At C:\Users\mhodler\Desktop\Remove Permission.ps1:57 char:8
+ Set-ACL <<<<  $path $objACL
    + CategoryInfo          : InvalidOperation: (C:\:String) [Set-Acl], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.SetAclCommand

I would like to change the ACL of the C: drive. What im trying to do is remove the permission that a user can create a folder directly on the drive. I tested the script on another folder while writing it. It worked without a problem. After completion i tried the script in our test envoirnment on the actual drive. I get an error that i cant figure out. If i remove the permission manualy it works without a problem. Anyone got an idea?

$path = "C:\"

$colRights = [System.Security.AccessControl.FileSystemRights]"CreateDirectories"

$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::None 
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None 

$objType =[System.Security.AccessControl.AccessControlType]::Allow 
$objUser = New-Object System.Security.Principal.NTAccount("Authenticated Users") 
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule ($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType) 

$objACL = Get-ACL $path 
$objACL.RemoveAccessRule($objACE) 

Set-ACL $path $objACL

The error is:

Set-Acl : The security identifier is not allowed to be the owner of this object.
At C:\Users\mhodler\Desktop\Remove Permission.ps1:57 char:8
+ Set-ACL <<<<  $path $objACL
    + CategoryInfo          : InvalidOperation: (C:\:String) [Set-Acl], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.SetAclCommand

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜中书 2024-11-25 23:07:54

我找到了答案。微软说

不幸的是Get-Acl缺少一些功能。即使您只想修改 DACL,它也始终会读取完整的安全描述符。这就是为什么 Set-ACL 也想写入所有者,即使您没有更改它。使用 GetAccessControl 方法允许您指定要读取安全描述符的哪一部分。

Get-Acl 调用替换为

$acl = (Get-Item $path).GetAccessControl('Access')

I found the answer. Microsoft says

Unfortunately Get-Acl is missing some features. It always reads the full security descriptor even if you just want to modify the DACL. That’s why Set-ACL also wants to write the owner even if you have not changed it. Using the GetAccessControl method allows you to specify what part of the security descriptor you want to read.

Replace the Get-Acl call with

$acl = (Get-Item $path).GetAccessControl('Access')
流年里的时光 2024-11-25 23:07:54

您需要 SeRestorePrivilege 来设置所有者。我使用下面 URL 中的 Lee Holmes 脚本来通过此附加权限提升我的进程,并且能够将所有者设置为除我之外的其他人。

http://www.leeholmes.com/ blog/2010/09/24/ adjustment-token-privileges-in-powershell/

我尝试了 (get-item $path).getaccesscontrol("access") 方法,但仍然遇到相同的错误,因为我的进程没有 SeRestorePrivilege

You need the SeRestorePrivilege to set the owner. I used Lee Holmes' script from the URL below to elevate my process with this additional priv and was able to set the owner to someone other than myself.

http://www.leeholmes.com/blog/2010/09/24/adjusting-token-privileges-in-powershell/

I tried the (get-item $path).getaccesscontrol("access") method but still got the same error since my process didn't have the SeRestorePrivilege.

空宴 2024-11-25 23:07:54

下面的代码对我有用:

$ApplicationPoolIdentity = "everyone"

function SetACL()
{
    param (
        [Parameter(Mandatory=$true)]
        [string]        $Path 
    )

    $Acl = (Get-Item $Path).GetAccessControl('Access')
    Write-Host "Path:" $Path "ID:" $ApplicationPoolIdentity
    $Ar = New-Object  system.security.accesscontrol.filesystemaccessrule($ApplicationPoolIdentity,"Write","Allow")
    $Acl.SetAccessRule($Ar)
    Write-Host $Acl
    $Acl | Set-Acl $Path
}

SetACL "C:\Test\"

The below code works for me:

$ApplicationPoolIdentity = "everyone"

function SetACL()
{
    param (
        [Parameter(Mandatory=$true)]
        [string]        $Path 
    )

    $Acl = (Get-Item $Path).GetAccessControl('Access')
    Write-Host "Path:" $Path "ID:" $ApplicationPoolIdentity
    $Ar = New-Object  system.security.accesscontrol.filesystemaccessrule($ApplicationPoolIdentity,"Write","Allow")
    $Acl.SetAccessRule($Ar)
    Write-Host $Acl
    $Acl | Set-Acl $Path
}

SetACL "C:\Test\"
酷到爆炸 2024-11-25 23:07:54

人们可能会发现这更容易:

icacls c:\ /remove "authenticated users"

People may find this easier:

icacls c:\ /remove "authenticated users"
单身狗的梦 2024-11-25 23:07:54

$Acl = (Get-Item $Path).GetAccessControl('Access')

为我工作。
我从 CMD 运行我的 PS 脚本,在这个 PS 脚本中我运行另一个 PS 脚本,只要我用我自己的用户执行它,一切都会正常工作。当我使用不同的用户时,我收到相同的错误:
Set-Acl :安全标识符不允许成为该对象的所有者。

只需将 Get-ACL 更改为上面的那一行即可,效果很好。
再次感谢。

$Acl = (Get-Item $Path).GetAccessControl('Access')

Worked for me.
I run my PS Script from CMD and in this PS Script i run another PS Script everything works fine as long as i do it with my own User. When i use different User i get the same Error:
Set-Acl : The security identifier is not allowed to be the owner of this object.

Just changed Get-ACL to that Line above and it worked fine.
Thanks again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文