Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Purpose: Stamps usernames to a file as users log on, then removes them when they log off.
- # Clears this file at each boot/reboot. Purpose is to keep a comma-separated file
- # listing all users logged on to the system so we can query it with other text-based tools.
- # Requirements: 1. Write-access to the log location
- # Author: vocatus on reddit.com/r/sysadmin and /r/usefulscripts
- # History: 1.0 Initial write
- # Usage: Call the script with one of the following flags:
- # -logon Add username to the list of current users
- # -logoff Remove username from the list of current users
- # -flush Flush all usernames (use when rebooting/booting)
- ########
- # Prep #
- ########
- $VERSION="1.0"
- $UPDATED="2014-01-21"
- $CUR_DATE=get-date -f "yyyy-MM-dd"
- #############
- # VARIABLES # -- Set these to your desired values
- #############
- # Rules for variables:
- # * Quotes are required (e.g.: "c:\directory\path" )
- # * NO trailing slashes on paths! (bad: "c:\directory\" )
- # * Spaces are okay (okay: "c:\my folder\with spaces" )
- # * Network paths are okay (okay: "\\server\share name" )
- # ( "\\172.16.1.5\share name" )
- # Where to keep the file containing the list of logged-on users? Regular users must have write-access to this location
- $LOGPATH=$env:systemdrive + "\Logs"
- $LOGFILE="logged_on_users"
- #############
- # EXECUTION #
- #############
- # If no argument passed, spit out an error and die
- if (! $args) {
- write-host ""
- write-host "ERROR: No action specified" -f red
- write-host ""
- write-host "Actions are: "
- write-host " -logon Stamp username to" $LOGPATH\$LOGFILE -f gray
- write-host " -logoff Remove username from" $LOGPATH\$LOGFILE -f gray
- write-host " -flush Flush all usernames from" $LOGPATH\$LOGFILE -f gray
- write-host ""
- write-host " e.g. .\logged_on_users.ps1 -logon" -f gray
- write-host ""
- break
- }
- # Add this user to the list of current users
- # Direct call to .net framework since PowerShell doesn't have an easy way to write to a file without a new-line
- if ($args -eq "-logon") {
- [System.IO.File]::AppendAllText("$LOGPATH\$LOGFILE", $env:username + ",")
- }
- # Remove user from list of current users
- if ($args -eq "-logoff") {
- # Do a find-replace on our username and remove it from the file
- (gc "$LOGPATH\$LOGFILE" -raw) | Foreach-Object {$_ -replace ($env:username+","), ""} | sc $LOGPATH\$LOGFILE -force
- # This is a workaround to using "set-content" since SC automatically inserts CR;LF (carriage return; line feed) which we don't want
- # Basically read the file into a variable ("thing"), then crawl it and remove new line characters
- $thing = Select-String -Pattern "\w" -Path $LOGPATH\$LOGFILE | foreach {$_.line}
- # Now use the .net IO.file function to write it back out, WITHOUT inserting a CR;LF
- [System.IO.file]::WriteAllText("$LOGPATH\$LOGFILE",$thing)
- }
- # Flush the log file ("clear-content" aka clc)
- if ($args -eq "-flush") {
- clc $LOGPATH\$LOGFILE
- }
Add Comment
Please, Sign In to add comment