拒絕資料夾和檔案的權限

拒絕資料夾和檔案的權限

我需要拒絕 C:\Windows\Installer 資料夾/檔案的群組

我嘗試使用以下內容,但它僅適用於資料夾而不是所有文件,它適用於某些文件

$path = "C:\Windows\Installer"
 $acl = Get-Acl $path
 $AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("YourUsers","FullControl", "ContainerInherit,ObjectInherit", "None", "Deny")
 $acl.AddAccessRule($AccessRule)
 Set-Acl -AclObject $acl -Path $path

我的劇本中遺漏了什麼嗎

編輯:檢查我可以刪除的檔案的權限後,看起來繼承已被停用

如何檢查繼承是否已停用以及如果停用則啟用它

我需要將其應用於 5000 台機器,這就是我透過腳本執行此操作的原因

答案1

您僅將 ACL 變更套用至使用 指定的資料夾$path,若要將 ACL 套用至資料夾中的所有內容,您需要遞歸地列出每個項目:

$path = "C:\Windows\Installer"
$items = Get-ChildItem -Recurse $path

# Enables inheritance on all items in folder
foreach ($item in $items)
{
  $acl = Get-acl $item.FullName
  $acl.SetAccessRuleProtection($false,$true)
  Set-Acl -AclObject -Path $item.FullName
}

# Sets file / folder permission to desired state
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("YourUsers","FullControl", "ContainerInherit,ObjectInherit", "None", "Deny")
foreach ($item in $items)
{
  $acl = Get-acl $item.FullName
  # Compare item's folder permission against root folder, if different apply ACL
  if ((Compare-Object -ReferenceObject (Get-ACL $path) -DifferentObject (Get-ACL $item) -Property access) -ne $null)
  {
    $acl.AddAccessRule($AccessRule)
    Set-Acl -AclObject $item.FullName
  }
}
  

相關內容