且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

注销/注销或注销用户无法使用窗口服务

更新时间:2023-12-02 12:41:22

你不能这样做:服务不在用户帐户下运行 - 这就是你无法显示的原因给用户的任何东西:没有用户,没有用户界面。

所以 shutdown -l 查看服务运行的当前用户,不会找到一个,什么都不做。



它在调试中有效,因为调试服务是一种特殊情况,需要很多小心:如何:调试Windows服务应用程序 [ ^ ]



基本上,您无法直接强制用户从服务注销。如果你的servioce有管理员权利,你可以 - 理论上,我没有尝试过 - 做到这一点: c# - 从服务中注销Windows中的交互式用户 - Stack Overflow [ ^ ]

但要小心。并且在安装testign服务之前要么备份,要么使用VM。

并且不要将你的应用程序带到我的电脑附近! :laugh:
You can't do it: the service does not run under a user account - which is why you can't display anything to a user: no user, no user interface.
So shutdown -l looks at the current user that the service operates as, doesn't find one, and does nothing.

It works in debug because debugging a service is a special case, with a lot of care needed: How to: Debug Windows Service Applications[^]

Basically, you can't force a user logoff from a service directly. If your servioce has admin rights however you can - in theory, I've not tried it - do it: c# - Logoff interactive users in Windows from a service - Stack Overflow[^]
But be careful. And either backup really well before you install the service for testign, or use a VM.
And don't bring your app anywhere near my PC! :laugh:


A service running as local system has the permission to do this.. 
Your task involves finding a list of logged on users : CMD
<pre>query user



然后输出如下内容:




Then that outputs something like this:

PS C:\Users\Athos7> query user
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
>Athos7            console             2  Active      none   7/24/2017 2:50 PM
PS C:\Users\Athos7>





然后您可以使用ID注销该会话:





Then you can just log off that session using the id:

logoff 2





您还可以使用findstr来获取具有匹配用户ID的行







这家伙有一个非常好的PS脚本,可以将属性转换为对象。你可以转换它或从你的服务中调用它。

Quick-Hits:Find目前登录用户|学习Powershell |实现更多 [ ^ ]





好​​运



You can also use findstr to only get the line with matching users ID


Or
This guy has a pretty nice PS script to turn the properties into an object. and you could either convert it or call it from your service.
Quick-Hits: Find currently logged on users | Learn Powershell | Achieve More[^]


Good Luck