Jump to content
francoiste

read environment variable set in calling batch script - UAC

Recommended Posts

francoiste

i'm using "test.bat" to set an environment variable and then invoke the compiled script:

@echo off
SETLOCAL
set MyCustomVar=HelloWorld

start "title" /b /wait "%~dp0\MyCompiledScript.exe" -test1

echo DONE
pause

the script source:

#pragma compile(x64, false)
#pragma compile(ExecLevel, highestavailable)

Local $myVar = "MyCustomVar"
Local $myValue = EnvGet($myVar)
MsgBox(0, "TEST", "environment variable " & $myVar & " has value: " & $myValue)

 

however, the variable is not visible to the autoit script.

any ideas?

--

EDIT:

i noticed this is due to the UAC execution level set as "highestavailable" (when the current user is member of the local admin group).
script source (above) edited accordingly.

this raises even more questions ...

Edited by francoiste

Share this post


Link to post
Share on other sites
AutoBert

Look for 'Command Line Parameters' in the AutoIt-Help, i think this is what you are searching fore.

Edited by AutoBert

Share this post


Link to post
Share on other sites
SadBunny

First I thought maybe something went wrong because you set the envvar in one shell, then use start to spawn a second shell to which maybe the envvar was not visible, but then I thought let's just try it first... Works fine for me on my Windows 8.1!

But yeah I agree with Bert. (Though AutoBert has a point as well :) )


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
francoiste

<empty>

Edited by francoiste
moved to the bottom

Share this post


Link to post
Share on other sites
argumentum

works just fine.

@echo off
SETLOCAL
set MyCustomVar=HelloWorld
MyCompiledScript.exe -test1
set MyCustomVar="Buy World"
MyCompiledScript.exe
echo DONE
pause

 

Share this post


Link to post
Share on other sites
francoiste

thanks for the replies.

however, i have good reasons to do it the batch way.
i'm not looking for suggestions to alternate approaches, but rather advice to solve the issue with this particular approach.

please note the update done to the initial posting. it seems to be specific to execution level "highestavailable".

Share this post


Link to post
Share on other sites
SadBunny

Yes, that makes sense. The batchfile runs in a walled-off process space as you don't start it elevated ("as administrator"), and by requesting UAC elevation in your script, the script runs in another process space and has no access to temporary environment settings in the former one.

Start your batchfile "as administrator" too, and it should work. (At least, it does for me.) Or set the environment variable as system variable (from command prompt: setx /M), note however that that still requires elevation.

/edit: if your chosen approach is almost certainly a "suboptimal" one, you're bound to get some suggestions for better ones. Live with it :)

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
francoiste

i still don't fully understand why the environment variables are not being inherited during UAC elevation (for the current user).

anyways, i'm now using the workaround to use a boot-strapper (*.exe) that does nothing but:

  • carrying the ExecLevel "highestavailable"
  • invoke the batch script (that in turn will invoke the compiled "MyCompiledScript.exe")

"RunMyTest.au3":

#pragma compile(x64, false)
#pragma compile(ExecLevel, highestavailable)

If Not @Compiled Then
    MsgBox(0, "ERROR", "This must be run from compiled *.exe")
    Exit
EndIf

ShellExecute(".\test.bat")

 

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

×