Freigeben über


Setting Environment Variables in a Task Sequence

Some tools require setting an environment variable when they are used.  For example, the User State Migration Tool has several that can be used for troubleshooting.  One is discussed in the Ask the Directory Services Team blog post here.  Unfortunately, the built-in MDT or ConfigMgr Task Sequence steps for capturing and restoring the user state don’t allow you to set environment variables.

Trying to set the environment variable using a script in a preceding step will not work.  If you set an environment variable in the script (e.g. using the SET command in a command shell script) it will only be set for that script.  The task sequencer parent process will not inherit the environment variable, so neither will subsequent steps.  Setting a System (or master) environment variable will have the same issue.  The task sequencer will not inherit the new master environment.   However, if you restart the computer after setting the System environment variable then task sequencer will inherit the updated System environment variables.

So the process for using an environment variable with something like USMT is to have steps before the steps that run the tool (the user state capture and restore steps in this case) that set the variable in the System environment and then restart the computer.  This is shown in the steps prefixed with Custom: in the sample MDT Lite Touch Task Sequence below.

image

Note that you can also place the step to set the variable before an existing earlier reboot in the same phase as well.  If you are using ConfigMgr OSD you should add the step(s) somewhere before any of the user state related steps (Request State Store, etc.).  Finally, if you are using an MDT-integrated ConfigMgr OSD task sequence you will need to add a Use Toolkit Package step after the reboot if you are adding an additional reboot for this.

There are a few ways to set System environment variables with a script.  The SETX command is built into Windows Vista and higher and a version is available in the Windows Server 2003 Resource Kit Utilities that can be used on Windows XP/Windows Server 2003.  The command syntax would be like this:

setx MIG_IGNORE_PROFILE_MISSING 1 /M

To have something a little more MDT-ish for this (with logging, etc.), I’ve attached an MDT script (MDTSetSystemEnvVar.wsf) for doing this.  Copy this to your MDT Scripts folder.

It can be used it two ways.  First, you can explicitly call out the environment variable name and value directly using command line switches.

cscript.exe "%SCRIPTROOT%\MDTSetSystemEnvVar.wsf" /envvar:MIG_IGNORE_PROFILE_MISSING /value:1

Finally, you can specify the name of a task sequence variable name.  The task sequence variable name and value will be used for setting the environment variable.

cscript.exe "%SCRIPTROOT%\MDTSetSystemEnvVar.wsf" /tsvar:MIG_IGNORE_PROFILE_MISSING

 

Disclaimer: The information on this site is provided "AS IS" with no warranties, confers no rights, and is not supported by the authors or Microsoft Corporation. Use of included script samples are subject to the terms specified in the Terms of Use .

This post was contributed by Michael Murgolo, a Senior Consultant with Microsoft Services - U.S. East Region.

MDTSetSystemEnvVar.zip

Comments

  • Anonymous
    January 01, 2003
    Karen, I added another paragraph with this information.  The process is almost exactly the same. Michael Murgolo

  • Anonymous
    January 01, 2003
    The comment has been removed

  • Anonymous
    August 03, 2011
    Could you please show me how to set it up thru sccm ts? thanks

  • Anonymous
    August 04, 2011
    Is there an evironment variable that we can use to specify timezones ?  I image systems across 4 different timezones and don't want to have to maintain an image for each zone.  RIght now, all my images run in one zone then we manually go in to adjust where needed.

  • Anonymous
    December 29, 2014
    Wanted to tell you that this article really helped me a lot; I needed to create an environment variable which I could use for Res Automation Manager, right after MDT did it's job. By using MDTSetSystemEnvVar.wsf I can do this perfectly.