AD 조직도 TREE형 추출 커맨드

ㅁ AD 조직도 TREE형 추출

: 파워쉘에서 아래 커맨드 이행

 

- 추출예시

OU_Level_1 OU_Level_2 OU_Level_3 Type Name
test1     OU test1
test1 test2   OU test2
test1 test2   User userA
test1 test2 sub1 Group GroupA
test1 test3   OU test3

 

- 커맨드

Import-Module ActiveDirectory

$script:result = @()
$MaxDepth = 10   # 필요 시 증가

function New-BaseRow {
    $row = [ordered]@{}
    for ($i = 1; $i -le $MaxDepth; $i++) {
        $row["OU_Level_$i"] = ""
    }
    return $row
}

function Get-OUTreeColumn {
    param (
        [string]$BaseDN,
        [string[]]$Path
    )

    $ous = Get-ADOrganizationalUnit -Filter * -SearchBase $BaseDN -SearchScope OneLevel | Sort Name

    foreach ($ou in $ous) {

        $newPath = $Path + $ou.Name

        # OU Row
        $row = New-BaseRow
        for ($i = 0; $i -lt $newPath.Count; $i++) {
            $row["OU_Level_$($i+1)"] = $newPath[$i]
        }
        $row["Type"] = "OU"
        $row["Name"] = $ou.Name
        $script:result += New-Object psobject -Property $row

        # Users
        Get-ADUser -Filter * -SearchBase $ou.DistinguishedName -SearchScope OneLevel |
        ForEach-Object {
            $row = New-BaseRow
            for ($i = 0; $i -lt $newPath.Count; $i++) {
                $row["OU_Level_$($i+1)"] = $newPath[$i]
            }
            $row["Type"] = "User"
            $row["Name"] = $_.SamAccountName
            $script:result += New-Object psobject -Property $row
        }

        # Groups
        Get-ADGroup -Filter * -SearchBase $ou.DistinguishedName -SearchScope OneLevel |
        ForEach-Object {
            $row = New-BaseRow
            for ($i = 0; $i -lt $newPath.Count; $i++) {
                $row["OU_Level_$($i+1)"] = $newPath[$i]
            }
            $row["Type"] = "Group"
            $row["Name"] = $_.Name
            $script:result += New-Object psobject -Property $row
        }

        # 재귀
        Get-OUTreeColumn -BaseDN $ou.DistinguishedName -Path $newPath
    }
}

$domain = Get-ADDomain
Get-OUTreeColumn -BaseDN $domain.DistinguishedName -Path @()

$script:result | Export-Csv "C:\AD_Tree_Column.csv" -NoTypeInformation -Encoding UTF8

 

 

- 좀 더 유저 정보를 딥하게 추출

Import-Module ActiveDirectory

# ─────────────────────────────────────
# OU 최대 깊이 자동 계산 (안 터지는 버전)
# ─────────────────────────────────────
$domain = Get-ADDomain
$rootDN = $domain.DistinguishedName

$allOUs = Get-ADOrganizationalUnit -Filter *

$MaxDepth = ($allOUs |
    ForEach-Object {
        ($_.DistinguishedName -split 'OU=').Count - 1
    } |
    Measure-Object -Maximum
).Maximum

Write-Host "Detected Max OU Depth: $MaxDepth" -ForegroundColor Cyan

# ─────────────────────────────────────
# 결과 저장소
# ─────────────────────────────────────
$script:result = @()

# ─────────────────────────────────────
# 기본 Row 생성
# ─────────────────────────────────────
function New-BaseRow {
    $row = [ordered]@{}
    for ($i = 1; $i -le $MaxDepth; $i++) {
        $row["OU_Level_$i"] = ""
    }
    return $row
}

# ─────────────────────────────────────
# OU Tree 순회
# ─────────────────────────────────────
function Get-OUTreeColumn {
    param (
        [string]$BaseDN,
        [string[]]$Path
    )

    $ous = Get-ADOrganizationalUnit `
        -Filter * `
        -SearchBase $BaseDN `
        -SearchScope OneLevel |
        Sort Name

    foreach ($ou in $ous) {

        $newPath = $Path + $ou.Name

        # ── OU ──
        $row = New-BaseRow
        for ($i = 0; $i -lt $newPath.Count; $i++) {
            $row["OU_Level_$($i+1)"] = $newPath[$i]
        }
        $row["Type"]           = "OU"
        $row["SamAccountName"] = ""
        $row["DisplayName"]    = ""
        $row["Email"]          = ""
        $row["Name"]           = $ou.Name
        $script:result += [pscustomobject]$row

        # ── Users ──
        Get-ADUser `
            -Filter * `
            -SearchBase $ou.DistinguishedName `
            -SearchScope OneLevel `
            -Properties DisplayName, Mail |
        ForEach-Object {

            $row = New-BaseRow
            for ($i = 0; $i -lt $newPath.Count; $i++) {
                $row["OU_Level_$($i+1)"] = $newPath[$i]
            }
            $row["Type"]           = "User"
            $row["SamAccountName"] = $_.SamAccountName
            $row["DisplayName"]    = $_.DisplayName
            $row["Email"]          = $_.Mail
            $row["Name"]           = $_.Name
            $script:result += [pscustomobject]$row
        }

        # ── Groups ──
        Get-ADGroup `
            -Filter * `
            -SearchBase $ou.DistinguishedName `
            -SearchScope OneLevel |
        ForEach-Object {

            $row = New-BaseRow
            for ($i = 0; $i -lt $newPath.Count; $i++) {
                $row["OU_Level_$($i+1)"] = $newPath[$i]
            }
            $row["Type"]           = "Group"
            $row["SamAccountName"] = ""
            $row["DisplayName"]    = ""
            $row["Email"]          = ""
            $row["Name"]           = $_.Name
            $script:result += [pscustomobject]$row
        }

        # 재귀
        Get-OUTreeColumn -BaseDN $ou.DistinguishedName -Path $newPath
    }
}

# ─────────────────────────────────────
# 실행
# ─────────────────────────────────────
Get-OUTreeColumn -BaseDN $rootDN -Path @()

# ─────────────────────────────────────
# 컬럼 자동 Export
# ─────────────────────────────────────
$ouColumns = 1..$MaxDepth | ForEach-Object { "OU_Level_$_" }
$finalColumns = $ouColumns + @(
    "Type",
    "SamAccountName",
    "DisplayName",
    "Email",
    "Name"
)

$script:result |
    Select-Object $finalColumns |
    Export-Csv "C:\AD_Tree_Column.csv" -NoTypeInformation -Encoding UTF8


댓글
0
댓글이 없습니다.