Link to home
Start Free TrialLog in
Avatar of gd015
gd015

asked on

Set Environment variable from logon script

How do I set environment variables from a logon script.

The NT resource kit has a utility SETX to set environment variables.

I can't get this to work when running from a logon script.
The value is set in the registry but the previous value that was set during the last session is the one that is available.

Its almost like the notify windows API's doesn't work during logon.

There may be another way altogether of acheiving my objective so if anyone knows, I would be most grateful.
Avatar of brauner071697
brauner071697

The CD-ROM version of Windows 95 includes a tool called Winset.exe. This tool is designed to set environment variables in Microsoft Windows NT login scripts, but you can also use it to set global environment variables in Windows 95. The syntax for Winset.exe is the same as for the SET command.
For example, to set a global TEMP environment variable within Windows 95, follow these steps:

    1.Copy the Winset.exe file from the Admin\Apptools\Envvars           folder on the Windows 95 CD-ROM to the Windows folder on your     hard disk.

    2.Click the Start button, and then click Run.

    3.Type the following line in the Open box, and then click OK:

             winset temp=c:\temp

Note that running the Winset.exe tool at a command prompt has no effect in the current instance of the command prompt. A new command prompt must be launched before the change is apparent.

For additional information, please see the Readme.txt file in the Admin\Apptools\Envvars folder on the Windows 95 CD-ROM.

hope this helps
Avatar of gd015

ASKER

Sorry, I think there is a misunderstanding.  I am running NT4 workstation, not 95.  My mistake.

The SETX utility does the same as winset, and only applies to new shells also.

This is why my problem is so annoying, because when the logon finishes, and I start a new shell, the variable has not been updated.


Let me se a part of your logon script, please.
The User-Environment is stored in HKCU/Environment. You may use REGEDIT to set something there. The Global Environment must be somewher under HKLM and can only be changed by the Administrator.
The Problem is that you must tell the other programs that you changed the variable. Changing Registry is not sufficient.

From SETX description:
A command-line utility that offers a batch method for setting environmental variables in the the user or computer environment from a variety of sources, without any programming or scripting. Besides taking both the variable and value from the command line, it can also take values from Registry keys and offsets into text files. Without SETX, there is no method for settings values directly into the master environment for Windows NT. These are configureable only through Control Panel or through the Registry Editor. The SET command, which is internal to the command interpreter, sets variables only into the environment of the current window. SETX allows you to set values for variables in either the user or computer environment from one of three sources: Command Line Mode, Registry Mode, and File Mode.

Are you really sure that you used SETX and not SET ?
SETX is designed to do exactly that what you want.
Did you install SP3 ???

Avatar of gd015

ASKER

Ok try this.

With NT running open a command prompt.

enter SETX NEWVAR BLAH

close the shell and start another one.
enter SET |MORE and you will see
NEWVAR=BLAH

The setx utility works as intended and the next time the machine is booted, the variable remains set.

I have no problems here.

However, if you edit LOGON.BAT and include the command

SETX NEWVAR ANOTHERBLAH

and restart the following happens.

Run a command shell and type SET |MORE

You will see NEWVAR=BLAH  (The old setting)

If you open regedit and check the environment you will see NEWVAR set to NEWBLAH.  (SETX worked but the system wasn't notified)

If you remove the line from LOGON.BAT and logon again, then run SET |MORE , you will now see NEWVAR=NEWBLAH

Which brings me to the crux of the problem.
SETX does not work when used in a LOGON script.

How do I do it?

ASKER CERTIFIED SOLUTION
Avatar of gantriis
gantriis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes this may work:

Edit or add value (REG_DWORD):

HKEY_CURRENT_USER\software\microsoft\windows nt\currentversion\winlogon

value: RunLogonScriptSync

0 = Don't wait for the logon script to complete before loading the desktop.
1 = Wait for the logon script to complete before loading the desktop.


Avatar of gd015

ASKER

OK, I think we have an answer, although not what I was hoping for.

Gantrii tried using setx and it worked for him.

I had tried all the above answers such as running scripts synchronously etc.

My logon script used a CALL statement to run some commands in another bat file.  It so happened that the call'ed bat file contained the setx commands.

Note that the bat file was CALLed, not STARTed.

If I moved the setx commands back to LOGON.BAT they worked.

I believe this to be some kind of obscure bug in NT.  If you run a command prompt, and then start another prompt from this shell, and then run setx, it still works OK.

So it looks like I am stuck with setting vars in LOGON.BAT for the time being.  Not a great solution when you have a complex procedure to code.

Thanks for everyone's help and comments.

GD015: next time please word your question better and give the details IN ADVANCE! Now we have an answered question where nobody can contribute and you do not have a solution.
Why grade if the answer is not satisfying? Why grade it "A" ?
All the answers and comment doesn't contain more information than given in the resourcekit.

I think your problem is NOT a bug, but unavoidable.