Tigerweld

get cmd line variable

9 posts in this topic

How do I make this variable work?

$MY_SESSION_ID = for /f "tokens=3-4" %%a in ('query session %username%') do @if "%%b"=="Active" set MY_SESSION_ID=%%a

MsgBox(1, "My Session ID", "Session ID is: " & $MY_SESSION_ID)

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

In order to retrieve/collect the output from the console process, you'll need to use the Std*Read functions.  The linked help article below has an example which you can modify.   https://www.autoitscript.com/autoit3/docs/functions/StderrRead.htm

Spoiler

Update the Local $iPID = Run line. ;)

 

Edited by spudw2k

Share this post


Link to post
Share on other sites

I'm not trying to get process id, I'm trying to get the id of the user's session.  This works in a batch file, but I want to use it in Autoit.

Share this post


Link to post
Share on other sites

I think I'm closer, but its giving me zero when I know the id is 1.

$MY_SESSION_ID = RunWait(@ComSpec & 'for /f "tokens=3-4" %a in (''query session %username%'') do @if "%b"=="Active" set MY_SESSION_ID=%a', @SystemDir)

MsgBox(1, "My Session ID", "Session ID is: " & $MY_SESSION_ID)

 

Share this post


Link to post
Share on other sites

Runwait returns the exitcode of the program you ran. It won't give you a session ID.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

so there is no way of obtaining the id?

Share this post


Link to post
Share on other sites

You could do it this way, no guarantee it will work as I don't know what the DOS commands are doing.

$PID = Run(@ComSpec & 'for /f "tokens=3,4" %a in (''query session %username%'') do @if "%b"=="Active" set MY_SESSION_ID=%a', @SystemDir, @SW_SHOW, 8)
While ProcessExists($PID)
    Sleep(100)
WEnd
$MY_SESSION_ID = StdoutRead($pid)
MsgBox(1, "My Session ID", "Session ID is: " & $MY_SESSION_ID)

 


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

no, unfortunately its coming up blank.  thanks though.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Looks like you are setting the environment variable MY_SESSION_ID with the output from the command.  You should be "echo"ing it instead to output it to the console so the StdoutRead function picks it up.

edit: There was also an issue with spaces in run command so it wasn't executing.  This works for me:

#include <AutoItConstants.au3>
$sCMD = 'for /f "tokens=3,4" %a in (''c:\windows\system32\query.exe session %username%'') do @if "%b"=="Active" echo %a'

$PID = Run(@ComSpec & " /c " & $sCMD, @ScriptDir, @SW_HIDE, $STDERR_MERGED)
While ProcessExists($PID)
    Sleep(100)
WEnd
$MY_SESSION_ID = StdoutRead($pid)
MsgBox(1, "My Session ID", "Session ID is: " & $MY_SESSION_ID)

 

On 12/13/2016 at 7:50 AM, BrewManNH said:

...I don't know what the DOS commands are doing.

He's essentially parsing the output of the query session command to retrieve just the value instead of the entire line.

 

@Tigerweld no comment?

Edited by spudw2k

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

  • Similar Content

    • jloyzaga
      By jloyzaga
      I can run this and notepad++ opens the file Run("C:\Program Files (x86)\Notepad++\notepad++.exe E:\icare\icare\NI-WI-NBSE-ERR.xml")
      But what I want is to run the .au3 or exe from command line and send it the file as parameter -am I being to simplistic in hoping its this easy?
      #include <FileConstants.au3>
      #include <MsgBoxConstants.au3>
      #include <Array.au3>
      #include <File.au3>
      ;E:\icare\icare\NI-WI-NBSE-ERR.xml
      ;Run("C:\Program Files (x86)\Notepad++\notepad++.exe E:\icare\icare\NI-WI-NBSE-ERR.xml")
      Run("C:\Program Files (x86)\Notepad++\notepad++.exe " & $CmdLine[0] )
      sleep (200)
      Send("{CTRLDOWN}a{CTRLUP}")
      sleep (500)
      Send("{CTRLDOWN}a{CTRLUP}{CTRLDOWN}s{CTRLUP}{CTRLDOWN}{SHIFTDOWN}b{CTRLUP}{SHIFTUP}")
      sleep (500)
      Send("{CTRLDOWN}{ALTDOWN}s{ALTUP}{CTRLUP}")
      sleep (500)
      Send("E:\icare\icare\NI_WI_MONTHLY_INSTALMENTS_c2base64-7.txt")
      sleep (500)
      Send("{ENTER}")
       
    • theak
      By theak
      ShellExecute("\\servername\cofs\Organization\ITS\_Software & Hardware\AS400\DVD\Windows\cwblaunch.exe", '"/v/qn ADDLOCAL=ALL"')  I had to dig to find the right setup.exe according to the documentation but I got it to run. However, it pops up with this dialog box (attached) showing me the command line parameters, as if I'm incorrect.
       
       
      It continues to run the install however, but seemingly without any of the command line parameters.
      Here's the command I entered into the CMD prompt exactly:
      "\\servername\cofs\Organization\ITS\_Software & Hardware\AS400\DVD\Windows\image64a\setup.exe",'"/v/qn ADDLOCAL=ALL"'
    • Tdurrant
      By Tdurrant
      I feel like i have been going in circles and getting no where
      I am trying to do a git pull
       
      LOCAL $sTemp = '"C:\Program Files (x86)\Git\bin\git.exe" pull'
      LOCAL $sLine = ''
      local $sToWrite = ''
      local $PID = Run($sTemp,$GitDir,@SW_MAXIMIZE, $STDIN_CHILD + $STDOUT_CHILD)
      StdinWrite($PID,$sToWrite)
      IF( $PID <> 0) THEN
          While 1
              $sLine &= StdoutRead($PID)
              MsgBox(0,'returned',$sLine)
              If @error Then ExitLoop
              Sleep(25)
          Wend
          Exit
      EndIf
      i have also tried
      LOCAL $sTemp = '"C:\Program Files (x86)\Git\bin\git.exe" pull'
      LOCAL $sLine = ''
      local $PID = Run($sTemp,$GitDir,@SW_MAXIMIZE, $STDIN_CHILD + $STDOUT_CHILD)
      StdinWrite($PID)
      IF( $PID <> 0) THEN
          While 1
              $sLine &= StdoutRead($PID)
              MsgBox(0,'returned',$sLine)
              If @error Then ExitLoop
              Sleep(25)
          Wend
          Exit
      EndIf
       
      This triggers the command prompt which gives me this info eventually
      Username for 'SomeNonGitHubRepositiory':
      Problem is that My message box always returns a blank without erroring out
      If it did return somethign then i would do
      $sToWrite = 'Username'
      StdinWrite($PID,$sToWrite)
      What did i miss?
    • Skysnake
      By Skysnake
      Hi Everyone
      I want to have a GUI, but which will accept command line options on launch.
      So, the commanline would look something like
      myAPP.EXE -bigfont -bigicon
      where myAPP.EXE would be the name of the AutoIt EXE, and the -bigfont & -bigicon items represent optional command line parameters with which the EXE starts.
      I am not looking at creating a CUI.  This is GUI, but with startup command line parameters.  These command line parameters would only be read once, during start up of the EXE.
      I have searched the forum, no luck.  What I did find was this commented by Water: https://www.autoitscript.com/forum/topic/138754-adding-command-line-parameter/
      Should I start the GUI EXE as normal, and then first possible opportunity read the command line? Is that the way to go?
      Thanks in advance
    • edthedestroyer
      By edthedestroyer
      I am trying to create a script that allows me to run and use it with CMD as the interface.

      For example:



      This is the code I have so far...

      #AutoIt3Wrapper_Change2CUI=y ConsoleWrite("Enter PIN" & @CRLF & ":") Local $input While 1 $input &= ConsoleRead() if StringInStr($input,@CRLF) <> 0 then ExitLoop Wend if $input = "1111" & @CRLF Then ConsoleWrite("Success") Else ConsoleWrite("Failure") ConsoleWrite($input) EndIf
      The problem is that after it writes Enter PIN: it will not accept input and gets stuck. After I close the script, all the input I typed shows up in cmd.


      I want be able to run this script without opening another cmd window but I haven't been able to find relevant examples for creating a "console" application used through cmd.
      Can anyone help me out?