Advertisement
Guest User

PS_UserAudit2

a guest
Apr 18th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # create an array of users
  2. $userslist = "batman","flash","superman"
  3. #Get-aduser -Filter * | where -Property 'enabled' -EQ $true
  4.  
  5. # loop through each user to find most recent last logon time
  6. foreach ($user in $userslist) {
  7.    
  8.     # Check last logon attribute on each domain controller
  9.     $DC1_ll = (get-aduser -Identity $user -Properties 'lastlogon' -Server 'DC1').lastlogon
  10.     $DC2_ll = (get-aduser -Identity $user -Properties 'lastlogon' -server 'DC2').lastlogon
  11.  
  12.     # Check if the user has never logged into either of the DCs. If not write to log, else move to next step.
  13.     # Start first if statement.
  14.     if ($DC1_ll -eq '' -or $DC1_ll -eq $null -and $DC2_ll -eq '' -or $DC2_ll -eq $null) {
  15.        
  16.         $ll = ''
  17.         "The account for $user, has never logged in." | Add-Content -Path "C:\temp\lastlogonlog.txt"
  18.  
  19.     } #End of first if statement
  20.  
  21.     # Start first else statement
  22.     else {
  23.    
  24.         #Start of second if statement
  25.         if ($DC1_ll -gt $DC2_ll) {
  26.            
  27.             $ll = $DC1_ll
  28.            
  29.         } #End of 2nd if statement
  30.  
  31.         #Start second else statement
  32.         else {
  33.            
  34.             $ll = $DC2_ll
  35.            
  36.         } #End of second else statement
  37.    
  38.     } #End of first else statement
  39.  
  40. $date = [datetime]::FromFileTime($ll)
  41. "$user last logged on $date" | Add-Content -Path "C:\temp\lastlogonlog.txt"
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement