2010-07-19 27 views
32

Estoy tratando de imitar la acción de hacer clic derecho en una carpeta, configurar "modificar" en una carpeta, y que los permisos se apliquen a la carpeta específica y subcarpetas y archivos .Configuración de indicadores de herencia y propagación con set-acl y powershell

Principalmente estoy usando Powershell, sin embargo, la herencia solo se establece como "subcarpetas y archivos" en lugar de como "esta carpeta, subcarpetas y archivos".

¿Hay algún indicador no listado para System.Security.AccessControl.PropagationFlags que establecerá esto correctamente?

Esto es lo que estoy trabajando hasta ahora.

$Folders = Get-childItem c:\TEMP\ 
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags]::ObjectInherit 
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::InheritOnly 
$objType = [System.Security.AccessControl.AccessControlType]::Allow 

foreach ($TempFolder in $Folders) 
{ 
echo "Loop Iteration" 
$Folder = $TempFolder.FullName 

$acl = Get-Acl $Folder 
$permission = "domain\user","Modify", $InheritanceFlag, $PropagationFlag, $objType 
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission 

$acl.SetAccessRule($accessRule) 
Set-Acl $Folder $acl 
} 
+9

Hice un cuadro de la correspondencia entre los diálogos de permisos de archivos y los permisos resultantes: http://bit.ly/inheritMatrix –

+0

Agregue la modificación desde el código siguiente para que esto funcione – riahc3

Respuesta

23

creo que su respuesta se puede encontrar en this page. Desde la página:

esta carpeta, subcarpetas y archivos:

InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit 
PropagationFlags.None 
+1

Gracias - funciona a la perfección ! –

+1

¿Podría agregar el código? Vincular a una página y citar IMO no es una respuesta adecuada. – riahc3

8

El hecho de que estás en PowerShell no se olvidó de buen ol ex. A veces pueden proporcionar la solución más fácil ej .:

icacls.exe $folder /grant 'domain\user:(OI)(CI)(M)' 
+0

Sí, casi resolvió los problemas con un archivo por lotes DOS y icacls o setacl, pero tratando de aprender powershell ... la mejor forma de aprender es resolviendo un problema con él, etc. –

+1

Entiendo. OTOH He estado usando PowerShell durante> 5 años y no dudo en regresar a un EXE si es significativamente más fácil que el equivalente de PowerShell. IOW hay mucho que aprender en PowerShell, algunos más valiosos que otros. :-) –

47

Aquí hay una tabla para ayudar a encontrar las banderas necesarias para diferentes combinaciones de permisos.

 ╔═════════════╦═════════════╦═══════════════════════════════╦════════════════════════╦══════════════════╦═══════════════════════╦═════════════╦═════════════╗ 
    ║    ║ folder only ║ folder, sub-folders and files ║ folder and sub-folders ║ folder and files ║ sub-folders and files ║ sub-folders ║ files ║ 
    ╠═════════════╬═════════════╬═══════════════════════════════╬════════════════════════╬══════════════════╬═══════════════════════╬═════════════╬═════════════╣ 
    ║ Propagation ║ none  ║ none       ║ none     ║ none    ║ InheritOnly   ║ InheritOnly ║ InheritOnly ║ 
    ║ Inheritance ║ none  ║ Container|Object    ║ Container    ║ Object   ║ Container|Object  ║ Container ║ Object  ║ 
    ╚═════════════╩═════════════╩═══════════════════════════════╩════════════════════════╩══════════════════╩═══════════════════════╩═════════════╩═════════════╝ 

Así que, como dijo David, querrá

 
    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit 
    PropagationFlags.None 

+0

muy agradable ... gracias. – nawfal

+4

+1 para la tabla. Daría un +1 adicional solo para los personajes de dibujo de cajas, si tan solo pudiera :) – Timwi

0

Aquí está the MSDN page describir las banderas y lo que es el resultado de sus diversas combinaciones.

Flag combinations => Propagation results 
========================================= 
No Flags => Target folder. 
ObjectInherit => Target folder, child object (file), grandchild object (file). 
ObjectInherit and NoPropagateInherit => Target folder, child object (file). 
ObjectInherit and InheritOnly => Child object (file), grandchild object (file). 
ObjectInherit, InheritOnly, and NoPropagateInherit => Child object (file). 
ContainerInherit => Target folder, child folder, grandchild folder. 
ContainerInherit, and NoPropagateInherit => Target folder, child folder. 
ContainerInherit, and InheritOnly => Child folder, grandchild folder. 
ContainerInherit, InheritOnly, and NoPropagateInherit => Child folder. 
ContainerInherit, and ObjectInherit => Target folder, child folder, child object (file), grandchild folder, grandchild object (file). 
ContainerInherit, ObjectInherit, and NoPropagateInherit => Target folder, child folder, child object (file). 
ContainerInherit, ObjectInherit, and InheritOnly => Child folder, child object (file), grandchild folder, grandchild object (file). 
ContainerInherit, ObjectInherit, NoPropagateInherit, InheritOnly => Child folder, child object (file). 

tenerlo aplicar los permisos para el directorio, así como todos los directorios y archivos de los niños de forma recursiva, tendrá que utilizar estas banderas:

InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit 
PropagationFlags.None 

lo tanto, el cambio de código específico que necesita para que por su ejemplo es:

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

Aquí hay un código de Powershell sucinta de aplicar nuevos permisos a una carpeta mediante la modificación está vigente ACL (Access control List).

# Get the ACL for an existing folder 
$existingAcl = Get-Acl -Path 'C:\DemoFolder' 

# Set the permissions that you want to apply to the folder 
$permissions = $env:username, '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 'C:\DemoFolder' 

Cada uno de los valores de la $permissions lista de variables se refieren a los parámetros de this constructor para la clase FileSystemAccessRule.

Cortesía de this page.