Flipper Posted July 8, 2023 Share Posted July 8, 2023 (edited) Ok, I have written a script, it works, I'm pleased. Yay Me! - I even worked out that if I run it in remote desktop, I need to move the GUI as the resolutions are different. $T = MsgBox(4,"","Are you Local?") if $T =6 Then Local $hGUI = GUICreate("",1250,50,-1,950) Else Local $hGUI = GUICreate("",1250,50,-1,600) EndIf Yay Me again. BUT! (you knew there was going to be a "but"...) What I really want is for the script to work out if it's being run locally, or from the remote session. I have tried messing about with SESSIONNAME. In a DOS command window, the dratted PC doesn't seem to refresh the value between the remote & local session. It's either "Console" or "rdp-tcp#0" but doesn't change. SO I tried "Query User" - In a DOS command window the results are better. C:\Users\Pete>query user USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME >pete console 1 Active 24 08/07/2023 12:06 C:\Users\Pete>query user USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME >pete rdp-tcp#0 1 Active . 08/07/2023 12:06 When I try to get this into my autoit script, $Users = _RunDos("c:\windows\system32\query user >> C:\Users\pete\users.txt" ) The output file is empty. Running the identical command in DOS results in a perfect file. - The DOS window is not elevated to Administrator! using Run("c:\windows\system32\query user >> C:\Users\pete\users.txt" ) does not generate a file. Help please! all I need is :- If remote then put GUI HERE else put GUI THERE Thanks! (I did used to be good at this!) Edited July 8, 2023 by Flipper Tidyup Link to comment Share on other sites More sharing options...
TheXman Posted July 8, 2023 Share Posted July 8, 2023 (edited) 44 minutes ago, Flipper said: using Run("c:\windows\system32\query user >> C:\Users\pete\users.txt" ) does not generate a file. Here are 3 different ways to capture console command output: Edited July 8, 2023 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
ioa747 Posted July 8, 2023 Share Posted July 8, 2023 (edited) can you use @UserName ? or @DesktopWidth ? Edited July 8, 2023 by ioa747 I know that I know nothing Link to comment Share on other sites More sharing options...
Danp2 Posted July 8, 2023 Share Posted July 8, 2023 Try this (untested) -- $bIsRemote = (EnvGet("sessionname") <> 'console') If $bIsRemote then ; put GUI HERE Else ;put GUI THERE EndIf Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
Nine Posted July 8, 2023 Share Posted July 8, 2023 (edited) @Flipper Here a few things you need to understand in order to make your Run statement working. First when you want to send output of a DOS command into a file, you need to reshell like this : Run(@ComSpec & " /c c:\windows\system32\query.exe user > users.txt") But it will still not work because there is an automatic redirection to SysWOW64 folder when running x86 on a 64bits machine. And query.exe does not exist in this folder. So you need to disable this redirection before the Run statement by using : _WinAPI_Wow64EnableWow64FsRedirection (False) Another way is to run your script x64 by placing this line at the very beginning (then there is no redirection). #AutoIt3Wrapper_UseX64=y Hope that helps... Edit : on the side note, when your run statement does not work you can use /k instead of /c so the console will still be showing after the run is completed. Edited July 8, 2023 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Solution Flipper Posted July 8, 2023 Author Solution Share Posted July 8, 2023 @ioa747 - unfortunately, as I'm the only chap using this system, the usernames are identical, and width don't work either, as both resolutions are the same, except on my laptop, the "Scale" is set at 150% as the screen is so small. @Danp2 - This I tried first, but the environment variable seems to be only set once, so it's either "Console" or "rdp-tcp#0" but doesn't change. This is why I was trying "Query User" as this seemed to be reliable. "Query Session" also looked interesting, as this listed both Sessionnames, with the active one flagged as ... active! @TheXman @Nine - Thanks folks, looking at these things now, Stay Tuned! Link to comment Share on other sites More sharing options...
Danp2 Posted July 8, 2023 Share Posted July 8, 2023 9 minutes ago, Flipper said: This I tried first, but the environment variable seems to be only set once, so it's either "Console" or "rdp-tcp#0" but doesn't change. Not sure that I understand the issue. Can you elaborate? Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
Flipper Posted July 8, 2023 Author Share Posted July 8, 2023 (edited) @Nine Thanks! That's what was needed, here's the code that works (note the runWAIT - that had me going for a while!) Local $array #AutoIt3Wrapper_UseX64=y Runwait(@ComSpec & " /c c:\windows\system32\query.exe user > c:\users\pete\users.txt") $array=FileReadToArray('c:\users\pete\users.txt') ;_ArrayDisplay($Array, "1D display") if StringInStr($array[1],"console") then Local $hGUI = GUICreate("",1250,50,-1,950) Else Local $hGUI = GUICreate("",1250,50,-1,600) EndIf FileDelete("c:\users\pete\users.txt") @Danp2 - Open CMD - type "echo %Sessionname%" - result - "Console" Now, without doing anything else, get onto the remote PC & do a remote desktop connection. In the same CMD window repeat the "Echo %sessionname%" and the result is "Console" again! Should have been "rdp-tcp#0" ---finally, how do i do a cool "codebox" in this forum? Edited July 8, 2023 by Flipper Tidyup Link to comment Share on other sites More sharing options...
Danp2 Posted July 8, 2023 Share Posted July 8, 2023 8 minutes ago, Flipper said: Should have been "rdp-tcp#0" To get that result, you would need to actually run the command within the remote desktop session. I'm still not following your logic. Are you expecting a script being run locally to detect that a separate RDP session is running? Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
Nine Posted July 8, 2023 Share Posted July 8, 2023 @Flipper When you post code, please use method described in link. Thanks. Flipper 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Flipper Posted July 8, 2023 Author Share Posted July 8, 2023 @Nine TA! @Danp2 Yep. Run a CMD on the MAIN PC - gives Console. Get the LAPTOP out. Connect to the MAINPC using remote desktop. In that remote session on the laptop, using the same window as before, re-run the Echo cmd. Still gives "Console" Note, "Query User" & "Query Session" give the correct answers in the same circumstances! Link to comment Share on other sites More sharing options...
Danp2 Posted July 8, 2023 Share Posted July 8, 2023 @Flipper I wouldn't expect the environmental variable to dynamically change within an existing Cmd session. However, it should give a different value if you launch a separate Cmd session from within the RDP session. FWIW, this is my experience when using RDP with Windows Server. Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
Flipper Posted July 8, 2023 Author Share Posted July 8, 2023 running this code #include <MsgBoxConstants.au3> Example() Func Example() ; Retrieve the value of the environment variable %SessionName%. ; When you assign or retrieve an envorinment variable you do so minus the percentage signs (%). Local $sEnvVar = EnvGet("sessionname") ; Display the value of the environment variable %APPDATA%. MsgBox($MB_SYSTEMMODAL, "", "The environment variable %SessionName% has the value of: " & @CRLF & @CRLF & $sEnvVar) EndFunc ;==>Example gives the same result locally and in a remote session. So I completely agree with your post! Just had to find a method that worked for my case. We be learning. Thanks for the interest! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now