Advertisement
ridersofgavony

Logoff Disconnected and Idle Users (edited)

Oct 19th, 2022 (edited)
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # VARIABLES
  2. $logFile = "C:\temp\Baseline_DisconnectedSessions.csv"
  3. $sessions = $null
  4. # DATATABLE
  5. $sessionsTable = New-Object System.Data.DataTable("Sessions")
  6. # DataTable "Sessions" Schema (columns)
  7. $stringCols = @("USERNAME","ID","STATE","IDLETIME","LOGONTIME","FlagLOGOFF","LOGOFFSUCCESS","TimeStamp")
  8. foreach ($col in $stringCols) {[void]$sessionsTable.Columns.Add($col,"string")} # These columns are type=string
  9. # START MAIN
  10. $sessions = (query user 2>&1 | ForEach-Object{ "$_" }) # Get user session info and suppress nativecommand errors
  11. foreach ($sesh in $sessions){
  12.     $trimmedSession = (($sesh -replace '^>', '') -replace '\s{2,}', ',').Trim() # Clean up the string
  13.     if ($trimmedSession.Split(',').Count -eq 5) {
  14.         # if the string is split into 5 chunks by commas do the following work
  15.         ($trimmedSession -replace '(^[^,]+)', '$1,') | Out-Null
  16.         $row = $sessionsTable.NewRow()
  17.         $row.USERNAME,$row.ID,$row.STATE,$row.IDLETIME,$row.LOGONTIME = $trimmedSession.split(',') # Chunk up the string and set each chunk to the appropriate field in the table
  18.         if (($row.IDLETIME -gt "0+00:40") -AND ($row.State -eq "Disc")){
  19.             $row.FlagLOGOFF = $true # if the IDLETIME is greater than 40 minutes and the STATE is DISC set this field to TRUE
  20.         } else {
  21.             $row.FlagLOGOFF = $false # else set this field to FALSE
  22.         }
  23.         $sessionsTable.rows.Add($row)
  24.     } else { CONTINUE } # if the string is not split into 5 chunks by commas move on to next $sesh in the foreach
  25. }
  26. $sessions = $sesh = $trimmedSession = $null # nulling out some variables, just in case
  27. foreach ($row in $sessionsTable){
  28.     if ($row.FlagLOGOFF -eq "TRUE"){
  29.         # read the table, if the field FlagLOGOFF is set to true do the following work
  30.         $dateTime = Get-Date
  31.         $row.TimeStamp = $dateTime # set the TIMESTAMP field to the current date and time
  32.         logoff $row.ID # Log off the user
  33.         if ((query user $row.Username 2>&1 | ForEach-Object{ "$_" }) -like "No User exists for*"){
  34.             $row.LOGOFFSUCCESS = $true # check for the user, if there is no session we succeeded, set the LOGOFFSUCCESS field to TRUE
  35.         } else {
  36.             $row.LOGOFFSUCCESS = $false # check for the user, if there is a session we failed, set the LOGOFFSUCCESS field to FALSE
  37.         }
  38.         $sessionsTable | Export-Csv -NoTypeInformation $logFile # write the table to the log
  39.         Set-Content $logFile ((Get-Content $logFile) -replace '"') # remove quotes from the log because I hate them
  40.     }
  41. }
  42. $dateTime = $null # nulling out some variables, just in case
  43. # END SCRIPT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement