我试图制定一个Powershell命令来远程注销用户。我们的终端服务器的程序非常不稳定,有时会锁定 session 。我们必须远程注销用户,但是我试图编写一个Powershell语句来注销运行脚本的人。我到处搜索,发现以下命令:

Invoke-Command -ComputerName MyServer -Command {shutdown -l}

但是,该命令返回“功能不正确”。我可以在方括号中成功运行其他命令,例如Get-Process。

我的想法是将其放入一个脚本中,用户可以运行该脚本以使自己从服务器注销(由于锁定后,他们无法通过GUI访问开始菜单或ALT + CTRL + END来执行此操作)。

流程如下:
Bob通过RDP登录MyServer,但是他的 session 冻结。在他的本地桌面上,他可以运行MyScript(包含与上面类似的命令),这将注销他在MyServer上的 session 。

最佳答案

也许令人惊讶的是,您可以使用logoff命令注销用户。

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.

可以使用qwinsta(query session)或quser(query user)命令确定 session ID(请参阅here):
$server   = 'MyServer'
$username = $env:USERNAME

$session = ((quser /server:$server | ? { $_ -match $username }) -split ' +')[2]

logoff $session /server:$server

10-07 19:00
查看更多