Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 14143
  • Last Modified:

Set environment variable in vbscript that can be access in a login script

hi,

when i set a User  envir variable in vbscript which can be used in the same CMD

all is ok if i run another script (or in another cmd)

but how can use this env variable in my loginscript ?

set-env.vbs
     Set objShell = WScript.CreateObject("WScript.Shell")
     Set colSystemEnvVars = objShell.Environment("User")      
     Set val = qeuryID("XXXXXXXXX")
      if val <> "" then
      colSystemEnvVars("MYVAR") = "val"
     .............


login.bat
     cscript set-env.vbs
     echo %MYVAR%


     
     
0
ingetic
Asked:
ingetic
2 Solutions
 
jkrCommented:
The problem is that an environment variable that is set like that will only be valid during the liftime of your .vbs script. If you want it to persist, you'll need to set ist in the registry under HKEY_CURRENT_USER\Environment
0
 
ingeticAuthor Commented:
my sample script set definitivly the variable , this is not the problem


but for next shells, next sessions, ... ... not in the same CMD or script
0
 
jkrCommented:
>>but for next shells, next sessions, ... ... not in the same CMD or script

As I wrote: "The problem is that an environment variable that is set like that will only be valid during the liftime of your .vbs script."

After your script has exited, the value you have set is gone. If you want it to persist for "next shells, next sessions", you will need to set it in the registry.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
ingeticAuthor Commented:
As i wrote, your wrong,

after my script exit, i can see the environment variable in necxt sessions, even after a reboot, in  next shells..

BUT NOT in current shell !!!
0
 
grayeCommented:
There are quiet a few command-line utilities that allow you a save an enviromental variable beyond the scope of the current shell.

I've used the SETX command from the WinXP Support Tools
http://www.microsoft.com/downloads/details.aspx?FamilyID=49ae8576-9bb9-4126-9761-ba8011fabf38&displaylang=en
0
 
DaVinci007Commented:
I have also been looking for an easy solution to this problem and the current accepted solutions are wrong.  What the author asks for is clear.  Here is how I suggest doing this.


set-env.vbs
        wscript.echo "set MYVAR=Value1"
        wscript.echo "set MYVAR2=Value2"
         .............

login.bat
     call cscript //Nologo set-env.vbs > %tempt%\setVars.bat
     call %TEMP%\setVars.bat
     echo %MYVAR%

This will output to the console:

Value1

Explanation:

Since the current scope of the cmd.exe running cannot be altered by either WMI, setx or the registry, the only way is to set the variable in the current environment.

     call cscript //Nologo set-env.vbs > %tempt%\setVars.bat

This will redirect all output of set-env.vbs to a file named setVars.bat. Make sure all wscript.echo commands output valid batch code.  Using "call" is important in order to wait for the called script to finish before continuing the logon script.

     call %TEMP%\setVars.bat

This executes in the current scope the batch file created from the output of set-env.vbs.

The variables are then available for use.

I hope this could help anyone in this situation.
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now