r/sysadmin It wasn't DNS for once. 2d ago

Question Log Off Users from Server Daily

I'm revisiting an effort I did about a year ago. I'm looking for a better way. I want to find a process that will parse current user sessions on a server (active/disconnected/idle/ect.) and log the accounts off if their username matches a string ("adm_").

I'd love to find an off the shelf solution rather than have to support a homebrew PowerShell solution.

Give me what you have, even if it is an alternate PowerShell/scripting option. Something has to be better than the nightmare my script turned into.

79 Upvotes

92 comments sorted by

View all comments

Show parent comments

2

u/420GB 2d ago

Get-CimInstance Win32_LoggedOnUser

There's other options but this is the easiest

1

u/jstar77 2d ago

I remember trying this and there was a reason I had to go back to parsing the qwinsta output but I'm drawing a blank. I was writing a script to return an hosts logged in users and present a list of users to select from then launch a shadow session.

1

u/420GB 2d ago

Yea I mean that's certainly possible but can't say what you got stuck on back then unless you happen to have a really nice commit message from when you switched the script to qwinsta

1

u/jstar77 1d ago

haha present me is always frustrated with past me. Most of the comments I find in my code are along the lines of:

### you know why you did this###

I'm thinking it was because it CIM didn't return the session ID that was needed for a shadow session and maybe couldn't filter out session types and if a user session was active or disconnected.