Guest User

Monitor Log on Off

a guest
Oct 4th, 2024
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #Set the Audit log on the Other Logon/Logoff Events which records lock/unlock events.
  2. Auditpol /set /subcategory:"Other Logon/Logoff Events" /success:enable /failure:enable
  3.  
  4. #List the last thirty days
  5. $Last30Days = (Get-date).AddDays(-30)
  6.  
  7. #Get Time of event
  8. $TimeProperty = @{n = "Time"; e = { $_.timecreated } }
  9.  
  10. #Get name of event (Lock, Unlock)
  11. $SecurityTypeProperty = @{n = "Action"; e = { if ($_.ID -eq 4800) { "Lock" } elseif ($_.ID -eq 4801) { "Unlock" } } }
  12.  
  13. #Get the user logon name from the securtiy event.
  14. $User = @{n = 'User'; e = { $_.Properties[0].Value.Translate([System.Security.Principal.NTAccount]) } }
  15.  
  16. # Filter system events for (Logon, Logoff)
  17. $TypeProperty = @{n = "Action"; e = { if ($_.ID -eq 7001) { "Logon" } else { "Logoff" } } }
  18.  
  19. #Get user logon name for the system logon/logoff event
  20. $UserProperty = @{n = "User"; e = { (New-Object System.Security.Principal.SecurityIdentifier $_.Properties.Value[1]).Translate([System.Security.Principal.NTAccount]) } }
  21.  
  22. #Get System logon events for the last thirty days
  23. Get-WinEvent -FilterHash @{LogName = 'system'; providername = 'Microsoft-Windows-Winlogon'; Starttime = $Last30Days } | Select-Object $UserProperty, $TimeProperty, $TypeProperty| Out-File -FilePath "C:\Users\Public\Documents\events.log"
  24.  
  25. #Collect security lock events for the last thirty days.
  26. Get-winevent -FilterHashtable @{Logname = 'security'; providername = 'Microsoft-Windows-Security-Auditing'; id = 4800, 4801; Starttime = $Last30Days } | Select-object $User, $TimeProperty, $SecurityTypeProperty | Out-File -FilePath "C:\Users\Public\Documents\events.log" -Append
Advertisement
Add Comment
Please, Sign In to add comment