Is User Logged in via Remote Desktop or Console?

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Is User Logged in via Remote Desktop or Console?

Post by Marcus Tettmar » Sun Mar 19, 2006 10:56 pm

Code to determine whether the active user is logged in via Remote Desktop or locally.

Code: Select all

VBSTART
Function UserType(sHost)

   If sHost = "" Then
     Exit Function  '-----> return Empty
   End if

   Set oShell = CreateObject("Wscript.Shell")
   Set oFS = CreateObject("Scripting.FileSystemObject")

   sTmpFile = oFS.GetSpecialFolder(2).ShortPath & "\" & oFS.GetTempName

   'Run command and redirect stdout and stderr into temp file
   oShell.Run "%ComSpec% /c %SystemRoot%\System32\QWINSTA.EXE /SERVER:" _
      & sHost & " >" & sTmpFile & " 2>&1", 0, True

   On Error Resume Next
   'Open the temp file
   Set oTF = oFS.OpenTextFile(sTmpFile)

   'Parse the file

   ' Read first line
   sLine = oTF.ReadLine
   If Err.Number <> 0 Then
     ' Something is wrong.
     Exit Function  '-----> return Empty
   End If
   On Error Goto 0

   If Left(Trim(sLine), 26) <> "SESSIONNAME       USERNAME" Then
     ' Something is wrong. Most likely is the content of the first line
     ' this: "Error opening Terminal server <host name>"
     Exit Function  '-----> return Empty
   End If

   'Read second line
   sLine = oTF.ReadLine
   'Close file
   oTF.Close
   'Delete it
   oFS.DeleteFile sTmpFile

   sChoppedLine = Mid(sLine, 20)
   If Left(sChoppedLine, 1) = " " Then
     'no user found
     GetUser = ""
   Else
	 if Trim(Left(sLine,8)) = "console" then
	   UserType = "Console"
	 else
	   UserType = "RDP"
	 end if
   End If

End Function
VBEND
VBEval>UserType("."),type
If>type=RDP
  MessageModal>Remote User
Else
  MessageModal>Console User
Endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

SupermanInNY
Newbie
Posts: 2
Joined: Thu Jan 24, 2013 12:00 am

Re: Is User Logged in via Remote Desktop or Console?

Post by SupermanInNY » Thu Jan 24, 2013 12:07 am

Marcus Tettmar wrote:Code to determine whether the active user is logged in via Remote Desktop or locally.

Code: Select all

VBSTART
Function UserType(sHost)

   If sHost = "" Then
     Exit Function  '-----> return Empty
   End if

   Set oShell = CreateObject("Wscript.Shell")
   Set oFS = CreateObject("Scripting.FileSystemObject")

   sTmpFile = oFS.GetSpecialFolder(2).ShortPath & "" & oFS.GetTempName

   'Run command and redirect stdout and stderr into temp file
   oShell.Run "%ComSpec% /c %SystemRoot%\System32\QWINSTA.EXE /SERVER:" _
      & sHost & " >" & sTmpFile & " 2>&1", 0, True

   On Error Resume Next
   'Open the temp file
   Set oTF = oFS.OpenTextFile(sTmpFile)

   'Parse the file

   ' Read first line
   sLine = oTF.ReadLine
   If Err.Number <0> return Empty
   End If
   On Error Goto 0

   If Left(Trim(sLine), 26) <> "SESSIONNAME       USERNAME" Then
     ' Something is wrong. Most likely is the content of the first line
     ' this: "Error opening Terminal server <host>"
     Exit Function  '-----> return Empty
   End If

   'Read second line
   sLine = oTF.ReadLine
   'Close file
   oTF.Close
   'Delete it
   oFS.DeleteFile sTmpFile

   sChoppedLine = Mid(sLine, 20)
   If Left(sChoppedLine, 1) = " " Then
     'no user found
     GetUser = ""
   Else
	 if Trim(Left(sLine,8)) = "console" then
	   UserType = "Console"
	 else
	   UserType = "RDP"
	 end if
   End If

End Function
VBEND
VBEval>UserType("."),type
If>type=RDP
  MessageModal>Remote User
Else
  MessageModal>Console User
Endif
Not working on a Win 2008 server.
I have the same user logged in simultaneously both in Console and in RDP.
In both instances, when running this code,. I get a pop up msgBox saying:

Console User

Any pointers on what I'm missing?

Thanks,

-Sup.

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Jan 24, 2013 10:17 am

I guess if the user is logged in in both environments this script picks up the first.

So we need a different method. Rather than looking at it from the user point of view and saying "what is the user logged into" maybe we can say "what is the current session".

Turns out the GetSystemMetrics function gives us a way to determine whether the current session is a remote session or not. Handy. So all we need to do is:

Code: Select all

Let>SM_REMOTESESSION=4096
LibFunc>User32,GetSystemMetrics,isRemote,SM_REMOTESESSION
If>isRemote>0
    MessageModal>Current Session is RDP/Terminal Services Session
Else
    MessageModal>Current Session is Local Session
Endif
For GetSystemMetrics info see:
http://msdn.microsoft.com/en-gb/library ... s.85).aspx
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

SupermanInNY
Newbie
Posts: 2
Joined: Thu Jan 24, 2013 12:00 am

Post by SupermanInNY » Thu Jan 24, 2013 11:52 am

Perfect!

Much shorter code,. much clearer, and works well when there are multi-logins to the same server both from Console and RDP - simultaneously (same time).

thanks Marcus!

Kisharn
Newbie
Posts: 1
Joined: Mon May 20, 2013 11:37 pm

Post by Kisharn » Mon May 20, 2013 11:40 pm

I am having issues with a script running the Auto Login, it is running a remote machine that is logged in via Remote Desktop connection when the machine is locked script starts to run but does not unlock the machine.

Would this code help with this?

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Tue May 21, 2013 3:53 am

Thank You Added As A Snippet.
FIREFIGHTER

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue May 21, 2013 7:01 am

Kisharn wrote:I am having issues with a script running the Auto Login, it is running a remote machine that is logged in via Remote Desktop connection when the machine is locked script starts to run but does not unlock the machine.

Would this code help with this?
You can't use AutoLogon within a Remote Desktop environment. It needs to be configured on a local account on a physical machine while connected physically.

Please see note on:
http://www.mjtnet.com/autologon.htm
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts