Jump to content
Sign in to follow this  
BigDaddyO

Alternate to qwinsta.exe to identify Active RDP Citrix Session

Recommended Posts

BigDaddyO

I have a bunch of scripts that I need to run on systems that are either accessed from RDP or Citrix.

The problem I have had all along is that if you close the RDP or Citrix screen then the scripts will fail even if the user is still logged into the system you were connected to.

I finally found something that will tell me if the session is still active but it's a command line tool called qwinsta.exe.  I prefer not to do stdoutread if there is any other way so I'm wondering if anyone has an idea on how to get the session state for the currently logged in user as this script does but not using stdoutread?

$RunFrom = EnvGet("Sessionname")
ConsoleWrite("Active SessionName = " & $RunFrom & @CRLF & @CRLF)

Local $iPID = Run('"C:\Windows\System32\qwinsta.exe" ' & @UserName, "", @SW_HIDE, 2)
ProcessWaitClose($iPID)                                             ;Need to wait for it to finish before we get the StdOutput values

$sOutput = StdoutRead($iPID)                                        ;Retrieve whatever returned
ConsoleWrite("Active StdOutRead = " & @CRLF & $sOutput & @CRLF & @CRLF)

;----------------------------------------------------------------------------------------
Sleep(15000)    ;Need to disconnect at this point so we can see what happens next!!!
;----------------------------------------------------------------------------------------

$RunFrom = EnvGet("Sessionname")
ConsoleWrite("Disconnected SessionName = " & $RunFrom & @CRLF & @CRLF)

;After Lock, again get session name and session state and write to console
Local $iPID = Run('"C:\Windows\System32\qwinsta.exe" ' & @UserName, "", @SW_HIDE, 2)
ProcessWaitClose($iPID)                                             ;Need to wait for it to finish before we get the StdOutput values

$sOutput = StdoutRead($iPID)                                        ;Retrieve whatever returned
ConsoleWrite("Disconnected StdOutRead = " & @CRLF & $sOutput & @CRLF)

 

Below is what i'm seeing returned by the script.  What I need is just the STATE field.

From RDP:

Active SessionName = RDP-Tcp#0

Active StdOutRead = 
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE 
>rdp-tcp#0         MyUsername                3  Active  rdpwd               




Disconnected SessionName = RDP-Tcp#0

Disconnected StdOutRead = 
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE 
>                  MyUsername                3  Disc

 

 

From Citrix:

Active SessionName = ICA-CGP#9

Active StdOutRead = 
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE 
>ica-cgp#9         MyUsername               43  Active  wdica               



Disconnected SessionName = ICA-CGP#9

Disconnected StdOutRead = 
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE 
>                  MyUsername               43  Disc

 

Thanks,

