Powershell Log Off Remote Session

前端 未结 11 1209
情话喂你
情话喂你 2020-12-02 13:15

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 re

11条回答
  •  無奈伤痛
    2020-12-02 14:15

    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.

    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
    

提交回复
热议问题