powershell nobind monitor

<pre class="wp-block-syntaxhighlighter-code"># ============================================================================
# Script d'analyse des Monitors NetScaler
# ============================================================================
# 
# But : Identifier les monitors non utilises
# - Un monitor est inutilise s'il n'est binde a aucun serviceGroup
#
# ============================================================================

# ========== CONFIGURATION ==========
# Modifiez cette ligne avec votre chemin vers ns.conf
$NS_CONF_PATH = "C:\logs\ns.conf"
# ====================================

# ============================================================================
# FONCTIONS PRINCIPALES
# ============================================================================

function Load-NSConf {
    param([string]$FilePath)
    
    if (Test-Path $FilePath) {
        Write-Host "`n[INFO] Chargement du fichier : $FilePath" -ForegroundColor Cyan
        return Get-Content -Path $FilePath -Encoding UTF8
    } else {
        Write-Host "`n[ERREUR] Le fichier $FilePath est introuvable !" -ForegroundColor Red
        exit 1
    }
}

function Get-Monitors {
    param([string[]]$Lines)
    
    $monitors = @{}
    
    foreach ($line in $Lines) {
        # Recherche les lignes : add lb monitor <nom> ...
        if ($line -match '^add lb monitor\s+(\S+)') {
            $monitorName = $Matches[1]
            $monitors[$monitorName] = $line
        }
    }
    
    return $monitors
}

function Get-MonitorBindings {
    param([string[]]$Lines)
    
    $bindings = @{}
    
    foreach ($line in $Lines) {
        # Bind serviceGroup <nom> -monitorName <monitor>
        if ($line -match '^bind serviceGroup\s+(\S+).*-monitorName\s+(\S+)') {
            $serviceGroupName = $Matches[1]
            $monitorName = $Matches[2]
            
            if (-not $bindings.ContainsKey($monitorName)) {
                $bindings[$monitorName] = @()
            }
            
            $bindings[$monitorName] += @{
                'ServiceGroup' = $serviceGroupName
                'Line' = $line
            }
        }
    }
    
    return $bindings
}

# ============================================================================
# AFFICHAGE DES RESULTATS
# ============================================================================

function Show-UnusedMonitors {
    param($Monitors, $Bindings)
    
    Write-Host "`n==================================================================" -ForegroundColor Red
    Write-Host " MONITORS NON BINDES" -ForegroundColor Red -BackgroundColor Black
    Write-Host "==================================================================" -ForegroundColor Red
    
    $unusedMonitors = @()
    
    foreach ($monitorName in $Monitors.Keys) {
        if (-not $Bindings.ContainsKey($monitorName)) {
            $unusedMonitors += $monitorName
        }
    }
    
    if ($unusedMonitors.Count -eq 0) {
        Write-Host "`nAucun monitor non binde trouve !" -ForegroundColor Green
    } else {
        Write-Host "`nNombre de monitors non bindes : $($unusedMonitors.Count)" -ForegroundColor Red
        Write-Host "Ces monitors ne sont bindes a AUCUN serviceGroup :`n" -ForegroundColor Yellow
        
        foreach ($monitorName in ($unusedMonitors | Sort-Object)) {
            Write-Host "  ├─ " -NoNewline -ForegroundColor Red
            Write-Host "$monitorName" -ForegroundColor Red
            Write-Host "  │  " -NoNewline -ForegroundColor DarkGray
            Write-Host "$($Monitors[$monitorName])" -ForegroundColor White
            Write-Host "  │" -ForegroundColor DarkGray
            Write-Host "  │  Commande de suppression :" -ForegroundColor DarkGray
            Write-Host "  │  " -NoNewline -ForegroundColor DarkGray
            Write-Host "rm lb monitor $monitorName" -ForegroundColor White
            Write-Host "  │" -ForegroundColor DarkGray
        }
        Write-Host "  └─────────────────────────────────────────────────────────────" -ForegroundColor Red
    }
}

function Show-UsedMonitors {
    param($Monitors, $Bindings)
    
    Write-Host "`n==================================================================" -ForegroundColor Green
    Write-Host " MONITORS BINDES" -ForegroundColor Green -BackgroundColor Black
    Write-Host "==================================================================" -ForegroundColor Green
    
    $usedMonitors = @()
    
    foreach ($monitorName in $Monitors.Keys) {
        if ($Bindings.ContainsKey($monitorName)) {
            $usedMonitors += $monitorName
        }
    }
    
    if ($usedMonitors.Count -eq 0) {
        Write-Host "`nAucun monitor binde trouve !" -ForegroundColor Yellow
    } else {
        Write-Host "`nNombre de monitors bindes : $($usedMonitors.Count)" -ForegroundColor Green
        Write-Host "Ces monitors sont bindes a au moins un serviceGroup :`n" -ForegroundColor Green
        
        foreach ($monitorName in ($usedMonitors | Sort-Object)) {
            Write-Host "  ├─ " -NoNewline -ForegroundColor Green
            Write-Host "$monitorName" -ForegroundColor Green
            Write-Host "  │  " -NoNewline -ForegroundColor DarkGray
            Write-Host "Binde sur les serviceGroups :" -ForegroundColor DarkGray
            
            foreach ($binding in $Bindings[$monitorName]) {
                Write-Host "  │    • " -NoNewline -ForegroundColor DarkGray
                Write-Host "ServiceGroup: $($binding['ServiceGroup'])" -ForegroundColor Cyan
                Write-Host "  │      " -NoNewline -ForegroundColor DarkGray
                Write-Host "$($binding['Line'])" -ForegroundColor White
            }
            Write-Host "  │" -ForegroundColor DarkGray
        }
        Write-Host "  └─────────────────────────────────────────────────────────────" -ForegroundColor Green
    }
}

# ============================================================================
# EXECUTION PRINCIPALE
# ============================================================================

Clear-Host

Write-Host "`n" -NoNewline
Write-Host "╔" -NoNewline -ForegroundColor Cyan
Write-Host "═══════════════════════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║" -NoNewline -ForegroundColor Cyan
Write-Host "                                                                               " -NoNewline
Write-Host "║" -ForegroundColor Cyan
Write-Host "║" -NoNewline -ForegroundColor Cyan
Write-Host "                     ANALYSE DES MONITORS NETSCALER                            " -NoNewline -ForegroundColor Yellow
Write-Host "║" -ForegroundColor Cyan
Write-Host "║" -NoNewline -ForegroundColor Cyan
Write-Host "                                                                               " -NoNewline
Write-Host "║" -ForegroundColor Cyan
Write-Host "╚" -NoNewline -ForegroundColor Cyan
Write-Host "═══════════════════════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan

# Charger la configuration
$lines = Load-NSConf -FilePath $NS_CONF_PATH

# Extraire les objets
Write-Host "`n[ETAPE 1] Extraction des monitors..." -ForegroundColor Cyan
$monitors = Get-Monitors -Lines $lines

Write-Host "`n[ETAPE 2] Extraction des bindings de monitors..." -ForegroundColor Cyan
$bindings = Get-MonitorBindings -Lines $lines

# Afficher les résultats
Write-Host "`n[ETAPE 3] Generation du rapport..." -ForegroundColor Cyan

# Calculer les statistiques
$unusedMonitorsCount = ($monitors.Keys | Where-Object { -not $bindings.ContainsKey($_) }).Count
$usedMonitorsCount = ($monitors.Keys | Where-Object { $bindings.ContainsKey($_) }).Count

# Afficher le RESUME en premier
Write-Host "`n==================================================================" -ForegroundColor Cyan
Write-Host " RESUME DE L'ANALYSE" -ForegroundColor Yellow -BackgroundColor Black
Write-Host "==================================================================" -ForegroundColor Cyan

Write-Host "`nMONITORS :" -ForegroundColor White
Write-Host "  • Total : $($monitors.Count)" -ForegroundColor Cyan
Write-Host "  • Bindes : $usedMonitorsCount" -ForegroundColor Green
Write-Host "  • Non bindes : $unusedMonitorsCount" -ForegroundColor Red

Write-Host "`n==================================================================" -ForegroundColor Cyan

# Generer les commandes de suppression si necessaire
$unusedMonitorsList = $monitors.Keys | Where-Object { -not $bindings.ContainsKey($_) } | Sort-Object

if ($unusedMonitorsList.Count -gt 0) {
    Write-Host "`n==================================================================" -ForegroundColor Yellow
    Write-Host " COMMANDES DE SUPPRESSION (copier-coller)" -ForegroundColor Yellow -BackgroundColor Black
    Write-Host "==================================================================" -ForegroundColor Yellow
    Write-Host ""
    
    foreach ($monitorName in $unusedMonitorsList) {
        Write-Host "rm lb monitor $monitorName" -ForegroundColor White
    }
    
    Write-Host "`n==================================================================" -ForegroundColor Yellow
}

# Afficher d'abord les objets NON UTILISÉS en ROUGE
Show-UnusedMonitors -Monitors $monitors -Bindings $bindings

# Afficher ensuite les objets UTILISÉS en VERT
Show-UsedMonitors -Monitors $monitors -Bindings $bindings

Write-Host "`nAnalyse terminee avec succes !" -ForegroundColor Green
Write-Host ""

</pre>