Powershell ACL AddAccessRule

问题描述 投票:-2回答:1

我目前正在尝试使用PowerShell设置权限。

这是我的代码:

    $Rights = [System.Security.AccessControl.FileSystemRights] "DeleteSubdirectoriesAndFiles, Write, ReadAndExecute, Synchronize"
    $Access=[System.Security.AccessControl.AccessControlType]::Allow
    $Inherit=[System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags]::ObjectInherit
    $Prop=[System.Security.AccessControl.PropagationFlags]::None

    $DirPath = "\\CENSOREDDIR" + $ComboBox1.SelectedItem + "\" + $TextBox1.Text
    Write-Host $DirPath
    New-Item $DirPath -ItemType Directory

    $GroupPath = $GruppenHT[$ComboBox1.SelectedItem]
    $GroupPathFull = "RES-" + $GroupPath + "-L-C"
    Write-Host "Group:"$GroupPathFull

    $AccessRule = new-object System.Security.AccessControl.FileSystemAccessRule $GroupPathFull,$Rights,$Inherit,$Prop,$Access
    Write-Host "AccessRule:"$AccessRule

    $ACL = Get-ACL $DirPath
    Write-Host "DIR:"$DirPath


    $ACL.AddAccessRule($AccessRule)
    Write-Host "Permissions:"$ACL.Access

这是调试输出的样子:

Group: RES-CENSOREDGROUP-L-C <-- CORRECT

AccessRule: System.Security.AccessControl.FileSystemAccessRule

DIR: \\CENSOREDDIR\test <-- CORRECT

Permissions: System.Security.AccessControl.FileSystemAccessRule System.Security.AccessControl.FileSystemAccessRule System.Security.AccessControl.FileSystemAccessRule S
ystem.Security.AccessControl.FileSystemAccessRule System.Security.AccessControl.FileSystemAccessRule System.Security.AccessControl.FileSystemAccessRule System.Security
.AccessControl.FileSystemAccessRule System.Security.AccessControl.FileSystemAccessRule 

但是什么都没有发生。没有显示错误,但是该权限不在目录中。

我也发现脚本执行后变量不再可用有点奇怪。在PowerShell ISE中实际上总是这样吗?

是因为整个事件都在“ Button_Click”事件中吗?

也许有人知道该怎么办。

powershell acl access-control
1个回答
0
投票

您仅更改对象$ACL。您必须使用$DirPath将其应用于Set-Acl

Set-Acl $DirPath $ACL

© www.soinside.com 2019 - 2024. All rights reserved.