Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # create an array of users
- $userslist = "batman","flash","superman"
- #Get-aduser -Filter * | where -Property 'enabled' -EQ $true
- # loop through each user to find most recent last logon time
- foreach ($user in $userslist) {
- # Check last logon attribute on each domain controller
- $DC1_ll = (get-aduser -Identity $user -Properties 'lastlogon' -Server 'DC1').lastlogon
- $DC2_ll = (get-aduser -Identity $user -Properties 'lastlogon' -server 'DC2').lastlogon
- # Check if the user has never logged into either of the DCs. If not write to log, else move to next step.
- # Start first if statement.
- if ($DC1_ll -eq '' -or $DC1_ll -eq $null -and $DC2_ll -eq '' -or $DC2_ll -eq $null) {
- $ll = ''
- "The account for $user, has never logged in." | Add-Content -Path "C:\temp\lastlogonlog.txt"
- } #End of first if statement
- # Start first else statement
- else {
- #Start of second if statement
- if ($DC1_ll -gt $DC2_ll) {
- $ll = $DC1_ll
- } #End of 2nd if statement
- #Start second else statement
- else {
- $ll = $DC2_ll
- } #End of second else statement
- } #End of first else statement
- $date = [datetime]::FromFileTime($ll)
- "$user last logged on $date" | Add-Content -Path "C:\temp\lastlogonlog.txt"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement