Guest User

[POWERSHELL] Logged On Users v1.0

a guest
Jan 22nd, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Purpose:        Stamps usernames to a file as users log on, then removes them when they log off.
  2. #                 Clears this file at each boot/reboot. Purpose is to keep a comma-separated file
  3. #                 listing all users logged on to the system so we can query it with other text-based tools.
  4. # Requirements:   1. Write-access to the log location
  5. # Author:         vocatus on reddit.com/r/sysadmin and /r/usefulscripts
  6. # History:        1.0   Initial write
  7.  
  8. # Usage:          Call the script with one of the following flags:
  9. #                   -logon    Add username to the list of current users
  10. #                   -logoff   Remove username from the list of current users
  11. #                   -flush    Flush all usernames (use when rebooting/booting)
  12.  
  13.  
  14. ########
  15. # Prep #
  16. ########
  17. $VERSION="1.0"
  18. $UPDATED="2014-01-21"
  19. $CUR_DATE=get-date -f "yyyy-MM-dd"
  20.  
  21. #############
  22. # VARIABLES # -- Set these to your desired values
  23. #############
  24. # Rules for variables:
  25. #  * Quotes are required              (e.g.:  "c:\directory\path"        )
  26. #  * NO trailing slashes on paths!    (bad:   "c:\directory\"            )
  27. #  * Spaces are okay                  (okay:  "c:\my folder\with spaces" )
  28. #  * Network paths are okay           (okay:  "\\server\share name"      )
  29. #                                     (       "\\172.16.1.5\share name"  )
  30.  
  31. # Where to keep the file containing the list of logged-on users? Regular users must have write-access to this location
  32. $LOGPATH=$env:systemdrive + "\Logs"
  33. $LOGFILE="logged_on_users"
  34.  
  35.  
  36. #############
  37. # EXECUTION #
  38. #############
  39. # If no argument passed, spit out an error and die
  40. if (! $args) {
  41.     write-host ""
  42.         write-host "ERROR: No action specified" -f red
  43.         write-host ""
  44.         write-host "Actions are: "
  45.         write-host "  -logon   Stamp username to" $LOGPATH\$LOGFILE -f gray
  46.         write-host "  -logoff  Remove username from" $LOGPATH\$LOGFILE -f gray
  47.         write-host "  -flush   Flush all usernames from" $LOGPATH\$LOGFILE -f gray
  48.         write-host ""
  49.         write-host "  e.g. .\logged_on_users.ps1 -logon" -f gray
  50.         write-host ""
  51.         break
  52. }
  53.  
  54. # Add this user to the list of current users
  55. # Direct call to .net framework since PowerShell doesn't have an easy way to write to a file without a new-line
  56. if ($args -eq "-logon") {
  57.     [System.IO.File]::AppendAllText("$LOGPATH\$LOGFILE", $env:username + ",")
  58. }
  59.  
  60. # Remove user from list of current users
  61. if ($args -eq "-logoff") {
  62.  
  63.     # Do a find-replace on our username and remove it from the file
  64.     (gc "$LOGPATH\$LOGFILE" -raw) | Foreach-Object {$_ -replace ($env:username+","), ""} | sc $LOGPATH\$LOGFILE -force
  65.  
  66.     # This is a workaround to using "set-content" since SC automatically inserts CR;LF (carriage return; line feed) which we don't want
  67.     # Basically read the file into a variable ("thing"), then crawl it and remove new line characters
  68.     $thing = Select-String -Pattern "\w" -Path $LOGPATH\$LOGFILE | foreach {$_.line}
  69.     # Now use the .net IO.file function to write it back out, WITHOUT inserting a CR;LF
  70.     [System.IO.file]::WriteAllText("$LOGPATH\$LOGFILE",$thing)
  71. }
  72.  
  73. # Flush the log file ("clear-content" aka clc)
  74. if ($args -eq "-flush") {
  75.     clc $LOGPATH\$LOGFILE
  76. }
Add Comment
Please, Sign In to add comment