且构网

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

Powershell 注销远程会话

更新时间:2023-10-21 16:50:58

也许令人惊讶的是,您可以使用 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