Re: Username of who is logged into workstation via remote desktop
- From: "Mike Walraven" <MikeWalraven@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 8 Aug 2005 21:39:04 -0700
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
"Torgeir Bakken (MVP)" wrote:
> 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
>
.
- References:
- Username of who is logged into workstation via remote desktop
- From: Mike Walraven
- Re: Username of who is logged into workstation via remote desktop
- From: Torgeir Bakken \(MVP\)
- Username of who is logged into workstation via remote desktop
- Prev by Date: Please help me need answers to these .NET Interview questions
- Next by Date: Re: Loop to check process
- Previous by thread: Re: Username of who is logged into workstation via remote desktop
- Next by thread: CDO server side mailing
- Index(es):
Relevant Pages
|