检测谁登录了远程Windows计算机


9

您如何找出谁登录了远程Windows计算机?

我目前正在使用psloggedon,但一次只能给我一台计算机。

psloggeon \\ 172.21.0.5

psloggedon

有没有更好的方法来扫描整个子网?最好是一些GUI应用程序。

Answers:


9

不是GUI,而是:

for /L %x in (2,1,254) do psloggedon \\172.21.0.%x

将从172.21.0.2-254进行扫描。您还可以嵌套:

for /L %z in (16,1,31) do for /L %x in (1,1,254)  do psloggedon \\172.21.%y.%x

这将扫描172.21。{16-31} .x子网。


很好,但是问题是它尝试每个IP地址。如果主机未处于活动状态,则它会在该主机上悬挂30秒左右,然后再继续运行。输出也有些混乱。
金德里奇2009年

您可以对计算机执行ping操作(ping xxxx -n 1)并检查其输出,这样一来,您在任何计算机上的超时都是ping的一秒钟
benPearce

8

我找到了这个脚本。它扫描整个域,并为您提供良好的输出(计算机名和用户名)。

whoisloggedinwhere.bat> users.txt

@回声关闭
setlocal
在('net view / domain:“%USERDOMAIN%” ^ | Findstr / L / C:“ \\”')中为/ f“ Tokens = 1” %% c做(
 在('PsLoggedOn -L %% c ^ | find / i“%USERDOMAIN%\”')中为/ f“ Tokens = *” %% u做(
  致电:report %% c“ %% u”
 )
)
本地
转到:EOF
:报告
设置工作=%1
设定comp =%work:〜2%
设置用户=%2
设置用户=%用户:“ =%
通话设置用户= %% user:*%USERDOMAIN%\ = %%
@echo%comp%%user%

该脚本使用PsLoggedOn




1

如果服务器正在运行终端服务,则可以使用终端服务管理器来查看域中的服务器以及谁登录到它们。它是GUI,可以在下面找到

Start -> Administrative Tools -> Terminal Services Manager

1

qwinsta是另一个dos命令,但一次只会给您一个...

C:\>qwinsta /server:test_srv
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
 console           test_usr                  0  Active  wdcon
 rdp-tcp                                 65536  Listen  rdpwd

1

通过以下PowerShell脚本查询WMI,可以检测到本地登录到工作站的用户。它返回本地登录者的名称或空字符串。

function logged_in($host_name) {
    (get-wmiobject -class Win32_ComputerSystem -computername $host_name `
        -namespace "root\CIMV2").UserName
}

0

我不确定我从哪里得到的,但是我周围放着这段代码,向用户显示了计算机上的信息。您可以将其包装在for每个循环中以扫描一堆机器。我想说的是,如果您想知道谁登录了系统,最简单的方法是打开登录审核并查看(或查询)安全日志。以下代码可查看谁在任何时刻都在工作:

' PARAMETERS
'
strComputer = "machineName"   ' use "." for local computer 
strUser = "domain\user" ' comment this line for current user
strPassword = "password" ' comment this line for current user

' CONSTANTS
'
wbemImpersonationLevelImpersonate = 3
wbemAuthenticationLevelPktPrivacy = 6

'=======================================================================
' MAIN
'=======================================================================

' Connect to machine
'
If Not strUser = "" Then

    ' Connect using user and password
    '
    Set objLocator = CreateObject("WbemScripting.SWbemLocator")
    Set objWMI = objLocator.ConnectServer _
        (strComputer, "root\cimv2", strUser, strPassword)
    objWMI.Security_.ImpersonationLevel = wbemImpersonationLevelImpersonate
    objWMI.Security_.AuthenticationLevel = wbemAuthenticationLevelPktPrivacy

Else

    ' Connect using current user
    '
    Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

End If

' Get OS name
'
Set colOS = objWMI.InstancesOf ("Win32_OperatingSystem")

For Each objOS in colOS
    strName = objOS.Name
Next

If Instr(strName, "Windows 2000") > 0 Then

    '-------------------------------------------------------------------
    ' Code for Windows 2000
    '-------------------------------------------------------------------

    ' Get user name
    '
    Set colComputer = objWMI.ExecQuery("Select * from Win32_ComputerSystem")

    For Each objComputer in colComputer
        Wscript.Echo "User: " & objComputer.UserName
    Next

    ' ------------------------------------------------------------------

Else

    ' ------------------------------------------------------------------
    ' Code for Windows XP or later
    ' ------------------------------------------------------------------

    ' Get interactive session
    '
    Set colSessions = objWMI.ExecQuery _ 
          ("Select * from Win32_LogonSession Where LogonType = 2") 

    If colSessions.Count = 0 Then 
        ' No interactive session found
        '
        Wscript.Echo "No interactive user found" 
    Else 
        'Interactive session found
        '
        For Each objSession in colSessions 

            Set colList = objWMI.ExecQuery("Associators of " _ 
            & "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " _ 
            & "Where AssocClass=Win32_LoggedOnUser Role=Dependent" ) 

            ' Show user info
            '
            For Each objItem in colList 
                WScript.Echo "User: " & objItem.Name 
                WScript.Echo "FullName: " & objItem.FullName 
                WScript.Echo "Domain: " & objItem.Domain 
            Next 

            ' Show session start time
            '
            Wscript.Echo "Start Time: " & objSession.StartTime 
        Next 
    End If 

    ' ------------------------------------------------------------------

End If

'=======================================================================

0

令我惊讶的是,我已经使用了很多年了,还没有人提到logon2。这是您要求的GUI实现,可在此处获得


当我在Windows 7上运行此命令时,出现错误:错误-退出。找不到ServerBrowseDialogA0的入口点。也许它不能在Win7上运行?您是否需要管理员权限才能正确运行它?
史蒂夫

@Steve,现在我知道为什么没有人发布它了。没错,它在Win 7上不起作用,至少不是64位版本。我将查看是否可以从XP计算机中复制所需的DLL并使其正常工作。如果我有任何成功,我会通知您。
John Gardeniers
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.