Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find if user is logged on console or rdp

Status
Not open for further replies.

terjene

Programmer
Joined
Jun 30, 2006
Messages
2
Location
NO
Hi!

I want to check if the current user is logged on the console or rdp. Any hint?

What I hope to solve is:
When a server restart i want to start some programs when the administrator logs on, but only if he's on the console. If the administrator logs on through rdp, i don't want to do anything..



Terje
 
I found a way to solve my problem.. if someone is interested, i used this code:

Function FindUserLogonType()
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 >" & sTmpFile & " 2>&1", 0, True
On Error Resume Next
'Open the temp file
Set oTF = oFS.OpenTextFile(sTmpFile)

'Parse the file

'The file should look like this
'Current user start with '>'
' SESSIONNAME USERNAME ID STATE TYPE DEVICE
' rdp-tcp#5 administrator 0 Active rdpwd
' rdp-tcp 65536 Listen rdpwd
'>rdp-tcp#4 administrator 2 Active rdpwd
' console 4 Conn wdcon

' 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
Exit Function '-----> return Empty
End If

Do While Not otf.AtEndOfStream
sLine = oTF.ReadLine
If Instr(1, sLine, ">", 1) > 0 Then
Exit Do
End if
Loop

'Close file
oTF.Close
'Delete it
oFS.DeleteFile sTmpFile
If (Mid(sLine,2,7) = "console") Or (Trim(Mid(sline,42,5))=0) Then
'if line starts with console or id = 0
MsgBox "Console"
Else
MsgBox "RDP"
End if
End Function
 
It should be pointed out that an admin using the Remote Desktops Snap-In can remotely take over the console as well.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top