Note that there are some explanatory texts on larger screens.

plurals
  1. POLogoff interactive users in Windows from a service
    text
    copied!<p>I'm trying to figure out a way to log off users in local Windows sessions from a Windows Service written in C#.</p> <p>Here's the background to the problem: I need to manage the computer usage time of a set of users; when their allotted time expires I want to log them off. This is in the context of a W2K8 domain. Unfortunately the login time controls in Windows simply disconnect the user from server resources; there is no way to force their sessions to terminate via this method.</p> <p>My approach is to build a Windows Service that I will deploy across the domain; the service will run on every client computer. At regular intervals the service will enumerate logged-in users on the computer, call out to a database to add the logged-in time since the last call to the total for the day, and if they have reached their maximum, log them out (with a five minute warning). Note - these are NOT terminal services sessions, they are regular local interactive logons. Note also that there may be multiple logons on a machine due to the "switch user" functionality in Win7 &amp; Vista. All my client PCs will be running Win7. The Windows Service will be running as Local System so privileges should not be a problem.</p> <p>I can successfully construct a list of logged-in users on the machine by user name using WMI. Here's a snippet of that code:</p> <hr> <pre><code> List&lt;string&gt; loggedInUsers = new List&lt;string&gt;(); ManagementClass mc = new ManagementClass("Win32_Process"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { ROOT.CIMV2.Process process = new ROOT.CIMV2.Process(mo); string domain, user; uint pid; process.GetOwner(out domain, out user); pid = process.ProcessId; if (process.Name.Trim().ToLower() == "explorer.exe") loggedInUsers.Add(user); } return loggedInUsers; </code></pre> <hr> <p>However, I am struggling to find a method that will allow me to log off a selected user's session. I know I can shut down the machine, but I don't want that - that will kill all users' sessions.</p> <p>Any ideas anyone? Thanks for reading this lengthy post!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload