Advertisement
Camiloluke

Check Locked Users

Jul 28th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3.  
  4.  
  5.  
  6. function show-progressbar([int]$actual,[int]$full,[string]$status,[string]$Activity)
  7. {
  8.     $porcentaje=($actual/$full)*100
  9.     if (!$status){
  10.         $status="Searching $actual of $full"
  11.     }
  12.     if (!$Activity){
  13.         $Activity="Getting results"
  14.     }
  15.     Write-Progress -Activity $Activity -status $status -percentComplete $porcentaje
  16.     }
  17.  
  18. import-module activedirectory
  19.  
  20.      
  21. $logcompleto=@()
  22. $logdcs=@()
  23. $cuentadc=1
  24.  
  25. # Creates a new "Events" folder, just in case it doesn't exist.
  26. New-Item -Name Events -Type directory -Force
  27.  
  28. # Levanta todos los DCs
  29. $dcs=Get-ADDomainController -Filter *
  30.  
  31. $dctotal=$dcs.count
  32.  
  33. foreach ($dc in $dcs){
  34.     $nombredc=$dc.name
  35.     $NoEvents=$false
  36.     $EventCounter=0
  37.     $show=$true
  38.     $EventType=" "
  39.      
  40.     # Checking Domain Controller $nombredc - Chequeando disponibilidad.
  41.     $Status="Checking Domain Controller $nombredc ($cuentadc of $dctotal)´
  42.    - Chequeando disponibilidad."
  43.     show-progressbar $cuentadc $dctotal $status    
  44.     $online = Test-Connection $nombredc -Quiet -count 1
  45.      
  46.     if ($online){
  47.      
  48.  
  49.         # Checking Domain Controller $nombredc - Getting Events.
  50.         $Status="Checking Domain Controller $nombredc ($cuentadc of ´
  51.        $dctotal) - Getting Events."
  52.         show-progressbar $cuentadc $dctotal $status    
  53.          
  54.         # Get-WinEvent searches the event log, we use -FilterXPath ´
  55.         #to search for specific events.
  56.         # The two relevant EventISDs for this script are:
  57.         # EventID=4740 = user Lockout
  58.         # EventID=4625 = Failed Login
  59.         $logs=(Invoke-Command -computername $nombredc -ScriptBlock ´
  60.         {Get-WinEvent -FilterXPath "*[System[(EventID=4740 or EventID=4625)]]" ´
  61.         -ErrorAction silentlyContinue} )
  62.         if ($logs -eq $null){
  63.             # No events to show
  64.             $NoEvents=$true
  65.             $TotalEvents=0
  66.         }
  67.          
  68.          
  69.         if (!$NoEvents){
  70.          
  71.             $TotalEvents=$logs.count
  72.             foreach ($evento in $logs){
  73.                 $EventCounter+=1
  74.                  
  75.                 # Checking Domain Controller $nombredc - Total Events
  76.                 $Status="Checking Domain Controller $nombredc ´
  77.                ($cuentadc of $dctotal)´
  78.                - Event $EventCounter of $TotalEvents"
  79.                 show-progressbar $cuentadc $dctotal $status
  80.                 $mensaje=$evento.message
  81.                  
  82.                 $hora=$evento.TimeCreated.ToShortTimeString()
  83.                 $fecha=$evento.TimeCreated.ToShortDateString()
  84.                  
  85.                 $id=$evento.ID
  86.                 $datos=$mensaje.Split("`n")
  87.                  
  88.                 Switch ($id)
  89.                 {
  90.                      
  91.                     4740{
  92.                         ## CASE 4740 - User Lockout
  93.                         # datos[10] has the username
  94.                         $usuario=(($datos[10].split("`t"))[3])
  95.                         # datos[13] has the machine where the lockout happened
  96.                         $Maquina=(($datos[13].split("`t"))[2])
  97.                         $EventType="Lockout"
  98.                         ## FIN CASE 4740
  99.                     }
  100.                      
  101.                     4625{
  102.                         ## CASE 4625 - Login Failure
  103.                         ## (Check for errors)
  104.                         if ($evento.Providername -eq ´
  105.                         "Microsoft-Windows-Security-Auditing"){
  106.                             # datos[12] has the username
  107.                             $usuario=(($datos[12].split("`t"))[3])
  108.                             # datos[25] has the machine where the failed login happened
  109.                             $Maquina=(($datos[25].split("`t"))[2])
  110.                             $EventType="Bad Login"
  111.                         }
  112.                         else{
  113.                             $show=$false
  114.                         }
  115.                         ## FIN CASE 4625
  116.                     }
  117.                     default{
  118.                         ## Something went bad.
  119.                         $usuario="ERROR"
  120.                         $maquina="ERROR"
  121.                         $EventType="ERROR"
  122.                     }
  123.                 }
  124.                  
  125.                 if ($show){
  126.                     $logcompleto+= $usuario | select-object ´
  127.                     @{Expression={$usuario};Label="User"},´
  128.                     @{Expression={$maquina};Label="Machine"},´
  129.                     @{Expression={$EventType};Label="Event"},´
  130.                     @{Expression={$fecha};Label="Date"},´
  131.                     @{Expression={$Hora};Label="Time"},´
  132.                     @{Expression={$nombredc};Label="DC"}
  133.                 }
  134.             }
  135.         }
  136.     }
  137.     else{
  138.     # Is the DC is unavailable
  139.     write-host "The Domain controller $nombredc is OFFLINE" ´
  140.     -backgroundcolor "red" ´
  141.     -ForegroundColor black
  142.     }
  143.      
  144.     $logDCS+=$cuentadc|select-object ´
  145.     @{Expression={$nombredc};Label="DC"}, ´
  146.     @{Expression={$online};Label="Online"}, ´
  147.     @{Expression={$EventCounter};Label="Evento"}
  148.      
  149.     $cuentadc+=1
  150. }
  151.  
  152. # Exports the Events log to CSV
  153. $logcompleto| export-csv ./Events/Log_Bloqueados.csv
  154. # Exports the DC Availability log to CSV
  155. $logdcs | export-csv ./Events/log_dcs.csv
  156. ##Fin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement