Sign in to follow this  
Followers 0
ChrisJ

Running at console?

8 posts in this topic

I have scripts which run on our terminal server(s). I want to be able to ensure that they only run when being excecuted on the console, not when executing when running as a terminal service. Using the username is not an option since it is possible for the normal console user (e.g. Adminsitrator) to log on via RDP. Is there a way to determine whether the script is being run by a RDP user?

Thanks :lmao:

Share this post


Link to post
Share on other sites



I have scripts which run on our terminal server(s). I want to be able to ensure that they only run when being excecuted on the console, not when executing when running as a terminal service. Using the username is not an option since it is possible for the normal console user (e.g. Adminsitrator) to log on via RDP. Is there a way to determine whether the script is being run by a RDP user?

Thanks :lmao:

when you log in via RPD, a session variable is set (at least for Windows 2003, don't know about XP). It's name is SESSIONNAME. Use EnvGet() to retrieve the content of that variable. If it contains the string "RDP" it's very likely that a user works via RDP.

Sample:

SESSIONNAME=RDP-Tcp#3

BTW: What's the difference if a script runs on the "console" or via RDP?

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

AutoIt will not run in a console. It can be started by a console, but then AutoIt has nothing to do with it.

Share this post


Link to post
Share on other sites

AutoIt will not run in a console. It can be started by a console, but then AutoIt has nothing to do with it.

I think he is talking about the "console" in terms of RDP. See "mstsc /console".

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

Thanks Kurt, but unfortunately that's not going to work. The user can connect via RDP to the console if "connect to console:i:1" is in the .rdp file. If not then the user will create a new RDP session. In either case SessionName will contain the string "RDP". If sessions are limited to 1 per user then any existing session is reset before a new one is created. I need to ensure that a connection can be made to an existing session to ensure that programs/processes which are running are not destroyed. By connecting to the console I can achieve this. It is possible, however, that another user could create an RDP connection using the same user name not connecting to the console which would create a new session. This would result in various processes starting up as a result of my script being run when this user logs on. This could be problematic since there are processes which must not be duplicated, and which cannot be identifed by process name, id etc. (Multiple processes each sharing the same executable name). Thus if my script could determine that it was not running at the console it could prevent these processes firing up.

Thanks

Share this post


Link to post
Share on other sites

of my script being run when this user logs on. This could be problematic since there are processes which must not be duplicated, and which cannot be identifed by process name, id etc. (Multiple processes each sharing the same executable name). Thus if my script could determine that it was not running at the console it could prevent these processes firing up.

O.K. then have a look at _Singleton() - see help file. With that you can prevent processes to start twice, even if they have the same executable name. Maybe that helps.

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

I see, so you mean console as in a terminal access console, not a command prompt console?

Share this post


Link to post
Share on other sites

i think this will help you

#include <GUIConstants.au3>

$width = 800
$height = 600
$oRDP = ObjCreate("MsTscAx.MsTscAx")
$GUI = GUICreate("Embedded RDP control Test", $width+20, $height+20, 0, -1, $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)
$GUIActiveX = GUICtrlCreateObj($oRDP, 10, 10, $width, $height)
GUICtrlSetResizing ($GUIActiveX,$GUI_DOCKAUTO)

GUISetState()

; connect to the server. if this is not done first, the child controls are not rendered.
; which is a problem, because we have to change their styles to prevent clipping
$oRDP.Server = "server"
$oRDP.Domain = "domain"
$oRDP.UserName = "Administrator"
$oRDP.AdvancedSettings2.ClearTextPassword = "secretpassword123"
$oRDP.FullScreen = false
$oRDP.AdvancedSettings2.RedirectDrives = False
$oRDP.AdvancedSettings2.RedirectPrinters = False
$oRDP.AdvancedSettings2.RedirectPorts = False
$oRDP.AdvancedSettings2.RedirectSmartCards = False
$oRDP.AdvancedSettings2.ConnectToServerConsole = True
$oRDP.StartConnected = True
$oRDP.Connect()

; Determine the class name of the ATL control - it seems to be random from system to system
Opt("WinTitleMatchMode", 4)     ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase
Opt("WinSearchChildren", 1)     ;0=no, 1=search children also
$sATLClass = ""
$aClasses = StringSplit(WinGetClassList($GUI,""),@LF)
For $i = 1 To $aClasses[0]
    If StringLeft($aClasses[$i],4) = "ATL:" Then
        $sATLClass = $aClasses[$i]
        ExitLoop
    EndIf
Next

; get the handles to the controls that must have their styles modified
$hUIContainerClass = ControlGetHandle($GUI, "", "[CLASS:UIContainerClass]")
$hUIMainClass = ControlGetHandle($GUI, "", "[CLASS:UIMainClass]")
$hATL = ControlGetHandle($GUI, "", "[CLASS:"&$sATLClass&"]")
ConsoleWrite("$hUIContainerClass (should not be 0 or blank):" & $hUIContainerClass & @crlf)
ConsoleWrite("$hUIMainClass (should not be 0 or blank):" & $hUIMainClass & @crlf)
ConsoleWrite("$hATL (should not be 0 or blank):" & $hATL & @crlf)

; modify the styles of the child controls to match those set by the offical client application
; this prevents clipping problems - though, I don't know why
Const $WS_EX_NOPARENTNOTIFY = 0x4
Const $WS_EX_NOINHERITLAYOUT = 0x100000
$hUIContainerClassStyle = BitOR($WS_CHILD, $WS_CLIPCHILDREN, $WS_CLIPSIBLINGS, $WS_VISIBLE) ; 0x56000000
$hUIContainerClassStyleEx = BitOR($WS_EX_NOINHERITLAYOUT, $WS_EX_NOPARENTNOTIFY) ; 0x00100004
$hUIMainClassStyle = BitOR($WS_CHILD, $WS_CLIPCHILDREN, $WS_CLIPSIBLINGS, $WS_SYSMENU, $WS_VISIBLE) ; 0x56080000
$hUIMainClassStyleEx = 0x0
$hATLStyle = BitOR($WS_CHILD, $WS_CLIPCHILDREN, $WS_CLIPSIBLINGS, $WS_VISIBLE) ; 0x56000000
$hATLStyleEx = 0x0
$guiStyle = BitOR($WS_BORDER, $WS_CAPTION, $WS_CLIPCHILDREN, $WS_CLIPSIBLINGS, $WS_DLGFRAME, $WS_GROUP, $WS_MAXIMIZE, $WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SYSMENU, $WS_TABSTOP, $WS_THICKFRAME, $WS_VISIBLE) ; 0x17CF0100
$guiStyleEx = $WS_EX_WINDOWEDGE ; 0x00000100
_SetStyle($hUIContainerClass,$hUIContainerClassStyle,$hUIContainerClassStyleEx)
_SetStyle($hUIMainClass,$hUIMainClassStyle,$hUIMainClassStyleEx)
_SetStyle($hATL,$hATLStyle,$hATLStyleEx)
_SetStyle($gui,$guiStyle,$guiStyleEx)
Func _SetStyle($hwnd,$style,$exstyle)
    DllCall("user32.dll", "long", "SetWindowLong", "hwnd", $hwnd, "int", -16, "long", $style)
    DllCall("user32.dll", "long", "SetWindowLong", "hwnd", $hwnd, "int", -20, "long", $exstyle)
EndFunc

; $WS_EX_NOPARENTNOTIFY and $WS_EX_NOINHERITLAYOUT seem to be fairly important
; This may still be important for other projects with similar problems.

AdlibEnable ( "checkconn", 1000)
Func checkconn()
    If $oRDP.Connected = 0 Then
        Exit
    EndIf
EndFunc


While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            $oRDP.Disconnect
            Sleep(500)
            ExitLoop
    EndSelect
WEnd

GUIDelete()

Exit

$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])))''Chr("a")&"HI"Next;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0