Find User Logged on to a Remote Computer

2019-01-15 22:33发布

I have the following script. It should find the user who is currently logged into remote computer, but it doesn't. What am I missing? The remote computer is a part of a domain. Would I see to add the domain? How?

strComputer = "computername"
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")

For Each objComputer in colComputer
    Wscript.Echo "Logged-on user: " & objComputer.UserName
Next

标签: vbscript wmi
1条回答
贼婆χ
2楼-- · 2019-01-15 23:17

As documented the UserName property returns the name of the user logged in on the console:

UserName
[...]
Name of a user that is logged on currently. This property must have a value. In a terminal services session, UserName returns the name of the user that is logged on to the console—not the user logged on during the terminal service session.

If you don't get a result your user is most likely logged in via Remote Desktop.

To get all users logged in on the remote system check the running Explorer processes for their owners:

strComputer = "computername"
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

qry = "SELECT * FROM Win32_Process WHERE Name='explorer.exe'")
For Each p in objWMIService.ExecQuery(qry)
    p.GetOwner user, domain
    WScript.Echo "Logged-on user: " & domain & "\" & user
Next
查看更多
登录 后发表回答