Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Username of who is logged into workstation via remote desktop

1,225 views
Skip to first unread message

Mike Walraven

unread,
Aug 8, 2005, 12:59:27 PM8/8/05
to
Thanks in advance for any assistance.

I am using a WMI script to return who is logged into a remote computer.
(Sample code for workstation MT_LINCOLN is below.) However, the
objComputer.UserName only returns users logged into the console, not users
logged in via remote desktop. Is there a way to query for both?

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

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

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

Regards,
Mike Walraven

Marty List

unread,
Aug 8, 2005, 2:27:56 PM8/8/05
to

Try enumerating the instances of Win32_LogonSession, check the LogonType
property for 10 (or possibly 12):
http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_logonsession.asp

Something similar to this:
"Select * from Win32_LogonSession Where LogonType = 10"

"Mike Walraven" <MikeWa...@discussions.microsoft.com> wrote in message
news:34C88F48-6A9A-44C7...@microsoft.com...

Torgeir Bakken (MVP)

unread,
Aug 8, 2005, 2:29:33 PM8/8/05
to
Mike Walraven wrote:

> Thanks in advance for any assistance.
>
> I am using a WMI script to return who is logged into a remote computer.
> (Sample code for workstation MT_LINCOLN is below.) However, the
> objComputer.UserName only returns users logged into the console, not users
> logged in via remote desktop. Is there a way to query for both?
>

Hi,

Assuming WinXP or Win2k3 Server:


Run the VBScript below in a command prompt (cmd.exe) with cscript.exe,
like this:

cscript.exe "c:\some path\some file.vbs"


'--------------------8<----------------------

strComputer = "." ' " use "." for local computer

Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _


& strComputer & "\root\cimv2")

Set colSessions = objWMI.ExecQuery _
("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10")

If colSessions.Count = 0 Then
Wscript.Echo "No interactive users found"
Else
For Each objSession in colSessions
If objSession.LogonType = 2 Then
WScript.Echo "Logon type: Console"
Else
WScript.Echo "Logon type: RDP/Terminal Server"
End If
Set colList = objWMI.ExecQuery("Associators of " _
& "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " _
& "Where AssocClass=Win32_LoggedOnUser Role=Dependent" )

For Each objItem in colList
WScript.Echo "User: " & objItem.Name
WScript.Echo "FullName: " & objItem.FullName
WScript.Echo "Domain: " & objItem.Domain
Next
Wscript.Echo "Session start time: " & objSession.StartTime
WScript.Echo
Next
End If

'--------------------8<----------------------


Alternatively:

To obtain a list of logged on users, an option is to parse the output
of Qwinsta.exe (comes builtin with WinXP and Win2k3 Server).

QWINSTA.EXE /SERVER:MT-LINCOLN

Look for lines with the text "RDP-Tcp#" in it, it will indicate a RDP
session and the same line will also contain the user account name.
The session name "console" will give you info about who is logged
on at the console.


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

Torgeir Bakken (MVP)

unread,
Aug 8, 2005, 2:34:37 PM8/8/05
to
Marty List wrote:

> Try enumerating the instances of Win32_LogonSession, check the LogonType
> property for 10 (or possibly 12):
> http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_logonsession.asp
>
> Something similar to this:
> "Select * from Win32_LogonSession Where LogonType = 10"
>
>

Hi,

10 for RDP/Terminal server sessions should be sufficient.

As the OP wanted both RDP and console users:

"Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10"

Mike Walraven

unread,
Aug 8, 2005, 3:36:02 PM8/8/05
to
Torgeir,

Thanks for this. It works quite well although it returns several RDP
sessions - I assume the ones that have happened since the workstation was
booted or maybe logged into the console.

Do you have further suggestions for this so that I only get whoever is
currently logged into either the console or RDP?

Another suggestion I had was:
strComputer = "MT-LINCOLN"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

set objEnum = objWMIService.execQuery("select __relpath from win32_process
where caption = 'explorer.exe'")

' for testing purposes
WScript.Echo " " & strComputer & " was contacted. Attempting to see
who is logged in."

for each obj in objEnum
set outParams = obj.ExecMethod_("GetOwner")
If outParams.User <> "" Then
wscript.echo "User: " & outParams.User & " is currently logged in on
computer " & strComputer & "." & VbCrLf
Else
WScript.Echo "No one is currently logged in on computer " & strComputer &
"." & VbCrLf
End If
next

However, the IF outParams.User <> "" statement never returns the echo "No
one is currently logged...." statement if no one is logged in. Other than
that, this suggestions seems to work well.

Assistance with either script is greatly appreciated.

Regards,
Mike Walraven

Mike Walraven

unread,
Aug 9, 2005, 12:39:04 AM8/9/05
to
Torgeir,

Due to the multiple replies that I got using the script you sent, I have
settled upon the following which seems to work quite well. Thanks for all
your help.

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

set objEnum = objWMIService.execQuery_


("select __relpath from win32_process where caption = 'explorer.exe'")

' for testing purposes
' WScript.Echo " " & strComputer &_


" was contacted. Attempting to see who is logged in."

If objEnum.Count = 0 then
wscript.echo "No one currently logged in to " & strComputer & "." & VbCrLf
Else


for each obj in objEnum
set outParams = obj.ExecMethod_("GetOwner")

wscript.echo "User: " & outParams.User &_


" is currently logged in on computer " & strComputer & "." & VbCrLf

next
End If


Regards,
Mike Walraven

0 new messages