windows 如何远程注销用户?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3867532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:22:15  来源:igfitidea点击:

How to remotely log off a user?

c#windows

提问by esac

How can I programatically log off a user remotely (from another machine) with C#? All I know is their username. This is done in an Active Directory environment where the account executing this would be an Administrator (Domain Admin). I assume that security would be handled. Would want to avoid having to install an application on the machine.

如何使用 C# 以编程方式远程(从另一台机器)注销用户?我只知道他们的用户名。这是在 Active Directory 环境中完成的,其中执行此操作的帐户将是管理员(域管理员)。我认为会处理安全问题。希望避免必须在机器上安装应用程序。

There does seem to be an API although I do not know what it uses, as "logoff.exe" provided with windows provides for this capability. In the end I can use this, but would prefer to avoid a Process.Start call and relying on it (plus it doesn't take the username, just the session id).

虽然我不知道它使用什么,但似乎确实有一个 API,因为 Windows 提供的“logoff.exe”提供了此功能。最后我可以使用它,但更愿意避免 Process.Start 调用并依赖它(而且它不需要用户名,只需要会话 ID)。

采纳答案by Scott Chamberlain

This is more terminal server side of user management but I really like using Cassia

这是用户管理的更多终端服务器端,但我真的很喜欢使用Cassia



Another option is do this

另一种选择是这样做

System.Diagnostics.Process.Start("shutdown.exe", String.Format(@"/l /f /m \{1}", remoteComputerName));

/lis logoff. /fis force. /m \\computernameis the name of the remote computer to do the operation on. If you are not on a domain and the user running the app does not have domain admin rights I can not guarantee the above command will work.

/l是注销。/f是力。/m \\computername是要对其进行操作的远程计算机的名称。如果您不在域中并且运行该应用程序的用户没有域管理员权限,我无法保证上述命令会起作用。



Third option: get PsExecthen run the shutdown command with it on the remote computer(shutdown.exe /l /f)

第三个选项:获取PsExec然后在远程计算机上使用它运行关机命令(shutdown.exe /l /f

回答by NekoIme

WMI class win32_operatingsystem wmi might be useful, more specifically win32shutdown method (has logoff flag too).

WMI 类 win32_operatingsystem wmi 可能有用,更具体地说是 win32shutdown 方法(也有注销标志)。

回答by NotMe

I believe you can do so through WMI. Here is a site with a regular script to do it: http://www.waynezim.com/2009/04/remote-shutdown-logoff-script-using-wmi/

我相信你可以通过 WMI 做到这一点。这是一个带有常规脚本的站点:http: //www.waynezim.com/2009/04/remote-shutdown-logoff-script-using-wmi/

And this link ( http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/59dd345d-df85-4128-8641-f01f01583194)

这个链接(http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/59dd345d-df85-4128-8641-f01f01583194

gives more information that is specific to C# plus a hint on how to make it work when the user is disconnected, but still logged in.

提供了特定于 C# 的更多信息,以及有关如何在用户断开连接但仍处于登录状态时使其工作的提示。

回答by Mitchell

This is my very simple solution. This will remotely logoff the user that is currently logged in. If you want to logoff specific users, query the session id's first, and change 'console' in the session id.

这是我非常简单的解决方案。这将远程注销当前登录的用户。如果要注销特定用户,请先查询会话 ID,然后更改会话 ID 中的“控制台”。

private void runRemoteApp(string[] processToRun, string machineName)
{
    var connection = new ConnectionOptions();
    var wmiScope = new ManagementScope(String.Format("\\{0}\root\cimv2", machineName), connection);
    var wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
    wmiProcess.InvokeMethod("Create", processToRun);
}

private void logoffMachine(string machineName)
{
    var processToRun = new[] { "logoff.exe console" };
    runRemoteApp(processToRun, machineName);
}

回答by goenning

Take a look at this ServerRemoteControl.

看看这个ServerRemoteControl