以编程方式远程注销活动域用户

发布于 2024-07-07 06:05:55 字数 199 浏览 10 评论 0原文

我需要能够从程序中注销任何用户的 Windows 会话。

我知道我可以以管理员身份登录并强制远程注销。 有没有其他方法可以在不登录的情况下强制注销?

该工具将以管理员身份运行,因此这不是问题,无需登录即可远程注销才是问题。

工具采用 .NET,但欢迎任何其他方式(JScript、从 PInvoke 运行的命令行工具等)

I need to be able to logoff any user from his windows session from a program.

I know I could log in as an admin and force a remote logoff. Is there any other way to force a logoff without logging in?

The tool will run as admin so that's not a problem, being able to remote logoff without logging in is.

Tool is in .NET, but any other way is welcome (JScript, command line tool to run from PInvoke, etc.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梨涡少年 2024-07-14 06:05:55

也许与 Sysinternals 的 PsTools 一起使用,特别是 PsShutdown

Perhaps with Sysinternals' PsTools, particularly PsShutdown?

云淡月浅 2024-07-14 06:05:55

终于在这个页面找到了这个脚本: http://www.robvanderwoude.com/files/logoff_vbs .txt

我决定将其作为字符串存储在工具内,并将其写入磁盘,执行它并删除它。 不是特别优雅(是的,是的,丑得要命),但目前已经足够好了。

' Logoff.vbs, Version 1.00
' Logoff current user on any WMI enabled computer on the network
'
' Adapted from posts by Alex Angelopoulos on www.developersdex.com
' and Michael Harris on microsoft.public.scripting.vbscript
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

' Check command line parameters
Select Case WScript.Arguments.Count
    Case 0
        ' Default is local computer if none specified
        strComputer = "."
    Case 1
        Select Case WScript.Arguments(0)
            ' "?", "-?" or "/?" invoke online help
            Case "?"
                Syntax
            Case "-?"
                Syntax
            Case "/?"
                Syntax
            Case Else
                strComputer = WScript.Arguments(0)
        End Select
    Case Else
        ' More than 1 argument is not allowed
        Syntax
End Select

' Define some constants that can be used in this script;
' logoff = 0 (no forced close of applications) or 5 (forced);
' 5 works OK in Windows 2000, but may result in power off in XP
Const EWX_LOGOFF   = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT   = 2
Const EWX_FORCE    = 4
Const EWX_POWEROFF = 8

' Connect to computer
Set OpSysSet = GetObject("winmgmts:{(Shutdown)}//" & strComputer & "/root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")

' Actual logoff
for each OpSys in OpSysSet
    OpSys.Win32Shutdown EWX_LOGOFF
next

' Done
WScript.Quit(0)


Sub Syntax
msg = vbCrLf & "Logoff.vbs,  Version 1.00" & vbCrLf & _
      "Logoff the current user of any WMI enabled computer on the network." & _
      vbCrLf & vbCrLf & "Usage:  CSCRIPT  LOGOFF.VBS  [ computer_name ]" & _
      vbCrLf & vbCrLf & _
      "Where:  " & Chr(34) & "computer_name" & Chr(34) & _
      "  is the name of the computer to be logged off" & vbCrLf & _
      "                         (without leading backslashes); default is " & _
      Chr(34) & "." & Chr(34) & vbCrLf & _
      "                         (the local computer)." & vbCrLf & vbCrLf & _
      "Written by Rob van der Woude" & vbCrLf & _
      "http://www.robvanderwoude.com" & vbCrLf & vbCrLf & _
      "Based on posts by Alex Angelopoulos on www.developersdex.com" & _
      vbCrLf & _
      "and Michael Harris on microsoft.public.scripting.vbscript" & vbCrLf
Wscript.Echo(msg)
Wscript.Quit(1)
End Sub

Finally found out this script in this page: http://www.robvanderwoude.com/files/logoff_vbs.txt

I decided to store it as a string inside the tool and make it write it to disk, execute it and delete it. Not particularly elegant (yes, yes, ugly as hell), but good enough for now.

' Logoff.vbs, Version 1.00
' Logoff current user on any WMI enabled computer on the network
'
' Adapted from posts by Alex Angelopoulos on www.developersdex.com
' and Michael Harris on microsoft.public.scripting.vbscript
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

' Check command line parameters
Select Case WScript.Arguments.Count
    Case 0
        ' Default is local computer if none specified
        strComputer = "."
    Case 1
        Select Case WScript.Arguments(0)
            ' "?", "-?" or "/?" invoke online help
            Case "?"
                Syntax
            Case "-?"
                Syntax
            Case "/?"
                Syntax
            Case Else
                strComputer = WScript.Arguments(0)
        End Select
    Case Else
        ' More than 1 argument is not allowed
        Syntax
End Select

' Define some constants that can be used in this script;
' logoff = 0 (no forced close of applications) or 5 (forced);
' 5 works OK in Windows 2000, but may result in power off in XP
Const EWX_LOGOFF   = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT   = 2
Const EWX_FORCE    = 4
Const EWX_POWEROFF = 8

' Connect to computer
Set OpSysSet = GetObject("winmgmts:{(Shutdown)}//" & strComputer & "/root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")

' Actual logoff
for each OpSys in OpSysSet
    OpSys.Win32Shutdown EWX_LOGOFF
next

' Done
WScript.Quit(0)


Sub Syntax
msg = vbCrLf & "Logoff.vbs,  Version 1.00" & vbCrLf & _
      "Logoff the current user of any WMI enabled computer on the network." & _
      vbCrLf & vbCrLf & "Usage:  CSCRIPT  LOGOFF.VBS  [ computer_name ]" & _
      vbCrLf & vbCrLf & _
      "Where:  " & Chr(34) & "computer_name" & Chr(34) & _
      "  is the name of the computer to be logged off" & vbCrLf & _
      "                         (without leading backslashes); default is " & _
      Chr(34) & "." & Chr(34) & vbCrLf & _
      "                         (the local computer)." & vbCrLf & vbCrLf & _
      "Written by Rob van der Woude" & vbCrLf & _
      "http://www.robvanderwoude.com" & vbCrLf & vbCrLf & _
      "Based on posts by Alex Angelopoulos on www.developersdex.com" & _
      vbCrLf & _
      "and Michael Harris on microsoft.public.scripting.vbscript" & vbCrLf
Wscript.Echo(msg)
Wscript.Quit(1)
End Sub
聚集的泪 2024-07-14 06:05:55

您可以使用简单的命令行从任何远程用户强制注销,如下所示

C:\>psexec \\remotepc shutdown /f /l

You may use simple command line to logoff forcefully from any remote user as below

C:\>psexec \\remotepc shutdown /f /l
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文