Powershell 注销远程会话 [英] Powershell Log Off Remote Session
问题描述
我正在尝试制定一个 Powershell 命令来远程注销用户.我们有一个终端服务器,其程序非常不稳定,有时会锁定会话.我们必须远程注销用户,但我正在尝试编写一个 Powershell 语句,该语句将注销运行脚本的人.我搜索了一下,找到了这个命令:
I am trying to formulate a Powershell command to remotely log off a user. We have a terminal server with a very unstable program that sometimes locks sessions. We have to remotely log off a user but I'm trying to write a Powershell statement that will log off the person who ran the script. I Googled around, and found this command:
Invoke-Command -ComputerName MyServer -Command {shutdown -l}
但是,该命令返回不正确的功能".我可以成功运行括号内的其他命令,例如Get-Process.
However, the command returns "incorrect function." I can run other commands successfully in the brackets, such as Get-Process.
我的想法是将其放入一个脚本中,用户可以运行该脚本以从服务器注销(因为当它锁定时,他们无法访问开始菜单或 ALT+CTRL+END 以通过 GUI 执行此操作).
The idea is for me to put that into a script that users can run to log themselves off of the server (since when it locks, they cannot access the start menu or ALT+CTRL+END to do it through the GUI).
流程是这样的:Bob 通过 RDP 登录 MyServer,但他的会话冻结.在他的本地桌面上,他可以运行 MyScript(包含与上述类似的命令),这将注销他在 MyServer 上的会话.
The flow would be this: Bob logs into MyServer via RDP but his session freezes. On his local desktop, he can run MyScript (containing a command similar to above) which will log off his session on MyServer.
推荐答案
也许令人惊讶的是,您可以使用 logoff
命令注销用户.
Perhaps surprisingly you can logoff users with the logoff
command.
C:\> logoff /?
Terminates a session.
LOGOFF [sessionname | sessionid] [/SERVER:servername] [/V] [/VM]
sessionname The name of the session.
sessionid The ID of the session.
/SERVER:servername Specifies the Remote Desktop server containing the user
session to log off (default is current).
/V Displays information about the actions performed.
/VM Logs off a session on server or within virtual machine.
The unique ID of the session needs to be specified.
会话 ID 可以通过 qwinsta
(query session
) 或 quser
(query user
) 确定命令(请参阅此处):
The session ID can be determined with the qwinsta
(query session
) or quser
(query user
) commands (see here):
$server = 'MyServer'
$username = $env:USERNAME
$session = ((quser /server:$server | ? { $_ -match $username }) -split ' +')[2]
logoff $session /server:$server
这篇关于Powershell 注销远程会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!