Solved

Set Environment variable from logon script

Posted on 1997-06-30
9
1,035 Views
Last Modified: 2013-12-23
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.
0
Comment
Question by:gd015
9 Comments
 
LVL 3

Expert Comment

by:brauner071697
ID: 1561548
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
0
 

Author Comment

by:gd015
ID: 1561549
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.


0
 

Expert Comment

by:matz
ID: 1561550
Let me se a part of your logon script, please.
0
 
LVL 5

Expert Comment

by:cer
ID: 1561551
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 ???

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:gd015
ID: 1561552
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?

0
 
LVL 2

Accepted Solution

by:
gantriis earned 200 total points
ID: 1561553
Hi gd015,

I have made the same test as you describe using the Windows NT resource kit utility SETX from a logon script to set or change an environment variable on the Windows NT workstation I log on to and it works just fine for me.

However, as brauner pointed out for the winset utility used under Windows 95, the change will only be effective in new command prompts being launched.

Also, if you launch the new command prompt from the logon script (e.g. by "start cmd" in the script) then the new prompt will inherit the environment from the instance running the logon script (where SETX will not set the environment variable). If you need the variable to be available there add a "SET varname=varstring" to the logon script.

If you launch the new command prompt where the variable is needed from the startup group you would probably need to specify the registry value RunLogonScriptSync as 1 in order to prevent the prompt being launched before the logon script has executed. See details for doing this in article Q131503: "How to Delay Start of Program Manager For Logon Scripts" (information still valid for NT 4.0) in the Microsoft knowledge base. If the variable needs to be available in an application or a command prompt it is important that the script has processed the SETX command before the application/prompt is launched (as this is the time where the environment is set).

This will also be a problem if you launch the new command enterpreter from an application that:
1. Was started before the SETX statement executed in your script.
2. Does not handle the environment change notification.

I hope this answers your question. If not, I think you will need to provide a little more information on the timing between the logon script and the new command prompt as well as information on how the new prompt in which you expect the change to appear is being launched.

Cheers
JG
0
 
LVL 5

Expert Comment

by:cer
ID: 1561554
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.


0
 

Author Comment

by:gd015
ID: 1561555
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.

0
 
LVL 5

Expert Comment

by:cer
ID: 1561556
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.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Nslookup is a command line driven utility supplied as part of most Windows operating systems that can reveal information related to domain names and the Internet Protocol (IP) addresses associated with them. In simple terms, it is a tool that can …
Enterprise networks where VoIP phones have been deployed frequently use port configurations that allow both a computer and an IP phone to be plugged into the same switch port but use different VLANs. On Cisco equipment I'm referring to the "native V…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now