Arikkan
asked on
Using VB.Net or C#.Net, - List active user logged into a machine and kill the session
We would like to build a simple program that lists users that are logged into the machine and by selecting the item on the list, we want to kill (log off) the session.
Could you please help in
1. code to get list of active session
2. code to log off the specific session (possibly by seeding ID from the #1 above)
(This is somewhat like what we can do from the Task Manager by selecting the user session and "Sign off")
Could you please help in
1. code to get list of active session
2. code to log off the specific session (possibly by seeding ID from the #1 above)
(This is somewhat like what we can do from the Task Manager by selecting the user session and "Sign off")
ASKER
Thank you very much, but we wish to handle them within .Net. Is there a way to do this without Powershell?
ASKER
Thank you but the first link is linking to a site that does not exist.
The second one may be ok with getting a list, but there is no way for terminating the session even we get a list...
The second one may be ok with getting a list, but there is no way for terminating the session even we get a list...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Andrei,
This code currently disconnect user. Is there a way to log off user?
Thanks.
This code currently disconnect user. Is there a way to log off user?
Thanks.
ASKER
Nevermind, found that we can do this:
[DllImport("wtsapi32.dll", SetLastError = true)]
static extern bool WTSLogoffSession(IntPtr hServer, int SessionId, bool bWait);
[DllImport("wtsapi32.dll", SetLastError = true)]
static extern bool WTSLogoffSession(IntPtr hServer, int SessionId, bool bWait);
and to run PS code from .Net, have a look at https://www.emoreau.com/Entries/Articles/2018/06/Running-a-PowerShell-script-from-a-Net-application.aspx