Appliquer les ACL
Introduction
Nous reprenons une nouvelle fois notre fichier CSV :
NIVEAU 1;NIVEAU FolderPath2;NIVEAU GG_RW3;NIVEAU GG_RO4;NIVEAU GDL_RW5;NIVEAU GDL_RO6;NIVEAU 7;NIVEAU 8;FolderPath;CREER_GROUPES;GG_ECRITURE;GG_LECTURE;GDL_ECRITURE;GDL_LECTURE;GDL_VUE;Parent_GDL
01-DIRECTION;;;;;;;;01-DIRECTION;;ORG_GG_DIRECTION;;GDL_01_ECRITURE;GDL_01_LECTURE;GDL_01_VUE;
02-FINANCE;;;;;;;;02-FINANCE;;ORG_GG_FINANCE;ORG_GG_DIRECTION;GDL_02_ECRITURE;GDL_02_LECTURE;GDL_02_VUE;
00-Commun02-FINANCE;01-BUDGET;;;;;;;02-FINANCE\01-BUDGET;;ORG_GG_FINANCE;ORG_GG_DIRECTION;GDL_02-01_ECRITURE;GDL_02-01_LECTURE;GDL_02-01_VUE;GDL_02_VUE
02-FINANCE;02-FACTURES;;;;;;;02-FINANCE\02-FACTURES;;ORG_GG_FINANCE;ORG_GG_DIRECTION;GDL_02-02_ECRITURE;GDL_02-02_LECTURE;GDL_02-02_VUE;GDL_02_VUE
03-RH;;;;;;;;03-RH;;ORG_GG_RH;ORG_GG_DIRECTION;GDL_03_ECRITURE;GDL_03_LECTURE;GDL_03_VUE;
Tous les utilisateurs
GDL_00_RW
GDL_00_RO
00-Commun\03-RH;01-Scan
Tous les utilisateurs
GDL_00-01_RW
GDL_00-01_RO
GDL_00_RO
00-Commun\02-Sport
GG_Sport
GDL_00-02_RW
GDL_00-02_RO
GDL_00_RO
01-RH
GG_RH, GG_Direction
GDL_01_RW
GDL_01_RO
01-CONTRATS;;;;;;;03-RH\01-PaieCONTRATS;;ORG_GG_RH;ORG_GG_DIRECTION;GDL_03-01_ECRITURE;GDL_03-01_LECTURE;GDL_03-01_VUE;GDL_03_VUE
03-RH;02-PAIE;;;;;;;03-RH;NON;;;;;;
04-IT;;;;;;;;04-IT;;ORG_GG_IT;ORG_GG_DIRECTION;GDL_04_ECRITURE;GDL_04_LECTURE;GDL_04_VUE;
04-IT;01-INFRA;;;;;;;04-IT\01-INFRA;;ORG_GG_IT;ORG_GG_DIRECTION;GDL_04-01_ECRITURE;GDL_04-01_LECTURE;GDL_04-01_VUE;GDL_04_VUE
04-IT;02-PROJETS;;;;;;;04-IT\02-PROJETS;;ORG_GG_IT;ORG_GG_DIRECTION;GDL_04-02_ECRITURE;GDL_04-02_LECTURE;GDL_04-02_VUE;GDL_04_VUE
GDL_01-01_RW04-IT;02-PROJETS;2026;;;;;;04-IT\02-PROJETS\2026;NON;;;;;;
GDL_01-01_RO
Application des droits
Voici le script qui permet d'appliquer les droits à chaque GDL homonymique de son répertoire, ainsi que l'héritage des groupes RW à l'ensemble des enfants.
Encore une fois, le script ne ce sert que des colonnes utiles dans le cas présent : FolderPath, GDL_RW et GDL_RO :
<#
===============================================================================
SCRIPT : 4.1 - Appliquer_ACL.ps1
OBJET : Appliquer les ACL NTFS tri-niveaux (ECRITURE / LECTURE / VUE)
===============================================================================
DESCRIPTION :
Pour chaque dossier present dans le CSV :
* Desactiver l'heritage NTFS
* Supprimer toutes les ACL existantes
* Appliquer les droits :
- GDL_ECRITURE : Modify (herite)
- GDL_LECTURE : ReadAndExecute (herite)
- GDL_VUE : ReadAndExecute (non herite)
- Administrateurs : Modify (herite)
- Admins du domaine : Modify (herite)
- SYSTEM : Modify (herite)
REMARQUE :
- Le script suppose que 2.1 et 3.1 ont deja genere les GDL
et les chainages Parent_GDL.
===============================================================================
#>
Param(
[string]$RootPath = "C:\Chemin\Destination\Arbo"\SRV-DATAS\datas\",
[string]$CsvPath = "C:\Chemin\vers\fichier.AGDLP\ORG_ARBO_GDL.csv"
)
$ErrorActionPreference = "Stop"
# Vérifier----------------------------------------------------------------------------
# 1. Verification des droits administratifs
ifadministrateur (indispensable pour Set-Acl)
# -not---------------------------------------------------------------------------
([$principal = New-Object Security.Principal.WindowsPrincipal] WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()).
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Warning "LeCe script doit êtreetre lancélance en tant qu'qu administrateur."
exit 1
}
# ----------------------------------------------------------------------------
# 2. Lecture du CSV
avec# séparateur----------------------------------------------------------------------------
;if et(-not UTF8(Test-Path $GDLMappingCsvPath)) {
Write-Error "CSV introuvable : $CsvPath"
exit 1
}
$rows = Import-Csv -Path $CsvPath -Delimiter '";'" -Encoding UTF8
$total = $rows.Count
$index = 0
Write-Host "Application des ACL NTFS a partir du fichier : $CsvPath"
Write-Host "Racine de donnees : $RootPath"
Write-Host ""
# ----------------------------------------------------------------------------
# 3. Comptes supplémentairesadministratifs àayant RWtoujours Modify (herite)
# ----------------------------------------------------------------------------
$RWAdmins = @(
"Administrateurs",
"Admins du domaine",
"Administrateur-lcc",
"SYSTEM"
)
# ----------------------------------------------------------------------------
# 4. Traitement principal
# ----------------------------------------------------------------------------
foreach ($entry in $GDLMapping)rows) {
$index++
# Mise a jour de la barre de progression
Write-Progress -Activity "Application ACL" `
-Status "$index / $total" `
-PercentComplete (($index / $total) * 100)
# Verifier la presence de FolderPath
if ([string]::IsNullOrWhiteSpace($entry.FolderPath)) { continue }
# Construit le chemin complet
$folderFullPath = Join-Path -Path $RootPath -ChildPath $entry.FolderPath
if (-not (Test-Path -LiteralPath $folderFullPath)) {
Write-Warning "Dossier introuvable : $folderFullPath"
continue
}
try {
# Réinitialiser--------------------------------------------------------------------
# 4.1 Recuperation des ACL existantesactuelles et desactivation de l heritage
# --------------------------------------------------------------------
$acl = Get-Acl -LiteralPath $folderFullPath
$acl.SetAccessRuleProtection($true, $false) # désactiverDisable héritageinheritance, remove inherited rules
# Suppression de toutes les ACL existantes
foreach ($rule in @($acl.Access | ForEach-ObjectAccess)) {
[void]$acl.RemoveAccessRule($_)rule)
}
# AppliquerTypes RWet audrapeaux GDL_RWutiles
$Allow = [System.Security.AccessControl.AccessControlType]::Allow
$CI_OI = [System.Security.AccessControl.InheritanceFlags] "ContainerInherit, ObjectInherit"
$NonePF = [System.Security.AccessControl.PropagationFlags]::None
# --------------------------------------------------------------------
# 4.2 GDL_ECRITURE : Modify (herite)
# --------------------------------------------------------------------
if ($entry.GDL_RWGDL_ECRITURE -and $entry.GDL_RW.GDL_ECRITURE.Trim() -ne "") {
$rwRuleruleE = New-Object System.Security.AccessControl.FileSystemAccessRule(
$entry.GDL_RW,GDL_ECRITURE,
"Modify",[System.Security.AccessControl.FileSystemRights]::Modify,
"ContainerInherit,ObjectInherit",$CI_OI, "None",$NonePF, "Allow"$Allow
)
[void]$acl.AddAccessRule($rwRule)ruleE)
}
# Appliquer--------------------------------------------------------------------
RO# au4.3 GDL_ROGDL_LECTURE : ReadAndExecute (herite)
# --------------------------------------------------------------------
if ($entry.GDL_ROGDL_LECTURE -and $entry.GDL_RO.GDL_LECTURE.Trim() -ne "") {
$roRuleruleL = New-Object System.Security.AccessControl.FileSystemAccessRule(
$entry.GDL_RO,GDL_LECTURE,
"ReadAndExecute",[System.Security.AccessControl.FileSystemRights]::ReadAndExecute,
"ContainerInherit,ObjectInherit",$CI_OI, "None",$NonePF, "Allow"$Allow
)
[void]$acl.AddAccessRule($roRule)ruleL)
}
# Appliquer--------------------------------------------------------------------
RW# aux4.4 comptesGDL_VUE : ReadAndExecute (non herite)
# --------------------------------------------------------------------
if ($entry.GDL_VUE -and $entry.GDL_VUE.Trim() -ne "") {
$ruleV = New-Object System.Security.AccessControl.FileSystemAccessRule(
$entry.GDL_VUE,
[System.Security.AccessControl.FileSystemRights]::ReadAndExecute,
[System.Security.AccessControl.InheritanceFlags]::None,
$NonePF,
$Allow
)
[void]$acl.AddAccessRule($ruleV)
}
# --------------------------------------------------------------------
# 4.5 Comptes administratifs : Modify (herite)
# --------------------------------------------------------------------
foreach ($admin in $RWAdmins) {
$adminRuleruleA = New-Object System.Security.AccessControl.FileSystemAccessRule(
$admin,
"Modify",[System.Security.AccessControl.FileSystemRights]::Modify,
"ContainerInherit,ObjectInherit",$CI_OI, "None",$NonePF, "Allow"$Allow
)
[void]$acl.AddAccessRule($adminRule)ruleA)
}
# Sauvegarder--------------------------------------------------------------------
# 4.6 Application finale des ACL
# --------------------------------------------------------------------
Set-Acl -LiteralPath $folderFullPath -AclObject $acl
Write-Host "DroitsACL appliquésappliquees sur: $folderFullPath"
} catch {
Write-Warning "Erreur ACL sur $folderFullPath :-> $_"
}
}
# Effacer la barre de progression
Write-Progress -Activity "Application ACL" -Completed
Write-Host ""
Write-Host "Application des ACL terminee." -ForegroundColor Green
Parfait ! Nous avons à présent misemis en place l'AGDLP pour une gestion des droits beaucoup plus fine, facile, et modulable dans le temps.
De plus, ce script s'appuie sur un répertoire racine qui peut etre modifié, si jamais une nouvelle branche devait être créé dans le partage.

