Skip to main content

Création des GDL

agdlp.pngagdlp.pngIntroduction

On le redit encore, mais le plus long pour l'AGDLP, c'est la mise en place, d'abord des dossiers, et ensuite des groupes GDL, ce que nous allons voir maintenant !

GDL et OU

Import-ModuleParam(
    ActiveDirectory

# Racine des dossiers
[string]$SourcePathModelePath = "chemin\vers\Partage"C:\Script\Arbo\arbo.txt",
    [string]$CsvPath = "C:\Script\Arbo\GDLMapping.csv",
    [string]$BaseOU = "OU=AGDLP,DC=ccpl,DC=local" # OU racineBase pour créer les OUsOU
)

Try { Import-Module ActiveDirectory -ErrorAction Stop } Catch {}

$OUracineGDLList = "OU=AGDLP,DC=domain,DC=local"@()

# OnLire récupèrele lafichier listemodèle desTXT dossiers,avec encodage UTF8
$ModeleLines = Get-Content $ModelePath -Encoding UTF8

# HashTable pour créergarder lale mêmemapping structureFolderPath dans-> l'AD, afin d'avoir une meilleure lisibilité en cas de modifs
function Get-OrCreateOU {
    param(
        [string[]]$OUlist,
        [string]$OUbase
    )GDL_RO
$currentPathFolderToGDL = @{}

Function Create-OU($OUbase
    foreach ($ou inouName, $OUlist)parentDN) {
    $currentPathouDN = "OU=$ou,ouName,$currentPath"parentDN"
    if (-not (Get-ADOrganizationalUnit -Filter "NameDistinguishedName -eq '$ou'ouDN'" -SearchBase $currentPath.Substring($currentPath.IndexOf(",")+1) -ErrorAction SilentlyContinue)) {
        New-ADOrganizationalUnit -Name $ououName -Path $currentPath.Substring(parentDN -ProtectedFromAccidentalDeletion $currentPath.IndexOf(",")+1)false
        Write-Host "OU créée : $ou sous $currentPath"
        }ouDN"
    }
    return $currentPathouDN
}

#Function Parcours récursif des dossiers
Get-ChildItem -PathCreate-GDL($GroupName, $SourcePath -Recurse -Directory | ForEach-Object {
    $fullPath = $_.FullName.Substring($SourcePath.Length).Trim("\")
    $parts = $fullPath -split "\\"  # découpe en dossiers

    if ($parts.Count -gt 0) {
        # Pour les groupes : garder uniquement le premier mot de chaque dossier
        # Le but est de limite la taille du nom du groupe, d'ou le système de numérotation dans la nomenclature
        # sauf pour les dossiers finaux dans le cas présent
        $tokens = @()
        foreach ($p in $parts) {
            $tokens += ($p -split " ")[0]
        }

        $nomBase = "GDL_" + ($tokens -join "-")
        $groupRO = "${nomBase}_RO"
        $groupRW = "${nomBase}_RW"

        # Pour les OU : garder le nom complet de chaque dossier
        # plus facile de retrouver les groupes associés aux repertoires si les OU et l'arborescence sont en miroir
        $ouPath = Get-OrCreateOU -OUlist $parts -OUbase $OUracine

        # Créer les groupes s’ils n’existent pas
        foreach ($grp in @($groupRO, $groupRW))OU) {
    if (-not (Get-ADGroup -Filter "Name -eq '$grp'GroupName'" -SearchBase $ouPath -ErrorAction SilentlyContinue)) {
        New-ADGroup -Name $grp -SamAccountName $grpGroupName -GroupScope Global -GroupCategory Security -Path $ouPathOU
        Write-Host "GroupeGDL créé : $grpGroupName dans $ouPath"OU"
    }
}

foreach ($folderPath in $ModeleLines) {
    $folderPath = $folderPath.Trim()
    if ($folderPath -eq "") { continue }

    # Extraire uniquement les numéros pour le GDL
    $numbers = ($folderPath -split '\\' | ForEach-Object {
        if ($_ -match '^\d+') { $Matches[0] }
    }) -join '-'

    $GDL_RO = "GDL_" + $numbers + "_RO"
    $GDL_RW = "GDL_" + $numbers + "_RW"

    # Création des OU correspondantes
    $parts = $folderPath -split '\\'
    $parentDN = $BaseOU
    foreach ($part in $parts) {
        $ouDN = Create-OU $part $parentDN
        $parentDN = $ouDN
    }

    # Calculer le parent pour le CSV
    $parentFolder = Split-Path $folderPath -Parent
    if ($parentFolder -and $FolderToGDL.ContainsKey($parentFolder)) {
        $parentRO = $FolderToGDL[$parentFolder]
    } else {
        $parentRO = $null
    }

    # Ajouter au mapping
    $FolderToGDL[$folderPath] = $GDL_RO

    # Créer les GDL dans l’OU correspondante
    Create-GDL $GDL_RO $ouDN
    Create-GDL $GDL_RW $ouDN

    # Ajouter à la liste pour le CSV
    $GDLList += [PSCustomObject]@{
        FolderPath   = $folderPath
        GDLNameRO    = $GDL_RO
        GDLNameRW    = $GDL_RW
        ParentGDLRO  = $parentRO
    }
}

# Export CSV avec UTF8 pour les accents
$GDLList | Export-Csv -Path $CsvPath -NoTypeInformation -Encoding UTF8
Write-Host "CSV généré : $CsvPath"

Bien ! A présent nous avons des groupes bien nommés dans des OU bien classées.
Nous allons passer à la mise en place de la hiérarchie des groupes !