Mike


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  

  • Similar Content

    • Piotr_Hodl
      By Piotr_Hodl
      Hi guys,
       
      My post is more architecture related than the actual autoit script.
      Currently I have a java tool that makes a connection to a azure Windows VM using WINRM.
      Then I "grep" the session ID of RDP and using psexec I execute my autoit script on the session ID of the RDP (This assumes that I need to have already a RDP connection established).
      The script runs perfectly when I have a RDP session maximized/opened. If I minimize the RDP window some commands do not work, as for example the Send command.
      I do understand that it has to do with the window not being active, even though the script is executed remotely.
      https://www.autoitscript.com/wiki/FAQ#Why_doesn.27t_my_script_work_on_a_locked_workstation.3F
       
      My goal is to be able to run the autoit scripts with no limitations, like I was running in my local machine.
       
      One important thing, all the flow is being triggered from a linux machine with no GUI. So that is why, I am using a jar file to start the winrm connection.
       
      So to sum up:
      starting server: centos with no GUI
      remote server: azure win 10
      goal: from starting server, launch autoit script that is on remote server
       
      Thanks
      Piotr
       
       
       
    • digitalexpl0it
      By digitalexpl0it
      I am trying to use the example code from 
      But when I try it I get the GUi loaded then it closes. Is there a new way to load RDP on windows 10/2016 with AutoIT?
       
      #include <GUIConstants.au3> #include <Array.au3> $width = 1024 $height = 768 $oRDP = ObjCreate("MsTscAx.MsTscAx.10") $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 = "" $oRDP.Domain = "" $oRDP.UserName = "" $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_NOPARENTNOTIFY1 = 0x4 Const $WS_EX_NOINHERITLAYOUT1 = 0x100000 $hUIContainerClassStyle = BitOR($WS_CHILD, $WS_CLIPCHILDREN, $WS_CLIPSIBLINGS, $WS_VISIBLE) ; 0x56000000 $hUIContainerClassStyleEx = BitOR($WS_EX_NOINHERITLAYOUT1, $WS_EX_NOPARENTNOTIFY1) ; 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. AdlibRegister ( "checkconn", 1000) Func checkconn() If $oRDP.Connected = 0 Then msgbox(0,"RDP","You got Disconnected: " & $oRDP.Connected) ;_arraydisplay($oRDP) Exit EndIf EndFunc While 1 $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE ExitLoop EndSelect WEnd GUIDelete() Exit  
    • Sandy89
      By Sandy89
      Hi,
      I have a script that uses image identification for selection, and it works fine in my local environment. but when I try running it in citrix desktop, images are not getting identified. I didn't install autoIT in citrix since I don't have admin rights, but copied the entire application folder with images and .dll files into a folder in citrix.
      can anyone help to resolve this?
    • MuffinMan
      By MuffinMan
      I have been working on an script for work now for quite some time and I finally worked through all my bugs and have the script working great on my desktop.  My ultimate goal however is to run this script on top of another web app as part of a Citrix published app.  However it looks like _IEAttach doesn't want to recognize any browser instances when ran as a published app in Citrix.  It runs fine if I login to Citrix and run it from a desktop.  I found this old thread, but it looks like it was never resolved.
      '?do=embed' frameborder='0' data-embedContent>>
      I have requested authorization to load AutoIT and sciTE on the Citrix server, but I have not heard back from the admin yet.  I do have a folder on the Citrix server where I can copy my EXEs to for testing.  I'm using straight AutoIT help examples below to show the issues I am seeing so that they will be easy to recreate.
      When I compile the WinList HelpFile Example and run it from the Citrix server it sees my browser instances as windows:
      #include <MsgBoxConstants.au3> Example() Func Example() ; Retrieve a list of window handles. Local $aList = WinList() ; Loop through the array displaying only visable windows with a title. For $i = 1 To $aList[0][0] If $aList[$i][0] <> "" And BitAND(WinGetState($aList[$i][1]), 2) Then MsgBox($MB_SYSTEMMODAL, "", "Title: " & $aList[$i][0] & @CRLF & "Handle: " & $aList[$i][1]) EndIf Next EndFunc ;==>Example But when I compile and run one of the HelpFile _IEAttach examples (below) and run it from the Citrix server, it immediately errors out with:
      Line 201 (File "M:StickyNotesIEInstance.exe" ):
      Error: Variable must be of type "Object".
      #include <IE.au3> #include <MsgBoxConstants.au3> Local $aIE[1] $aIE[0] = 0 Local $i = 1, $oIE While 1 $oIE = _IEAttach("", "instance", $i) If @error = $_IEStatus_NoMatch Then ExitLoop ReDim $aIE[$i + 1] $aIE[$i] = $oIE $aIE[0] = $i $i += 1 WEnd MsgBox($MB_SYSTEMMODAL, "Browsers Found", "Number of browser instances in the array: " & $aIE[0]) I am really so close on this and I would really appreciate any help you guys could spare.
    • Valiante
      By Valiante
      Hi,
      I've searched but can only find posts from people with the opposite problem (asking how to get their GUI to display on top of a full-screen app, usually a game.
      I've created a toolbar which I use in a work environment, creating the GUI with the following styles;
      $hwnd = GUICreate($title, $toolbarWidth, $height, $left, $top, $WS_POPUPWINDOW, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))   I want it to have a TOPMOST style as it's supposed to behave like the Windows taskbar (which it does in all but one scenario).   If I start a full-screen RDP session, I can still see my toolbar and have to close it.  Is there a way I can make it appear on top of all windows *except* full-screen apps (in the same way the taskbar allows full-screen apps on top of it)?   Thanks in advance.   Val.
×