Advertisement
Guest User

Check Locked Users

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