Skip to main content

Création des GDL

agdlp.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

Param(
    [string]$ModelePath = "C:\chemin\vers\modele.txt",
    [string]$CsvPath = "C:\chemin\vers\GDLMapping.csv",
    [string]$BaseOU = "OU=AGDLP,DC=domain,DC=local" # Base pour créer les OU
)
Try { Import-Module ActiveDirectory -ErrorAction Stop } Catch {}

$GDLList = @()

# Lire le fichier modèle TXT avec encodage UTF8
$ModeleLines = Get-Content $ModelePath -Encoding UTF8

# HashTable pour garder le mapping FolderPath -> GDL_RO
$FolderToGDL = @{}

Function Create-OU($ouName, $parentDN) {
    $ouDN = "OU=$ouName,$parentDN"
    if (-not (Get-ADOrganizationalUnit -Filter "DistinguishedName -eq '$ouDN'" -ErrorAction SilentlyContinue)) {
        New-ADOrganizationalUnit -Name $ouName -Path $parentDN -ProtectedFromAccidentalDeletion $false
        # retirer -ProtectedFromAccidentalDeletion $false pour la prod, cela autorise la suppression de toute l'arbo dans AGDLP
        Write-Host "OU créée : $ouDN"
    }
    return $ouDN
}

Function Create-GDL($GroupName, $OU) {
    if (-not (Get-ADGroup -Filter "Name -eq '$GroupName'" -ErrorAction SilentlyContinue)) {
        New-ADGroup -Name $GroupName -GroupScope Global -Path $OU
        Write-Host "GDL créé : $GroupName dans $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 !