Jump to content

Help with "Query User" please.


Go to solution Solved by Flipper,

Recommended Posts

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 by Flipper
Tidyup
Link to comment
Share on other sites

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 by TheXman
Link to comment
Share on other sites

@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 by Nine
Link to comment
Share on other sites

  • Solution

@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

@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 by Flipper
Tidyup
Link to comment
Share on other sites

@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

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

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...