Advertisement
Guest User

Preserve Last Login

a guest
Nov 15th, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Saves the last login so the last user account on the login screen (if enabled) is preserved after you remote login
  2. # to a computer using a different account. Run script before remote logging into a computer and make sure it's
  3. # started before you log in. It will wait for you to login before it completes.  
  4. # It will save the current value of last logged in user so after you log in, it will change it back to what it was.  
  5.  
  6. # Save current last login info BEFORE logging in as a different account.  Make sure max script login time is high
  7. # enough so you have time to complete the login.
  8.  
  9. $SID = (Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Authentication\LogonUI").LastLoggedOnUserSID
  10. $displayName = (Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Authentication\LogonUI").LastLoggedOnDisplayName
  11. $user = (Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Authentication\LogonUI").LastLoggedOnUser
  12. $SAMuser = (Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Authentication\LogonUI").LastLoggedOnSAMUser
  13.  
  14. # Wait for admin to get logged in.  Soon as the last logged in keys change, reset them back to what they were.
  15. while ($user -eq ((Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Authentication\LogonUI").LastLoggedOnUser)) {
  16.     start-sleep 5
  17. }
  18.  
  19. reg add HKLM\Software\Microsoft\Windows\CurrentVersion\Authentication\LogonUI /v LastLoggedOnUserSID /d $SID /f > $null
  20. reg add HKLM\Software\Microsoft\Windows\CurrentVersion\Authentication\LogonUI /v LastLoggedOnDisplayName /d $displayName /f > $null
  21. reg add HKLM\Software\Microsoft\Windows\CurrentVersion\Authentication\LogonUI /v LastLoggedOnUser /d $user /f > $null
  22. reg add HKLM\Software\Microsoft\Windows\CurrentVersion\Authentication\LogonUI /v LastLoggedOnSAMUser /d $SAMuser /f > $null
  23.  
  24. echo "Last login reset to $user - $displayName"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement