lincruste

List locked accounts - All DCs

Jan 27th, 2026
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 1.73 KB | Source Code | 0 0
  1. # 1. Récupérer tous les contrôleurs de domaine
  2. $DCs = Get-ADDomainController -Filter *
  3. $LookbackHours = 24 # On regarde les dernières 24h
  4.  
  5. $Results = ForEach ($DC in $DCs) {
  6.     Write-Host "Interrogation de $($DC.HostName)..." -ForegroundColor Cyan
  7.    
  8.     try {
  9.         # Filtre optimisé
  10.         $FilterHash = @{
  11.             LogName = 'Security'
  12.             Id      = 4740
  13.             StartTime = (Get-Date).AddHours(-$LookbackHours)
  14.         }
  15.        
  16.         $Events = Get-WinEvent -ComputerName $DC.HostName -FilterHashtable $FilterHash -ErrorAction Stop
  17.  
  18.         ForEach ($Event in $Events) {
  19.             # Extraction par index des propriétés (plus fiable que le nom XML parfois capricieux)
  20.             # Index 0: Utilisateur verrouillé / Index 1: Machine source
  21.             $Properties = $Event.Properties
  22.            
  23.             [PSCustomObject]@{
  24.                 Date           = $Event.TimeCreated
  25.                 Utilisateur    = $Properties[0].Value
  26.                 SourceMachine  = $Properties[1].Value
  27.                 DC_Emetteur    = $DC.HostName
  28.             }
  29.         }
  30.     } catch {
  31.         # Si aucun événement n'est trouvé, Get-WinEvent génère une erreur, on la gère ici
  32.         if ($_.Exception.Message -like "*No events were found*") {
  33.             Write-Host "  -> Aucun verrouillage sur ce DC." -ForegroundColor Gray
  34.         } else {
  35.             Write-Warning "Erreur sur $($DC.HostName) : $($_.Exception.Message)"
  36.         }
  37.     }
  38. }
  39.  
  40. # 4. Affichage final
  41. if ($Results) {
  42.     $Results | Sort-Object Date -Descending | Out-GridView -Title "Détails des verrouillages AD"
  43. } else {
  44.     Write-Host "Aucun événement trouvé dans les dernières $LookbackHours heures." -ForegroundColor Yellow
  45. }
Advertisement
Add Comment
Please, Sign In to add comment